Web Development Blog

PHP: How to Save Image From Web (Remote Image)

$remoteFile can be a link to any file, not only an image.

Please keep in mind that $localFile should have same extension as $remoteFile.

<?
$remoteFile
='http://chart.apis.google.com/chart?chs=150x150&cht=qr&chld=L|0&chl=http://superiorwebsys.com'
;
$localFile=$_SERVER['DOCUMENT_ROOT'].'/tmp/image2.png'
;
saveFileFromTheWeb($remoteFile,$localFile
);
function
saveFileFromTheWeb($remoteFile,$localFile
){
   $ch = curl_init
();
   $timeout = 0
;
   curl_setopt ($ch, CURLOPT_URL, $remoteFile
);
   curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout
);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1
);
   curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1
);
   $image = curl_exec($ch
);
   curl_close($ch
);
   $f = fopen($localFile, 'w'
);
   fwrite($f, $image
);
   fclose($f
);
}
?>

Thanks to rgnelson for the question asked here: Generate QR Code with PHP Using Google API

Associated tags:  PHP, Curl

Comments:

Ivascos wrote on June 1, 2011 at 12:21
Great post. This site growing into good development resource. Keep it up!!!

Tambers wrote on July 11, 2011 at 21:11
Keep these articles coming as they’ve oneped many new doors for me.

Chandan Sahoos wrote on February 22, 2012 at 06:54
How to display QR code image and its below display a link for download this QR code image with google api

Michael Pankratovs wrote on February 22, 2012 at 11:13
You can read Google API output using PHP and then send a force-download header:
header("Content-Type: application/force-download");
Also, in order to download an image you can use your browser (in IE right click on the image and select "save as")

Chandan Sahoos wrote on February 23, 2012 at 01:37
Sorry, I cant describe properly.

1) first display the QR image
2) just below the image there will be a link for download the QR code image
3) when user click on the link, the current QR code will be download.

the QR code generate with url with user id, email and password.
like --- http:website.com/index.php?userid=2&email=xyz@gmail.com&pass=xyz&lang=en

Michael Pankratovs wrote on February 23, 2012 at 19:21
Once QR code is generated and you saved it on your server (code for this is above)
You create another php file, that you will link to under QR code:
<?
header("Content-Type: application/force-download");
header("Content-Disposition: attachment;
filename="".$fileName."";" );
header("Content-Transfer-Encoding: binary");
readfile(filePath);
exit();
?>
where $fileName is a name of file with needed QR code and $filePath is path to the file on your server (Ex: $_SERVER[`DOCUMENT_ROOT`]."/dir/file.jpg")

Chandan Sahoos wrote on February 24, 2012 at 01:22
Can I direct download when I click on download link with out save image on our server? I want to display the QR code image and want to download the image without saving on server.

and another question is am I send the url with user`s id, email, password for generated QR code?

Michael Pankratovs wrote on February 24, 2012 at 09:06
If you need QR code to direct to the page with user`s ID and other information then yes, you pass it to QR code generator, but I would not recommend sending password - this is not secure at all.
You can try to use PHP function like fopen() or fsockopen()
I will post a code to that later on today in our blog.


Add Comment:

CAPTCHA