downloading remote images [SOLVED]

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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

downloading remote images [SOLVED]

Post by s.dot »

I've a script, which allows users to input images for use in the web page along with simple HTML. However for bandwidth and accessability reasons, I've made it so the script grabs all of the jpg, png, gif images and transfers them to our server. Thus, site users won't see "bandwidth exceeded" or downtime of other 3rd party free image hosting solutions (e.g. photobucket, imageshack, tinypic).

Using GD imagejpeg(), imagegif(), and imagepng() functions, I can save them to the path I want on our server, then replace the image links in the submitted code and all works as expected.

Except for animated .gif's. I didn't really think about it, but I suppose that is still a limitation on the GD imaging software. Only the first frame is written.

The solution I've come up with is to simply grab the contents of the image and write it to a raw file. e.g.

Code: Select all

$file = file_get_contents($imgLink);
$handle = fopen('images/display/' . $newImageName, "w");
fwrite($handle, $file);
fclose($handle);
This feels hackish to me. Or is it the only way of accomplishing the download and storage of animated gifs?

EDIT| Copyright issues shouldn't be a problem. ;) As the images are going to be made of images they've taken, made, or found from open source directories or royalty free imagery web sites.
Last edited by s.dot on Mon Oct 01, 2007 4:36 am, edited 1 time in total.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Using GD imagejpeg(), imagegif(), and imagepng() functions, I can save them to the path I want on our server
What for do you need the gd functions?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Well, I have been using:

Code: Select all

if ($img_info = @getimagesize($imageurl))
{
	$img_name = time() . substr(md5(microtime()), 0, mt_rand(5, 10));
	
	switch($img_info[2])
	{
		case '1':
		$img_name .=  '.gif';
		imagegif(imagecreatefromgif($imageurl), '/home/usr1/public_html/img/' . $img_name);
		break;
						
		case '2':
		$img_name .= '.jpg';
		imagejpeg(imagecreatefromjpeg($imageurl), '/home/usr1/public_html/img/' . $img_name);
		break;
						
		case '3':
		$img_name .= '.png';
		imagepng(imagecreatefrompng($imageurl), '/home/usr1/public_html/img/' . $img_name);
		break;
	}
	
	$replaced[] = $img_name;
}
I suppose I'm just throwing in extra gd function calls when I could just be fwrite()ing them all?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd simply call file_put_contents() with file_get_contents()'s results.
Post Reply