.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;
}