Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php GD-从imagecreatefromstring()获取正确的输出函数_Php_Gd - Fatal编程技术网

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

Php GD-从imagecreatefromstring()获取正确的输出函数,php,gd,Php,Gd,我已经通过imagecreatefromstring()读取了我的图像路径,并对其进行了处理,但现在我需要输出它,这就是我遇到的问题 $imgpath = "test.jpg"; //Testing $img = imagecreatefromstring(file_get_contents($imgpath)); //Do stuff to $img imagejpeg($img); imagedestroy($img); 这将把图像(无论类型)加载到$img资源变量中。但是,我目前只使

我已经通过
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无法返回它正在使用的图像类型,那么我似乎必须这样做

谢谢

您可以使用getimagesize()php函数来查看正在处理的图像类型,并据此使用适当的方法。