Matt
Matt

Reputation: 163

Use PHP to convert PNG to JPEG

I have a website where users have been uploading a bunch of high quality PNG files. I want to use PHP to convert them to JPEG and re-size them to make them smaller in file size.

How can I do this when they upload the file? What is the process for doing this? Is a new image created or is it edited?

Thanks

Upvotes: 0

Views: 1787

Answers (4)

Jean-Philippe Bond
Jean-Philippe Bond

Reputation: 10649

You can use something like this :

function pngTojpg($pngImage, $outputPngFile, $outputJpgFile, $quality) {
    $image = imagecreatefrompng($pngImage);

    //Save the png image
    imagepng($image, $outputPngFile);         

    //Save the jpeg image
    imagejpeg($image, $outputJpgFile, $quality);

    // Free up memory
    imagedestroy($image);
}

"quality is optional, and ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file). The default is the default IJG quality value (about 75)"

The php doc : imagejpeg, imagecreatefrompng

These functions are from the GD library, here the installation instruction : Php GD

Upvotes: 1

Lucas Serafim
Lucas Serafim

Reputation: 3822

Well, you can use simple php code to do that but I use and recomend this library to work with images:

Verot - Class Upload http://www.verot.net/php_class_upload.htm

You can convert images to other format, reduce size, transform and do a lot others stuffs.

Upvotes: 0

yacon
yacon

Reputation: 1122

Just try ImageMagick: http://www.imagemagick.org/script/convert.php

I think, that is what you are looking for.

Upvotes: 0

Ben
Ben

Reputation: 35613

Use ImageMagick to do all kinds of conversions. You should be able to find examples at this link:

Upvotes: 0

Related Questions