Zac Brown
Zac Brown

Reputation: 6093

Convert Images with PHP without using ImageMagick

I need to convert a .JPG, .JPEG, .JPE, .GIF, etc to a .PNG from my PHP webpage without using ImageMagick. Any ideas?


Here is the code I found and am trying to work with:

<?php
header("content-type: image/png");
$original_filename = $_HTTP_POST_FILES['uploaded_file']; 
imagepng($original_filename,'border/testconvert.png',9);

?>

Upvotes: 0

Views: 1708

Answers (2)

Trufa
Trufa

Reputation: 40747

function jpg2png($originalFile, $outputFile, $quality) {
    $image = imagecreatefromjpeg($originalFile);
    imagepng($image, $outputFile, $quality);
    imagedestroy($image);
}

Try something like this.

Tell me if if works!!

Good luck

Upvotes: 0

richsage
richsage

Reputation: 27102

Who needs ImageMagick? Take a look at the built-in image functions using gd.

EDIT Basic example:

<?php
 $filename = "myfolder/test.jpg";
 $jpg = @imagecreatefromjpeg($filename);
 if ($jpg)
 {
   header("Content-type: image/png");
   imagepng($jpg);
   imagedestroy($jpg);
   exit;
 }

 // JPEG couldn't be loaded, maybe show a default image
?>

You can do more with this such as change compression and quality values etc, save the output to a file instead of outputting to the browser and so on - check the docs for further info :-)

Note that the image functions issue warnings/notices etc if there are problems loading an image, hence the use of the @ symbol to suppress, otherwise you'll get spurious output instead of just the image data.

Upvotes: 2

Related Questions