Showing Or Saving An Image Retrieved Via cURL
As a developer cURL can be a very handy for numerous reasons. In my case I have used it for my plugin Twitter Stream, but here we are going to look at a little tip I stumbled across while playing with cURL.
cURL is used to make a connection to an external source to retrieve the content at the source. What most people new to cURL don’t realize is that it can be used to retrieve any content, including images.
Display An Image In Browser After Retrieval Via cURL
Let’s take a look at an example of displaying a image in browser after using cURL to grab it.
//The cURL stuff...
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com/randomimage.jpg');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$picture = curl_exec($ch);
curl_close($ch);
//Display the image in the browser
header('Content-type: image/jpeg');
echo $picture;
Yes, believe it or not it is actually that simple. Unfortunately I can’t provide an example any more, but it does work… Honest. Scouts honour and everything.
Let’s also go through what is going on a little. First we initialize cURL, we set the URL, then we tell it we don’t want the header information. We tell it we want to return the information, and we also state we want the transfer to be binary. We then execute it & store the result in a variable, and close/destroy the cURL connection/handler.
Finally we tell the browser that the content we are going to hand it is a jpeg image. You could of course use a MIME type grabbed from the image. Then we echo the binary information inside the variable, that is then interpreted as an image thanks to the content type.
Saving An Image Retrieved Via cURL
Let’s take a look at how to save the image instead of sending it to the browser.
//cURL code is exactly the same as before...
//Save image...
$fh = fopen('filename.jpg', 'x');
fwrite($fh, $picture);
fclose($fh);
A little more complicated that the previous example, but if you are familiar with PHP file operations it’s still very simple. I can’t really give an example for this one, as you won’t be able to see the result. Trust me though, it works.
Let’s go through this one a little too. The cURL section is exactly the same as before so we don’t need to cover that. First we use fopen to create a file handler & tell PHP what file to write too. x tells PHP to create the file if it doesn’t exist, but to error out if it does already exist. Then we use fwrite to write the raw binary data held in the variable $picture to the file. We then close & destroy the file handler by using fclose.
Possible Uses
One of the main reasons to use cURL to save or display a remote image is that some servers/hosts have allow_url_fopen turned off. This means functions like fopen can’t open files held on external servers.
You could go into the whole copyright issue of displaying other people’s images etc, but I’m not going to get started on my thoughts about that. Just be careful with this kind of script & use it with care.
I hope you have enjoyed this tutorial. If you have any questions or comments please don’t hesitate to leave them below. You can follow me on Twitter, or you can ‘like’ Return True’s Facebook page.

Discussion: 10 Comments » add a comment
Thanks for the tutorial! It’s the only functional one I found
Just one question tho, is there a way to add variables for example
curl_setopt($ch, CURLOPT_URL, ‘http://img.youtube.com/vi/.echo $out['something'].’);
Indeed there is. You had it right, you just don’t need to add the echo since you don’t want to output to the browser.
So like this:
That shoul work.
Hi,
The first will indeed add speed, if your server supports GZip via cURL & the server you are asking for the data from supports GZip too.
The second stops cURL from sending back the HTTP header in it’s response, if you set this to 1 then tried to output the data as a image it would most likely be corrupt since the HTTP headers would be attached to the front of the image data. In most cases it’s already set to 0 (false), but I set it just in case.
The last is to make sure you are get the raw image data returned, this allows us to save it to file or echo it by outputting a header containing the correct MIME type & show the image.
Hope that helps answer your questions.
any way to make a function or loop? It can return text but fails on images.
function myurl($url){ $ch = curl_init(); $timeout = 5; // set to zero for no timeout curl_setopt($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $page = curl_exec($ch); header('Content-type: image/jpeg'); if(curl_exec($ch) === false) { echo 'Curl error: ' . curl_error($ch); } else { echo 'cURL Operation completed without any errors'; } curl_close($ch); return $page; } myurl('ht tp://kayakidaho.com/images/snotel/wsr.jpg');Admin Edit: I added a space in the url above that shouldn’t be there to stop WordPress auto parsing the URL. It’s being a pain…
Hi Grantiago,
Does it work outside the function? Also are you getting an error back from cURL or does it just not produce anything?
The main thing I can see is that you are returning the value. That will do nothing since the function is trying to pass it back to a variable. You’d need either:
echo myurl('url/here.jpg');or
$img = myurl('url/here.jpg'); echo $img;The only advantage the second one has is the ability to manipulate the data in the variable before echoing the data.
You could always change the
returnin your function to anechoof course and that should also fix the problem.Hope that helps.
Thanks for the response. I put the echo statement in the function and removed the if statement.
header('Content-type: image/jpeg'); echo $page; curl_close($ch);Finally! I appreciate the input.
No problem.
Happy you managed to get things working.
I tried the code above, I can manage to display the picture in the browser but I’m not able to save it.. what could be the problem?
Hi Zeeshan,
There are a few common problems. The most common of them all is insufficient access privileges in the folder you are trying to save in. Try to chmod the folder to 755.
If that doesn’t work your host may have disabled fopen completely, which is unusual but possible.
As an alternative you could try changing the whole save section to:
file_put_contents('file.jpg', $picture);Hopefully one of those will help.