ImageCreateFromString and getimagesize in PHP - php

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?

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.

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

Related

how can i check if a given URL is an image?

I thought of using getimagesize($url); but there are still many cases where i can access the image through the browser but the same image returns nothing from getimagesize($url);
$url = 'http://lp.hm.com/hmprod?set=key[source],value[/model/2012/P01 06826 05102 04 0026 4.jpg]&set=key[rotate],value[]&set=key[width],value[]&set=key[height],value[]&set=key[x],value[]&set=key[y],value[]&set=key[type],value[STILL_LIFE_FRONT]&call=url[file:/product/large] ';
Just check the Content-Type header for the string image.
Just use the function get_headers(): http://php.net/manual/en/function.get-headers.php
You can also use curl if it's available on your system, details here: Get mime type of external file using cURL and php
To use getimagesize() you need to download the url and save it as a local file. Then pass the string of the filename stored locally to getimagesize().

php get image size from a string of image content

I have a function that would fetch a remote image via CURL, returning a string variable of the contents of the remote image.
I do not wish to write the content into the file, but wish to get the size of the image.
since getimagesize() only supports a file, is there a function similar to getimagesize() but can support strings of image content?
to clarify:
$image_content = file_get_contents('http://www.example.com/example.jpg');
how to get the image size of $image_content instead of running getimagesize('http://www.example.com/example.jpg');?
Thanks in advance
PHP functions imagecreatefromstring, imagesx, and imagesy.
Something like this;
$image_content = file_get_contents('http://www.example.com/example.jpg');
$image = imagecreatefromstring($image_content);
$width = imagesx($image);
$height = imagesy($image);
FYI, for those coming late to the game, PHP >= 5.4 now includes getimagesizefromstring() which is identical to getimagesize() but accepts a string for the first param instead of a location.
http://php.net/manual/en/function.getimagesizefromstring.php
OK, I found answer for PHP 5.3 and less
if (!function_exists('getimagesizefromstring')) {
function getimagesizefromstring($data)
{
$uri = 'data://application/octet-stream;base64,' . base64_encode($data);
return getimagesize($uri);
}
}

create image from url using imagecreatefromjpeg

I want to create image from php using the function imagecreatefromjpeg(string $filename ),
but when I am providing a image URL as a parameter to this function then this function is not able to create image.
$pic = imagecreatefromjpeg('http://www.example.com/image.jpg');
header("Content-type: image/jpeg");
imagejpeg($pic);
imagedestroy($pic);
You can actually create images from remote files, but please be sure the 'fopen wrappers' have been enabled, see also http://php.net/manual/en/function.file.php
if it doesn't work, what kind of error do you see? and what kind of variable is $pic?
I can't check this right now, but i'd wager it has to be a local file. I.e. you need to have the file on your server.
php.net says: A URL can be used as a filename with this function if the fopen wrappers have been enabled. (http://php.net/manual/en/function.imagecreatefromjpeg.php)

Can I read an image into GD without knowing its type?

GD seems to have various imagecreatefrom_x_ functions which is great if you know the image type ahead of time. In my particular situation, I don't. Is there a way to read in the image without knowing the type?
My only option so far is to save it to disk (expensive!) and then use getimagesize (which also provides mime type) - but this results in me reading in the image twice - once to determine the MIME type and then again to read it into GD.
Alternatively, is it possible to just treat a variable as a file? something like this:
$image = file_get_contents('http://the.remote.file.com/myfile');
# doesn't work with already read in image ... can I treat this as a file w/o saving it?
$info = getimagesize($image);
You can use imagecreatefromstring(), which is indifferent to the actual file format and the best option if you've read in the file already.
But for practical purposes you should be able to use getimagesize($filename). You don't have to read in the file before. Just use the filename or url.
print_r(
getimagesize("http://sstatic.net/stackoverflow/img/sprites.png?v=2")
);
Use imagecreatefromstring, imagesx and imagesy (the latter two to prevent another request, since it seems to concern you):
$image = imagecreatefromstring(file_get_contents('http://path/to/image.jpg'));
list($width, $height) = array(imagesx($image), imagesy($image));

php get the KB size of an image

i've been googleing but all i get is getimagesize and filesize.
getimagesize dosent get the KB size just width and height which is not what im looking for.
filesize give me the message Warning: filesize() [function.filesize]: stat failed for
the file in question is 51kb .jpg file
$imgsize=filesize("http://localhost/projects/site/schwe/user/1/1.jpg");
does not work,
how do i accomplish this?
You cannot get file size of remote elements, either give a relative path on your system OR do a file_get_contents() to get contents first
. Thus, instead of http:// , do a filesize('/path/to/local/system') . Make sure its readable by php process
You can't look up the filesize of a remote file like that. It is meant for looking at the filesize of local files.
For instance...
$imgsize = filesize( '/home/projects/site/1.jpg' );
filesize() is the function to use. It might be failing because
You're trying to get a web address & URL wrappers may not be turned on
That URL isn't valid.
If you're trying to run filesize() on a local file, reference the file system path, not some web URL.
Or you can also do something like :
$header = get_headers($url);
foreach ($header as $head) {
if (preg_match('/Content-Length: /', $head)) {
$size = substr($head, 15);
}
}
filesize takes the name of the file as argument not a URL and it returns the size of the file in bytes. You can divide the return value with 1024 to get the size in KB.
I had the same problem, which i solved like this. I don't know how optimal it is, but it works for me:
getimagesize("http://localhost/projects/site/schwe/user/1/1.jpg");
$file_size = $file[0]*$file[1]*$file["bits"];

Categories