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().
Related
I am trying to get a file size of an image from a remote url, I am trying to this like so:
$remoteUrl = $file->guid;
//remote url example: http://myApp/wp-content/uploads/2017/05/Screen-Shot-2017-05-08-at-10.35.54.png
$fileSize = filesize($remoteUrl);
But, I get:
filesize(): stat failed for
http://myApp/wp-content/uploads/2017/05/Screen-Shot-2017-05-08-at-10.35.54.png
You can use HTTP headers to find the size of the object. The PHP function get_headers() can get them:
$headers = get_headers('http://myApp/wp-content/uploads/2017/05/Screen-Shot-2017-05-08-at-10.35.54.png', true);
echo $headers['Content-Length'];
This way you can avoid downloading the entire file. You also have access to all other headers, such as $headers['Content-Type'], which can come in handy if you are dealing with images (documentation).
That error usually means the supplied URL does not return a valid image. When I try to visit http://myapp/wp-content/uploads/2017/05/Screen-Shot-2017-05-08-at-10.35.54.png it does not show an image in my browser. Double check that the returned URL from $file->guid; is correct.
You will need to try a different method, the http:// stream wrapper does not support stat() which is needed for the filesize() function.
This question has some options you might use, using curl to make a HEAD request and inspect the Content-Length header.
I am trying to save to disk an image that is served to me via a JSON result. The returned JSON result property that I am interested in is this:
https://i.scdn.co/image/6cd03f58ddf30a1393f06d6469973ba16ac908df
Which is the correct image. The problem is that, while the above URL does display the image, it does not allow me to download it, yet I can download it by right-clicking on it.
What I need to be able to do is, using my PHP code, save it to disk.
I have no issues saving results from other sites that give results that link to a direct image extension (.jpg, .gif or .png). But I have not been able to figure out how to programmatically download the image from the above URL.
Is it possible?
This is the code that I use, which works correctly on results that give a URL that has a correct image extension. The URL returned is loaded into the $largeimg variable.
$input = $largeimg;
$output = 'image.jpg';
file_put_contents($output, file_get_contents($input));
How do I achieve this?
file_get_contents() is able to accept raw URI arguments. Your code works perfectly for me, if modified in the way:
$input = 'https://i.scdn.co/image/6cd03f58ddf30a1393f06d6469973ba16ac908df';
So, file_get_contents() can download the image directly. I think, the problem is your $largeimg variable.
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)
What are some possible ways to save an image or make use of it that is generated from a PHP script. Using save as it does not help though.
This is not an image created by me that's why I want to avoid get_contents.
here is the picture
and here is the url
https://render01.fontshop.com/fonts/font_rend.php?idt=f&id=38005&rbe=fsifr&rt=how+do+I+save+this?&rs=38&w=500&bg=ffffff&fg=000000&tp=0.0
Just write the content of the URL to a file
<?php
file_put_contents("img.png", file_get_contents("http://render01.fontshop.com/fonts/font_rend.php?idt=f&id=38005&rbe=fsifr&rt=how+do+I+save+this?&rs=38&w=500&bg=ffffff&fg=000000&tp=0.0"));
Using file_put_contents() function. If you don't have data in variable and want to readout use file_get_contents()
Since you are not generating the image in your own code, the simplest would be a combo of file_get_contents and file_put_contents:
$url = '...'; // your url here
$data = file_get_conents($url);
file_put_conents('image.png', $data);
In this specific case the render is a PNG image, but if there's a possibility of it being a JPEG or something else then you need to somehow detect that as well. I 'm not giving any suggestions for this because there's not enough info to go by.
You can define a filename in imgpng() or the other functions to tell PHP to store the picture instead of sending it to the calling browser.
I understand you want to save it on the client, with a browser, not on the server.
"Save as" worked fine for me (Firefox 7). In Chrome you'll have to specify the extension of the filename manually. Did not test other browsers, but it should work similarly
You can do this from the terminal using the curl command.
curl -o out.png 'http://render01.fontshop.com/fonts/font_rend.php?idt=f&id=38005&rbe=fsifr&rt=how+do+I+save+this?&rs=38&w=500&bg=ffffff&fg=000000&tp=0.0'
This will save the file as out.png
use imagepng function.
It will return file to browser or save it specified location.
Need to set parameter for function to save image on specified location.
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!