TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Caching Remote Images (http://www.talkphp.com/advanced-php-programming/1669-caching-remote-images.html)

aristoworks 12-08-2007 01:59 PM

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!

Wildhoney 12-08-2007 02:24 PM

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);

Jay 12-08-2007 02:25 PM

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 ^^

Wildhoney 12-08-2007 03:01 PM

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?

Jay 12-08-2007 03:13 PM

Yea, there are definately a lot of aliases in PHP

count and sizeof are identical :-)

Karl 12-08-2007 03:18 PM

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.

aristoworks 12-08-2007 03:18 PM

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

Thanks
jw

aristoworks 12-08-2007 03:24 PM

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

Karl 12-08-2007 03:40 PM

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.

Jay 12-08-2007 03:43 PM

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 ^^

Salathe 12-08-2007 04:58 PM

Quote:

Originally Posted by aristoworks (Post 5929)
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)
 


All times are GMT. The time now is 03:03 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0