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?
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!
I suggest you follow this approach:
Using
exif_imagetype()
is a lot faster thangetimagesize()
, the same goes forImageSX()
/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 alternativeexif_imagetype()
, from the PHP Manual:That's because
exif_imagetype()
will only read the first few bytes of data.If you've already got an image resource, you'd get the size using the imagesx and imagesy functions.
getimagesize can be used with HTTP.
Thus
should be fine.