I've read my image path through imagecreatefromstring()
, processed it, but now I need to output it, which is where I'm stuck.
$imgpath = "test.jpg"; //Testing
$img = imagecreatefromstring(file_get_contents($imgpath));
//Do stuff to $img
imagejpeg($img);
imagedestroy($img);
This will load up the image, regardless of type, into the $img
resource variable. However, I'm currently just using imagejpeg()
to output the image, which works fine if the source ($imgpath
) was a jpeg. However, if I change imagejpeg
to imagepng
, the server has to convert the jpeg to a png, which is very noticeable on image load times.
Since I will never know precisely what type of image is being fed through the function, how can I tell what type of image it is and act accordingly for output? I am trying to avoid the filename checking method, but it seems I'll have to go that way if there's no way for GD to return what type of image it's working with.
Thanks!