Use PHP to convert PNG to JPG with compression?

Be careful of what you want to convert. JPG doesn't support alpha-transparency while PNG does. You will lose that information.

Up vote 6 down vote favorite 4 share g+ share fb share tw.

I have a bunch of high quality PNG files. I want to use PHP to convert them to JPG because of it's smaller file sizes while maintaining quality. I want to display the JPG files on the web.

Does PHP have functions/libraries to do this? Is the quality/compression any good? Php png jpg link|improve this question asked Jul 29 '09 at 17:20John3,00942466 95% accept rate.

5 Note that depending on what the images are, JPG won't always have a smaller file size than a PNG, so make sure you're using the right one for your situation. Lbrandy.com/blog/2008/10/my-first-and-last-webcomic – jimyi Jul 29 '09 at 17:27.

Be careful of what you want to convert. JPG doesn't support alpha-transparency while PNG does. You will lose that information.

To convert, you may use the following function: // Quality is a number between 0 (best compression) and 100 (best quality) function png2jpg($originalFile, $outputFile, $quality) { $image = imagecreatefrompng($originalFile); imagejpeg($image, $outputFile, $quality); imagedestroy($image); } This function uses the imagecreatefrompng() and the imagejpeg() functions from the GD library.

This is a small example that will convert 'image. Png' to 'image. Jpg' at 70% image quality: Hope that helps.

You might want to look into Image Magick, usually considered the de facto standard library for image processing. Does require an extra php module to be installed though, not sure if any/which are available in a default installation. HTH.

This is a very good thumbnail script =) Here's an example: $path = The path to the folder where the original picture is. $name = The filename of the file you want to make a thumbnail of. $thumbpath = The path to the directory where you want the thumbnail to be saved into.

$maxwidth = the maximum width of the thumbnail in PX eg. 100 (wich will be 100px). CreateThumbnail($path, $name, $thumbpath, $maxwidth).

I hope you do know that the switch does exist in PHP... – Nicolas Dec 8 '10 at 10:59 I used this and it worked well, thank you! – user1053263 Mar 17 at 4:20.

PHP has some image processing functions along with the imagecreatefrompng and imagejpeg function. The first will create an internal representation of a PNG image file while the second is used to save that representation as JPEG image file.

What I mean is : if you need to convert a lot of images, doing it in PHP might not be the best way : you'll be confronted to memory_limit, max_execution_time, ... I would also say GD might not get you the best quality/size ratio ; but not sure about that (if you do a comparison between GD and other solutions, I am very interested by the results ;-) ) Another approach, not using PHP, would be to use Image Magick via the command line (and not as a PHP extension like other people suggested) You'd have to write a shell-script that goes through all . Png files, and gives them to either convert to create a new . Jpg file for each .

Png file or mogrify to directly work on the original file and override it. As a sidenote : if you are doing this directly on your production server, you could put some sleep time between bunches of conversions, to let it cool down a bit sometimes ^^ I've use the shell script + convert/mogrify a few times (having them run for something like 10 hours one time), and they do the job really well :-).

Do this to convert safely a PNG to JPG with the transparency in white. $image = imagecreatefrompng($filePath); $bg = imagecreatetruecolor(imagesx($image), imagesy($image)); imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255)); imagealphablending($bg, TRUE); imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image)) imagedestroy($image); imagejpeg($bg, $filePath . ".

Jpg", 50);// the 50 is to set the quality, 0 = worst-smaller file, 100 = better-bigger file ImageDestroy($bg).

See this list of php image libraries. Basically it's GD or Imagemagick.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions