Our blog
PHP Save Images Using cURL
Some hosts disabled the ini setting allow_url_fopen. This also means that the ability to easily grab images by calling imagecreatefromjpeg($img) where $img is a url for an external image does not work.
This is a shame as this technique is pretty easy..
-
$remote_img = 'http://www.somwhere.com/images/image.jpg';
-
$img = imagecreatefromjpeg($remote_img);
-
$path = 'images/';
-
imagejpeg($img, $path);
However I was tearing my hair out trying to get a product feed integration system to work on a host that has disabled the allow_url_fopen mechanism which meant the above wouldnt work.
Thankfully cURL was still working. So here is the function I made for grabbing and saving an image using cURL instead:
-
//Alternative Image Saving Using cURL seeing as allow_url_fopen is disabled - bummer
-
function save_image($img,$fullpath){
-
$ch = curl_init ($img);
-
curl_setopt($ch, CURLOPT_HEADER, 0);
-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
-
$rawdata=curl_exec($ch);
-
curl_close ($ch);
-
}
-
}
Another Problem Solved
More Reading:
56 Comments
|
Hi! I have a problem with image downloading from Remote server, which sends headers: image/jpeg for some images I would like to use curl to check once with remote server for content-type and accordingly redirect for image/jpeg or else download using curl to set correct image/jpeg header for binary data. Can you help? Dear Admin! pls email me on email specified, txt only. |
|
emailed you |
|
can you please post how you did it? |
|
not had an email from her yet... I suspect though that the solution is to download whatever it is via curl and then check for a valid image once we have a local file to work on. |
|
Hi Joseph! Thanks for replying. Here are the images: i.indiafm.com/stills/celebrities/sada/thumb1.jpg AND i.indiafm.com/stills/celebrities/sada/thumb5.jpg See both image requests have different content-type headers. I await a response from you asap. Thanks PS: I actually have somehow implemented something for the time being as you suspected... |
|
This code is saving both images fine for me: PHP:
|
|
Litza July 29th, 2009 |
Just wanted to say thanks - this worked like a charm and saved me lots of time! |
|
nur August 30th, 2009 |
this cURL example isn't working for me! it saves empty image file! what would be the reason? even appreciating for email. |
|
Hi, these scripts are working perfekt. is there a way, to translate the url first, and then download the image? hope you can help. greetings Philipp |
|
|
|
WOW!!!! Great site with great HELP !!! This tutorial helps me too much... Thanks |
|
I have a business card application and am trying to get 4 items created from file.drawtext2.php to be saved into 1 png image. |
|
I mis-stated what I was trying to do. I am not using file.drawtext2.php anymore and am trying to get your code to save all images into 1 complete image. Do you know how I can do this? |
|
Hi Ed thats beyond the scope of this blog post I'm afraid |
|
Hussein January 30th, 2010 |
Hi, this is a great thank you. I am trying to save website screen shots locally so that I don't over use the free service offered by a third party. Here is how the image urls look like: I would like to be able to save it with the domain name in the following format: domain.ext.jpg I know how to extract the domain from the url but don't know where I need to change the name of the file before it is saved because currently it saves it with the last segment of the long url. example: url: http://www.example.com/dir/remotedir url: http://www.example.com/dir/remotefile.php I would like it to save the image in both cases above as: example.com.jpg Thank you in advance. |
|
Thanks a lot or this help |
|
I keep failing to get this script to work. Right out of the gate I am getting undefined functions for $save_image(). |
|
KB February 27th, 2010 |
try 2 things Player911. 1) Remove the $ from save_image and if that doesn't work, put the function above the line where you call save_image(img,$fullpath); |
|
Rab May 13th, 2010 |
Help me out too - #3 in Google for the keyowrds I searched. Nice one admin, that's another one I owe you |
|
lol HTH |
|
Very well put together tutorial, well worth the read thank you |
|
Nick July 7th, 2010 |
Nice tutorial.but it creates a problem when the image mime type will not retrieve. You can find almost all help related to the open sources from, |
|
Rombaut September 26th, 2010 |
Hey, This tut looks great but still I'm noob at this. However I was wondering: Is there a possibility to get specific information from a remote image like width, height.. etc.. using cURL? Appreciate any suggestions.. Thank you!! |
|
download the image and then use the getimagesize function |
|
TerMa December 11th, 2010 |
this cURL example isn't working for me! it saves empty image file (Zero Size)! what would be the reason? even appreciating for email. |
|
TerMa December 11th, 2010 |
Upon further investigation, it appears that the problem of my curl is Error #6 = " Couldn't resolve host 'aHostName' " and I see somewhere "becuse the DNS resolution on the absolut Apache server is not working consistently" but I never get true result. what would be the reason? even appreciating for email. |
|
thanks for your advice .. I had use curl to grab image from other website .. its working very well |
|
Curl is awesome and you're da man. Thank you! |
|
RJ March 1st, 2011 |
Hello, I'm having the same issue that others have had where a png file is created, but there's no actual image showing up. Here is my code: $remote_img = 'http://chart.apis.google.com/chart?chxl=0:|Jan|Feb|Mar|Jun|Jul|Aug|1:|100|75|50|25|0&chxt=x,y&chs=300x150&cht=lc&chd=t:60.037,57.869,56.39,51.408,42.773,39.38,38.494,31.165,30.397,26.876,23.841,20.253,16.232,13.417,12.677,15.248,16.244,13.434,10.331,10.58,9.738,10.717,11.282,10.758,10.083,17.299,6.142,19.044,7.331,8.898,14.494,17.054,16.546,13.559,13.892,12.541,16.004,20.026,18.529,20.265,23.13,27.584,28.966,31.691,36.72,40.083,41.538,42.788,42.322,43.593,44.326,46.152,46.312,47.454&chg=25,25&chls=0.75,-1,-1'; $ch = curl_init ($remote_img); if(file_exists($path)) curl_close ($ch); Any help would be appreciated. Thank you |
|
Julio March 22nd, 2011 |
What a great help, thx |
|
Joseph March 29th, 2011 |
Hi, great tutorial. Is there any way to tweak it in order to be able to change the name that the images is saved as? Also if possible to resize it? |
|
Noel B March 31st, 2011 |
I used the script to store images into server from remote sources but, however the image saved has zero bytes... Any clues to whats wrong? Thanks! |
|
You savet my life with that cURL saving |
|
waleed April 21st, 2011 |
great script. |
|
Kory April 23rd, 2011 |
Any suggestions on how to have this script read the url from a database. I need to harvest a large number of images, and the names are stored in my mysql db named jos151, table jos_vm_product, and field name product_full_image. Thanks for your great posts. |
|
For all those having empty image issues; use chmod after you have cURLed one out. ... curl_close ($ch); //This sets permissions on img making it viewable. chmod($image_name,777); |
|
Azirius May 18th, 2011 |
This helped a lot with an Facebook avatar integration system that I needed to develop. Sourcing off of Facebook's image server seemed to slow down page loads a fair chunk, so, having this system download a new one every 24 hours is a great way for me to keep down page loads. |
|
Matthew June 9th, 2011 |
How can you make this script work with an image url like the following: My images keep appearing to be 0kb in size. My source code is the following: $netsuite_image = DIR_FS_CATALOG . DIR_WS_IMAGES . "netsuite_" . $_POST['netsuite_id'] . ".jpg"; |
|
Matthew June 9th, 2011 |
please disregard my last post, it was a simple error using the wrong variable name |
|
carlos July 29th, 2011 |
graciasssssssssssssssss me salvaron la vida solo me flataba |
|
Hari August 21st, 2011 |
I wish to save files >300mb, the script work fine for images but not for heavy movie files. Can u provide a example please. |
|
@Admin your code works like charm. thanlks alot you save me time. |
|
yas September 29th, 2011 |
I'm using PHP cURL to fetch image from another website and insert it into my page. I was wondering if it is possible to have the fetched image cached on my server? For example, when a visitor requests a page, the image is fetched and cached on my server for 24 hours. The image is then entirely served locally for 24 hours. When the 24 hours expire, the image is again fetched and cached when another visitor requests it, in the same way. |
|
This post is useful to me. Could I translate it into Vietnamese ? |
|
Eduard November 16th, 2011 |
Nice cURL script. Works fine but I wanted to add user authentication I added: curl_setopt($ch,CURLOPT_USERPWD,"$username:$password"); but that didn't work. |
|
Hello, |
|
Thanks heaps for this. It's a really handy bit of code. One question, if you have time... It all seems to be working until I go to open the saved file. It's just a blank jpg. Any idea what I might be doing wrong? |
|
A combination of this and Simple HTML DOM Parser would probably be a good idea. |
|
Not sure why, there is no DOM on an image |
|
prabagar February 11th, 2012 |
thanks |
|
Sha March 6th, 2012 |
Great! i used it to run thru a loop and downloaded all the users profile images. Thanks a LOT! |
|
zhiyo March 9th, 2012 |
I also get 0kb files... how do you fix this? Here's my code: $dir = str_replace('//','/',$dir); |
|
zhiyo March 9th, 2012 |
Ok, found the problem... it works with http but not with https... do you have any clue how to skip that problem ? Configuration problem? |
|
Nice tutorial. I hope it will solve my problem. Thanks to share this. |
|
Corey April 10th, 2012 |
For HTTPS support, try adding the following: |
|
Gads, after reading all the "this script works perfectly" comments, I feel like an idiot. All I want to do is copy a file from one folder and put it in another: to copy the file 'tester.jpg' from images to images/test Nothing copied. What am I leaving out? Thanks -- Mike |
RSS Feed
Niki
December 16th, 2008