resize image of type gif nake it black

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
pelegk1
Forum Newbie
Posts: 9
Joined: Tue Aug 31, 2004 12:30 am

resize image of type gif nake it black

Post by pelegk1 »

when i resize a gif i recive a black image
why is that?
this is the code:

Code: Select all

header('Content-type: image/gif');
$thumb = ImageCreateTrueColor( $new_w, $new_h );
$source = imagecreatefromgif($input_file);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $new_w, $new_h, $width, $height);
imagegif($thumb, $new_name . '.gif',100);
thnaks in advance
peleg
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

.gif are palette images so you should use imagecreate() and not imagecreatetruecolor(). If you are concerned about transparancy you will need to allocate the transparancy colour of the source to your drawing slate before copying it. Something like the following should cover most eventualities:

Code: Select all

function resize_gif($source, $destination = null, $longside = 150)
{
	$w = $h = $longside;
	$source  = imagecreatefromgif($source);
	if(($x = imagesx($source)) < ($y = imagesy($source)))
	{
		$w = round(($h / $y) * $x);
	}
	else
	{
		$h = round(($w / $x) * $y);
	}
	$slate = @imagecreate($w, $h) or die('Invalid thumbnail dimmensions');
	if(false !== ($trans = @imagecolorsforindex($source, imagecolortransparent($source))))
	{
		$trans = ImageColorAllocate($slate, $trans['red'], $trans['green'], $trans['blue']);
		imagefill($slate, 0, 0, $trans);
		imagecolortransparent($slate, $trans);
	}
	imagecopyresampled($slate, $source, 0, 0, 0, 0, $w, $h, $x, $y);
	$destination or header('Content-Type: image/gif');
	$destination ? imagegif($slate, $destination) : imagegif($slate);
	imagedestroy($source);
	imagedestroy($slate);
	$destination or die;
}
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

and what if the gif is animgif

Post by pelegk2 »

will it work too?
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Re: and what if the gif is animgif

Post by bokehman »

pelegk2 wrote:will it work too?
At present PHP has no built in support for animated gifs although there are hacks available.
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

what do u mean by :

Post by pelegk2 »

although there are hacks available
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Like this one for example but of course you would need to be in control of the server to install such a thing.
Post Reply