How can I save this php created image? - 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.

Related

Programmatically (PHP) save image with no extension

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.

How to return an image created with php

I want to return an image when a PHP script is called.
The problem I am having is, that I have the image saved in a variable ($image).
I know I could first save the image on the server with
imagepng($image, 'image.png');
and after that return it with something like
readfile('image.png');
but I am sure there is a more elegant way to do the job, I just don't know it.
Is there a way to combine these two commands or any other way, that allows me not to use a temporary file?
imagepng($image), with no second parameter, will simply dump the raw PNG data out to the client (e.g. the browser). No need to write it to a file, then read that file back in.
This is clearly documented in the man page: http://php.net/imagepng and applies to ALL of the "save" functions in GD.

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().

Using PHP to upload a file that's generated by a URL

So, I have a little PHP code that uses Kaywa to generate and display a QR code:
echo "<img src='http://qrcode.kaywa.com/img.php?s=10&d=$qr_url' alt='QR code' />";
Easy peasy. But for the sake of having a backup, I'd like to save this image to my server, maybe in "myserver/qrbackups". I know how to make PHP upload a file from a form, but can I do from a retrieved image URL?
See file_get_contents.
$data = file_get_contents("http://qrcode.kaywa.com/img.php?s=10&d=$qr_url");
$saved = file_put_contents('/path/to/myserver/qrbackups/the-code.png', $data);
Keep in mind /path/to/myserver/qrbackups/the-code.png should be a unique file name for each individual QR code.
You can use cURL to programmatically access URLs.
http://us3.php.net/manual/en/curl.examples-basic.php
You can try to use curl or file_get_contents to pull the file from the server.
For example:
http://www.phpriot.com/articles/download-with-curl-and-php
And after getting the file just display the one you got from your server or remote.
copy('http://domain.com/path', '/tmp/file.jpeg');
should work for you. And, as others have pointed you, cURL will work too.
If you have the GD extension on your server, you can use a library like PHP QR Code to eliminate the dependency on Kaywa. Usage requires only one line of code wherever you want to generate a QR code.

Saving an Image

I have a picture url like this http://www.address.com/image.jpg
I want do dynamically save this image on my server
How can i achieve this using php
Thanks
You can get it using file_get_contents() and saving it using file_put_contents(). Something like:
$image = file_get_contents('http://www.address.com/image.jpg');
file_put_contents('image.jpg', $image);
You also might want look into cURL to fetch the image; it should perform better than file_get_contents() (unless you compile curl with --with-curlwrappers)

Categories