[英]ImageCreateFromString and getimagesize in PHP
Currently if a user POST/uploads a photo to my PHP script I start out with some code like this 目前,如果用户POST /上传照片到我的PHP脚本,我开始使用这样的代码
getimagesize($_FILES['picture1']['tmp_name']);
I then do a LOT more stuff to it but I am trying to also be able to get a photo from a URL and process it with my other existing code if I can. 然后我做了很多其他的东西,但我也试图从URL获取一张照片并用我的其他现有代码处理它,如果可以的话。 SO I am wanting to know, I f I use something like this
所以我想知道,我使用这样的东西
$image = ImageCreateFromString(file_get_contents($url));
Would I be able to then run getimagesize() on my $image variable? 我能在我的$ image变量上运行getimagesize()吗?
UPDATE UPDATE
I just tried this... 我刚试过这个......
$url = 'http://a0.twimg.com/a/1262802780/images/twitter_logo_header.png';
$image = imagecreatefromstring(file_get_contents($url));
$imageinfo = getimagesize($image);
print_r($imageinfo);
But it didnt work, gave this. 但它没有用,给了这个。
Warning: getimagesize(Resource id #4) [function.getimagesize]: failed to open stream: No such file or directory in
Any idea how I can do this or something similar to get the result I am after? 知道如何做到这一点或类似的东西,以获得我追求的结果?
I suggest you follow this approach: 我建议你按照这种方法:
// 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
Using exif_imagetype()
is a lot faster than getimagesize()
, the same goes for ImageSX()
/ ImageSY()
, plus they don't return arrays and can also return the correct image dimension after the image has been resized or cropped for instance. 使用
exif_imagetype()
比getimagesize()
快得多, ImageSX()
/ ImageSY()
,加上它们不返回数组, 并且在调整图像大小或裁剪后也可以返回正确的图像尺寸。
Also, using getimagesize()
on URLs isn't good because it'll consume much more bandwidth than the alternative exif_imagetype()
, from the PHP Manual : 此外,在URL上使用
getimagesize()
并不好,因为它将比PHP手册中的替代exif_imagetype()
消耗更多的带宽:
When a correct signature is found, the appropriate constant value will be returned otherwise the return value is FALSE.
找到正确的签名后,将返回相应的常量值,否则返回值为FALSE。 The return value is the same value that
getimagesize()
returns in index 2 butexif_imagetype()
is much faster.返回值与
getimagesize()
在索引2中返回的值相同,但exif_imagetype()
更快。
That's because exif_imagetype()
will only read the first few bytes of data. 那是因为
exif_imagetype()
只会读取数据的前几个字节。
如果您已经拥有图像资源,则可以使用imagesx和imagesy函数获得大小。
getimagesize can be used with HTTP. getimagesize可以与HTTP一起使用。
Filename - It can reference a local file or (configuration permitting) a remote file using one of the supported streams .
文件名 - 它可以使用其中一个支持的流引用本地文件或(配置允许) 远程文件 。
Thus 从而
$info = getimagesize($url);
$image = ImageCreateFromString(file_get_contents($url));
should be fine. 应该没事。
Not sure if this will help, but I ran into a similar issue and it turned out the firewall controlled by my host was blocking outgoing http connection from my server. 不确定这是否有帮助,但我遇到了类似的问题,结果发现我的主机控制的防火墙阻止了从我的服务器传出的http连接。
They changed the firewall settings. 他们更改了防火墙设置。 My code then worked.
然后我的代码工作了。
BTW: I thought this might have been an issue when I tried file_get_contents() on a number of urls, none of which worked! 顺便说一句:当我在许多网址上尝试使用file_get_contents()时,我认为这可能是一个问题,但没有一个工作!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.