ImageCreateFromString and getimagesize in PHP

2020-06-05 07:32发布

Currently if a user POST/uploads a photo to my PHP script I start out with some code like this

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. 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?



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?

4条回答
孤傲高冷的网名
2楼-- · 2020-06-05 08:08

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.

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!

查看更多
Luminary・发光体
3楼-- · 2020-06-05 08:14

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.

Also, using getimagesize() on URLs isn't good because it'll consume much more bandwidth than the alternative exif_imagetype(), from the PHP Manual:

When a correct signature is found, the appropriate constant value will be returned otherwise the return value is FALSE. The return value is the same value that getimagesize() returns in index 2 but exif_imagetype() is much faster.

That's because exif_imagetype() will only read the first few bytes of data.

查看更多
乱世女痞
4楼-- · 2020-06-05 08:16

If you've already got an image resource, you'd get the size using the imagesx and imagesy functions.

查看更多
该账号已被封号
5楼-- · 2020-06-05 08:26

getimagesize can be used with 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.

查看更多
登录 后发表回答