Hello,
I need to read an image from a remote url, and then save it on my hosting webspace.
This is the image that I’d like to save (it’s a webcam test): http://remoteimage.ddns.info:17000/snapshot.cgi
As you can see, if you put the image url in a browser then you can see the image properly. But if I try to save the image by using a php script, nothing happens. I’ve tried three different php scripts, but none of them works.
Some more notes:
- php version 5.3.29
- “allow_url_fopen” is set to “on”,
- if I use a different image url (eg: google.com/images/srpr/logo11w.png), all the scripts work fine.
Can anyone help me on how to save this remote image with a php script?
Here are the three scripts that I’ve tested so far with no result.
Script 1 - using file_get_contents:
$remoteUrl = "http://remoteimage.ddns.info:17000/snapshot.cgi";
$image = file_get_contents($remoteUrl);
$fileName = "captured-image.jpg";
file_put_contents($fileName, $image);
With the above script I get this warning: file_get_contents(url): failed to open stream: Connection refused on line 2
Script 2 – using GD functions:
$remoteUrl = "http://remoteimage.ddns.info:17000/snapshot.cgi";
$image = imagecreatefromjpeg($remoteUrl);
$fileName = "captured-image.jpg";
$quality = 90;
imagejpeg($image, $fileName, $quality);
With the above script I get again the same warning: file_get_contents(url): failed to open stream: Connection refused on line 2
Script 3 – using curl:
$remoteUrl = “http://remoteimage.ddns.info:17000/snapshot.cgi”;
[code]$ch = curl_init();
$timeout = 20;
curl_setopt ($ch, CURLOPT_URL, $remoteUrl);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$image = curl_exec($ch);
curl_close($ch);
$fileName = “captured-image.jpg”;
file_put_contents($fileName, $image);[/code]
In this case I get no warning, but in the end I get an empty image file.