Copy Image from Remote Server Over Http

Copy Image from Remote Server Over HTTP

If you have PHP5 and the HTTP stream wrapper enabled on your server, it's incredibly simple to copy it to a local file:

copy('http://somedomain.com/file.jpeg', '/tmp/file.jpeg');

This will take care of any pipelining etc. that's needed. If you need to provide some HTTP parameters there is a third 'stream context' parameter you can provide.

How to copy images from a remote server to a local machine in Python

Can be done using numerous ways. I prefer using request module as u mentioned that images are accessible over http.

import requests
img = open('ur_file_name.jpg', 'wb')
img.write(requests.get('http://url_of_image_in_remote_server.jpg').content)
img.close()

This will save the image in current working directory (cwd). If u wish to save images in folder other than cwd then refer to the python's os.path documentation.

PS: If the number of images are very high and u r hitting someone elses server, which u dont own, then its advisable to not flood the server with too many requests in short span of time. Its not encouraged (at least I don't).

Preferred way to copy JPG files from a remote server using PHP

if all you want is a copy, copy() is better.

using the gd library functions (imagecreatefromjpeg/imagejpeg) will end up re-compressing the image (probably, maybe it's smart enough not to, but probably). If you wanted to convert the images to .png or something, then you'd want to use gd (or ImageMagick)

Unable to save an image from a remote server using PHP

It's because your URL get interpreted wrong. Always use {} around variable in string:

copy("https://$_SSActiveWear_BaseURL/$image_URL", $_SERVER['DOCUMENT_ROOT']."/tmp/" . basename($image_URL));

is converted to copy("https://SSActiveWear_BaseUrl/_URL", ...). As you can see PHP does not find variables $_ and $image and applies null for them.

Correct syntax:

copy("https://{$_SSActiveWear_BaseURL}/{$image_URL}", "{$_SERVER['DOCUMENT_ROOT']}/tmp/".basename($image_URL));


Related Topics



Leave a reply



Submit