TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-08-2007, 01:59 PM   #1 (permalink)
The Contributor
 
aristoworks's Avatar
 
Join Date: Nov 2007
Location: Nashville
Posts: 44
Thanks: 7
aristoworks is on a distinguished road
Default Caching Remote Images

I've never seen or heard of anything like this but I'm confident that it's been done many times over.

I'm working on an application whereas users can reference their own remote image say.. as an avatar.

Outside of accessing a tool like "wget" is there a way natively for php to grab a remote image and download it to the server for archiving?

<Yes I'm aware of copyright implications but I'm looking for a methodical answer>

Thanks!
Send a message via AIM to aristoworks
aristoworks is offline  
Reply With Quote
Old 12-08-2007, 02:24 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

This bit of code will do it for you:

php Code:
$szSourceImage = 'http://www.google.com/intl/en_ALL/images/logo.gif';
$szTargetImage = 'myImage';
$szData = file_get_contents($szSourceImage);
$szExtension = end(explode('.', $szSourceImage));
file_put_contents($szTargetImage . '.' . $szExtension, $szData);
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
aristoworks (12-08-2007)
Old 12-08-2007, 02:25 PM   #3 (permalink)
Jay
The Contributor
Good Samaritan 
 
Join Date: Dec 2007
Posts: 60
Thanks: 5
Jay is on a distinguished road
Default

You can open (I'm pretty sure, at least) the image location with file_get_contents, and save it with fopen and fwrite:

PHP Code:
$file fopen'./cache/avatars/'$imagename .'.'$image_extension'a+' );
fwrite$filefile_get_contents$image_location );
fclose$file ); 
Edit - It seems WildHoney posted a more advanced one before me
Jay is offline  
Reply With Quote
The Following User Says Thank You to Jay For This Useful Post:
aristoworks (12-08-2007)
Old 12-08-2007, 03:01 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Who knows why PHP added the file_put_contents and file_get_contents, they'd just be wrappers for the fopen way. Seem a little peculiar to me having many ways to do such a simple task. It's like PHP and it's key_exists and array_key_exists - are they not both exactly the same?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-08-2007, 03:13 PM   #5 (permalink)
Jay
The Contributor
Good Samaritan 
 
Join Date: Dec 2007
Posts: 60
Thanks: 5
Jay is on a distinguished road
Default

Yea, there are definately a lot of aliases in PHP

count and sizeof are identical
Jay is offline  
Reply With Quote
Old 12-08-2007, 03:18 PM   #6 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

There is one difference, quite important too. By default, with file_get_contents there is no limit on the buffer, if you tell PHP to "file_get_contents" a 50 MB file, it will, or it'll try anyway. As of PHP 5.1.0 you can change this behavior using the additional parameters set in the function call.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 12-08-2007, 03:18 PM   #7 (permalink)
The Contributor
 
aristoworks's Avatar
 
Join Date: Nov 2007
Location: Nashville
Posts: 44
Thanks: 7
aristoworks is on a distinguished road
Default

dang - that's easy enough. Didn't even think of using that but I'm sure it'll work.

Thanks
jw
Send a message via AIM to aristoworks
aristoworks is offline  
Reply With Quote
Old 12-08-2007, 03:24 PM   #8 (permalink)
The Contributor
 
aristoworks's Avatar
 
Join Date: Nov 2007
Location: Nashville
Posts: 44
Thanks: 7
aristoworks is on a distinguished road
Default

This might negate its own thread but... I'm interested in creating some kind of "hash" based on each image so that I'm not storing the same image in the database 100 times.

Any ideas on taking some "part" of the image and running through md5 to get a unique string? Obviously using the filename won't work because the same image might be named 10 different things.

I'm thinking maybe the "contents" of the image?

Thanks
Send a message via AIM to aristoworks
aristoworks is offline  
Reply With Quote
Old 12-08-2007, 03:40 PM   #9 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

You could try using crc32, although I'm not sure how reliable that would be. In reality though, would it really matter if it isn't 100% accurate? In your situation, the worse to expect is duplicate images.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 12-08-2007, 03:43 PM   #10 (permalink)
Jay
The Contributor
Good Samaritan 
 
Join Date: Dec 2007
Posts: 60
Thanks: 5
Jay is on a distinguished road
Default

Does it really matter? You don't have to cache the images, if you do, you're only going to waste your own bandwidth

Granted an image from another host may take a while to load, and potentially slow down the page.. (most of the it shouldn't for avatars..)
It's not going to affect your server in any way, because it's at a remote location. Also it's going to use less of your precious bandwidth because it will be loading from a different host. Where as if you cache'd it, it will always load from you, causing more stress on bandwidth
Jay is offline  
Reply With Quote
Old 12-08-2007, 04:58 PM   #11 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by aristoworks View Post
Any ideas on taking some "part" of the image and running through md5 to get a unique string?
You can just hash the whole file contents, MD5 will be just fine for that.

php Code:
// Overly simple example of hashing a remote file
$file = file_get_contents('http://domain.com/file.jpg');
$hash = md5($file); // or, hash('md5', $file)
 
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 10:32 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design