![](/img/trans.png)
[英]Issue with creating an image with PHP GD `imagecreatefromstring` function
[英]GD - Getting the proper output function from imagecreatefromstring()
我已经通过imagecreatefromstring()
读取了图像路径,并对其进行了处理,但是现在我需要输出它,这就是我遇到的问题。
$imgpath = "test.jpg"; //Testing
$img = imagecreatefromstring(file_get_contents($imgpath));
//Do stuff to $img
imagejpeg($img);
imagedestroy($img);
无论类型如何,这都会将图像加载到$img
资源变量中。 但是,我目前仅使用imagejpeg()
输出图像,如果源( $imgpath
)是jpeg,则可以正常工作。 但是,如果我将imagejpeg
更改为imagepng
,则服务器必须将jpeg转换为png,这在图像加载时间上非常明显。
由于我永远不会确切知道通过函数输入的是哪种类型的图像,我如何分辨它是哪种类型的图像并相应地采取行动以进行输出? 我试图避免使用文件名检查方法,但是如果GD无法返回正在处理的图像类型,则似乎必须采用这种方法。
谢谢!
您可以使用php函数getimagesize()来查看要处理的图像类型,并根据其使用适当的方法。 http://php.net/manual/zh/function.getimagesize.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.