简体   繁体   中英

GD - Getting the proper output function from imagecreatefromstring()

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!

You can use getimagesize() php function in order to see what type of image you are dealing with and accordingly use the proper method based on it. http://php.net/manual/en/function.getimagesize.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM