简体   繁体   English

GD-从imagecreatefromstring()获取正确的输出函数

[英]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. 我已经通过imagecreatefromstring()读取了图像路径,并对其进行了处理,但是现在我需要输出它,这就是我遇到的问题。

$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. 无论类型如何,这都会将图像加载到$img资源变量中。 However, I'm currently just using imagejpeg() to output the image, which works fine if the source ( $imgpath ) was a jpeg. 但是,我目前仅使用imagejpeg()输出图像,如果源( $imgpath )是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. 但是,如果我将imagejpeg更改为imagepng ,则服务器必须将jpeg转换为png,这在图像加载时间上非常明显。

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. 我试图避免使用文件名检查方法,但是如果GD无法返回正在处理的图像类型,则似乎必须采用这种方法。

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. 您可以使用php函数getimagesize()来查看要处理的图像类型,并根据其使用适当的方法。 http://php.net/manual/en/function.getimagesize.php http://php.net/manual/zh/function.getimagesize.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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