0

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!

0

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.