CodeIgniter Forums
Converting Image Formats - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Converting Image Formats (/showthread.php?tid=48881)

Pages: 1 2


Converting Image Formats - El Forum - 01-31-2012

[eluser]cassy[/eluser]
Hey guys, this is my first post here. I'm new to PHP and CI and I love it! :-)

As I'm so new, I hope someone can help me. How do I use the Image Manipulation Class to convert between file formats?

Lets say I have a .PNG file on my server; how can I convert this to a .JPG format?

I'm looking forward to your replies.

Thanks.
Cassy.


Converting Image Formats - El Forum - 02-01-2012

[eluser]vbsaltydog[/eluser]
There are no reformat methods in the class:

http://ellislab.com/codeigniter/user-guide/libraries/image_lib.html

however, since the class does support all three of the major image libraries, you can extend the class and add a reformat method.


Converting Image Formats - El Forum - 02-01-2012

[eluser]Jason Stanley[/eluser]
I only had 2 minutes to look at this. The image lib class looks at the extension of the new image config variable. I assume it then outputs the correct image type based on the extension. Have you actually tried setting the $config['new_image'] variable? You could always then unlink the current image to get rid of the duplicate image.



Converting Image Formats - El Forum - 02-01-2012

[eluser]cassy[/eluser]
@vbsaltydog
I'm not sure how I can do that. My version of XAMP on Windows only has the GD library.


@jason
If the source is a PNG and I specify the target to be a JPG, it saves as a new "JPG" but it is really a PNG with a JPG extension!




Converting Image Formats - El Forum - 02-01-2012

[eluser]Jason Stanley[/eluser]
That sounds like a bug to me. You should log it on GitHub.

In the meantime you can either find another library or write something yourself.

I would start here:
http://www.php.net/manual/en/function.imagecreatefrompng.php

Then:
http://www.php.net/manual/en/function.imagejpeg.php

Its super easy.


Converting Image Formats - El Forum - 02-01-2012

[eluser]cassy[/eluser]
@jason,
That scares me. I'm very new at this and it's very daunting.

Can someone compile the ImageMagick library into a DLL file for me?
I don't really know account compiling stuff like that and I can't find the DLL anywhere.


Converting Image Formats - El Forum - 02-01-2012

[eluser]vbsaltydog[/eluser]
Where is your application eventually going to reside? On a Windows Server or a Linux Server? If a Linux Server, you don't want to use a dll and FWIW, most developing is done in a *nix environment.


Converting Image Formats - El Forum - 02-01-2012

[eluser]Jason Stanley[/eluser]
Cassy.. what is daunting about the PHP manual?

What is so hard about doing doing this!? A + B = C

Code:
$image = imagecreatefrompng('path/to/image');
imagejpeg($image, 'path/to/new/image');
imagedestroy($image);

To get better you read and experiment. If you can't even be bothered to do that I don't know why you are programming...


Converting Image Formats - El Forum - 02-01-2012

[eluser]cassy[/eluser]
@vbsaltydog
I'm not sure. I'm doing this to learn so that I can apply to a college next year. I already do web design but I want to be a "Web Developer". My dad said that's where the money is. I don't know much about Linux. I only have access to a Windows 7 computer and am running XAMP + CI.

@jason
Interesting. Thanks for the Tip.

Can you tell me how I can programatically determine if a file is an image and not a, lets say a malicious .PHP file renamed to a .JPG file?


Converting Image Formats - El Forum - 02-01-2012

[eluser]vbsaltydog[/eluser]
http://php.net/manual/en/ref.image.php