Showing Or Saving An Image Retrieved Via cURL

/ PHP / by Paul Robinson / 17 Comments
This post was published back on May 11, 2010 and may be outdated. Please use caution when following older tutorials or using older code. After reading be sure to check for newer procedures or updates to code.

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.

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.

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.

17 Comments

Author’s gravatar

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’].’);

Reply
Author’s gravatar author

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. 😉

Author’s gravatar

Reply
Author’s gravatar author

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.

Author’s gravatar

any way to make a function or loop? It can return text but fails on images.

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…

Reply
Author’s gravatar author

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:

or

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 return in your function to an echo of course and that should also fix the problem.

Hope that helps.

Author’s gravatar

Thanks for the response. I put the echo statement in the function and removed the if statement.

Finally! I appreciate the input.

Reply
Author’s gravatar author

No problem.

Happy you managed to get things working. 😉

Author’s gravatar

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?

Reply
Author’s gravatar author

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:

Hopefully one of those will help. 🙂

Author’s gravatar

I would suggest to check the response code and the content-type received from the server request (useful to avoid possible errors in further image manipulation (resizing, cropping..):

Reply
Author’s gravatar

I’ve tried the code, but unfortunately get a 504 gateway timeout. I’ve tried various curl scripts as well with the same result.

I can access my IP camera from outside my network (ie port forwarding etc is working) and the URL of snapshot.cgi is correct for my brand of IP camera. I can use curl to access images on other websites too.. Hmm

Any suggestions about what’s going wrong would be appreciated!
Cheers,
Simon

Reply
Author’s gravatar author

Hi Simon,

Sadly I haven’t a clue, a 504 usually means exactly what it says; The source couldn’t be contacted in a reasonable amount of time. You could try increasing the timeout setting using:

If that doesn’t work you could try using the Shell version of curl if you have Shell available. It is generally the best way to see if it is a problem with curl in general instead of just the connection PHP has with curl.

Author’s gravatar

this appears to be working for me… but when i view the saved image in my browser, i just get a little square nothing ?

Reply
Author’s gravatar author

Hi,

That would normally mean it isn’t really saving properly. Try adding:

This will add verbose output to your browser, regardless of your output settings and hopefully will give you some idea of why it isn’t saving properly.

Author’s gravatar

Can you let me know, how to show the image if I get the data in ajax response. I don’t want to save the image but want to show directly using the ajax response?

Reply
Older Comments
Newer Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

I'll keep your WordPress site up-to-date and working to its best.

Find out more