PHP中的ImageCreateFromString和getimagesize
目前,如果一个用户将一张照片上传到我的PHP脚本中,我会从下面的代码开始PHP中的ImageCreateFromString和getimagesize,php,image-processing,Php,Image Processing,目前,如果一个用户将一张照片上传到我的PHP脚本中,我会从下面的代码开始 getimagesize($_FILES['picture1']['tmp_name']); $image = ImageCreateFromString(file_get_contents($url)); 然后我对它做了很多事情,但我也在尝试从URL获取一张照片,如果可以的话,用我现有的其他代码处理它。所以我想知道,如果我用这样的东西 getimagesize($_FILES['picture1']['tmp_nam
getimagesize($_FILES['picture1']['tmp_name']);
$image = ImageCreateFromString(file_get_contents($url));
然后我对它做了很多事情,但我也在尝试从URL获取一张照片,如果可以的话,用我现有的其他代码处理它。所以我想知道,如果我用这样的东西
getimagesize($_FILES['picture1']['tmp_name']);
$image = ImageCreateFromString(file_get_contents($url));
然后我能在$image变量上运行getimagesize()吗
更新 我刚试过这个
$url = 'http://a0.twimg.com/a/1262802780/images/twitter_logo_header.png';
$image = imagecreatefromstring(file_get_contents($url));
$imageinfo = getimagesize($image);
print_r($imageinfo);
但它不起作用,给了这个
Warning: getimagesize(Resource id #4) [function.getimagesize]: failed to open stream: No such file or directory in
你知道我怎样做才能得到我想要的结果吗?getimagesize可以与HTTP一起使用 文件名-它可以使用支持的流之一引用本地文件或(配置允许)远程文件 因此
应该没问题。如果您已经有图像资源,您可以使用和imagesy函数获得大小。我建议您采用以下方法:
// if you need the image type
$type = exif_imagetype($url);
// if you need the image mime type
$type = image_type_to_mime_type(exif_imagetype($url));
// if you need the image extension associated with the mime type
$type = image_type_to_extension(exif_imagetype($url));
// if you don't care about the image type ignore all the above code
$image = ImageCreateFromString(file_get_contents($url));
echo ImageSX($image); // width
echo ImageSY($image); // height
使用exif\u imagetype()
比getimagesize()
快得多,ImageSX()
/ImageSY()
也一样,而且它们不返回数组,例如,在调整图像大小或剪切图像后,也可以返回正确的图像尺寸
另外,在URL上使用getimagesize()
也不太好,因为它会比另一种exif\u imagetype()
消耗更多的带宽,它来自:
当找到正确的签名时
将使用适当的常量值
否则返回值为
错。返回值相同
getimagesize()
返回的值
索引2,但exif\u imagetype()的
更快
这是因为
exif\u imagetype()
将只读取前几个字节的数据。不确定这是否有帮助,但我遇到了类似的问题,结果是主机控制的防火墙阻止了来自服务器的传出http连接
他们更改了防火墙设置。然后我的代码工作了
顺便说一句:我想这可能是一个问题,当我尝试在许多URL上使用file_get_contents()时,所有这些都不起作用 询问者可能只需要关于图像的高度和宽度的其他信息。例如图像类型、mime类型等等。很好,我试图从$image的值中获取ImageSize,因为它已经抓取了图像,而不是两次点击URL,但是如果这是需要的方式,那么这很好,下载文件不是两次吗?我不知怎么错过了
imagesx()
和imagesy()
在手册中。谢谢你+1.