Capture image from a website and saving it to my own server.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
edliu
Forum Newbie
Posts: 2
Joined: Sat Jun 24, 2006 10:08 pm

Capture image from a website and saving it to my own server.

Post by edliu »

Hi Guys,

I have a small problem here. I have the address of the image on the web such as "<img src = "http://abc.com/testing4jh3e.jpg".

I managed to extract the address of the image src as it is constantly changing. However, I wish to be able to download the image using php in to my web server root directoy so that I can use php again to process the image such as resize etc...I tried file functions an image functions but due to the fact that I am still a newbie, I am not able to comprehend how it works...Thus requesting advice from you guys.....

Thanks a lot! :cry:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

provided you have permission to pull the image from their server, file_get_contents() can often be used to retrieve the file's binary data. file_put_contents() or a combination of fopen()-fwrite()-close() can write the file to your server.
edliu
Forum Newbie
Posts: 2
Joined: Sat Jun 24, 2006 10:08 pm

Post by edliu »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


[quote="feyd"]provided you have permission to pull the image from their server, [url=http://php.net/function.file_get_contents]file_get_contents()[/url] can often be used to retrieve the file's binary data. [url=http://php.net/function.file_put_contents]file_put_contents()[/url] or a combination of [url=http://php.net/function.fopen]fopen()[/url]-[url=http://php.net/function.fwrite]fwrite()[/url]-close() can write the file to your server.[/quote]


Can you provide me an example? Thanks. However, here is my code:

Code: Select all

/*=========================*/

$onlyimage = 'http://www.onemotoring.com.sg/trafficsmart/images/2701_1412_20060625141115_f01bb5.jpg"

$somecontent= file_get_contents($onlyimage);  

if (fwrite("/home/edmundlx/pics/traffic/abc.jpg", $somecontent) === FALSE) {
       echo "Cannot write to file ($filename)";
       exit;
}
/*=============================*/
It gives me error....cannot write to file..
Pls guide me .Thanks


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

fwrite isn't properly written...
You have to give it a resource handle: fwrite ( resource handle, string string).

Code: Select all

//...
$handle = fopen("/home/edmundlx/pics/traffic/abc.jpg");
if (fwrite($handle, $somecontent) === FALSE) {
  echo "Cannot write to file ("/home/edmundlx/pics/traffic/abc.jpg")";
  exit;
}
Post Reply