0

When images are uploaded to my site I convert all of them to jpgs using gd lib:

$image = imagecreatefrompng($img);
$outputFile = str_replace('png', 'jpg', $img);
unlink($img);
imagejpeg($image, $outputFile, $quality);
imagedestroy($image);

This works perfectly fine until I need to make a copy of an image for cropping. It fails in imagecreatefromjpeg()

Looking at the image details with getimagesize($outputFile) I can see that even though I have successfully converted a png or gif to a jpg, and it has a jpg file extension the mime type is still image/png or image/gif.

For example:

Array
(
    [0] => 250
    [1] => 188
    [2] => 3
    [3] => width="250" height="188"
    [bits] => 8
    [mime] => image/png
)

How do I change the mime type when I convert to jpg then? Or what can I do to stop it failing when I need to do something to the image like copy it or resize it for cropping using imagecreatefromjpeg()?

user3065931
  • 531
  • 7
  • 20

1 Answers1

0

you can only use imagecreatefromjpeg() on an jpg-image
in your example above, to check your created output file, you would have to change your code to

   <?php... getimagesize($outputFile); .... ?>


$img contains your source file path... . If you want to make a jpeg copy from a png image, imagecreatefrompng(...) and then outputing it with imgejpeg(...) would be the way to do it...

useDuser
  • 21
  • 5