Page 1 of 1

downloading remote images [SOLVED]

Posted: Mon Oct 01, 2007 3:11 am
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.

Posted: Mon Oct 01, 2007 3:19 am
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?

Posted: Mon Oct 01, 2007 3:26 am
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?

Posted: Mon Oct 01, 2007 9:18 am
by feyd
I'd simply call file_put_contents() with file_get_contents()'s results.