|
GD image resize transparent gif
hey i was wondering if anyone knew the best way to resample a transparent gif using PHP/GD library. Currently this is the closest i've got:
PHP Code:
$imgSource = imagecreatefromgif('source.gif');
$imgDestination3 = imagecreatetruecolor(90, 90);
$imgDestination4 = imagecreatetruecolor(90, 90);
$black = imagecolorallocate($imgDestination3, 0, 0, 0);
$white = imagecolorallocate($imgDestination4, 255, 255, 255);
imagefill($imgDestination3, 0, 0, $black);
imagefill($imgDestination4, 0, 0, $white);
imagecopyresampled($imgDestination3, $imgSource, 0, 0, 0, 0, 90, 90, $intImage_Width, $intImage_Height);
imagecopyresampled($imgDestination4, $imgSource, 0, 0, 0, 0, 90, 90, $intImage_Width, $intImage_Height);
$intImage_Width = 90;
$intImage_Height = 90;
$arrColors = array();
for($i=0; $i<$intImage_Width; $i++)
{
for($j=0; $j<$intImage_Height; $j++)
{
$pos1 = imagecolorat($imgDestination3, $i, $j);
$color1 = imagecolorsforindex($imgDestination3, $pos1);
$pos2 = imagecolorat($imgDestination4, $i, $j);
$color2 = imagecolorsforindex($imgDestination4, $pos2);
if (($color1['red'] == $color2['red']) && ($color1['green'] == $color2['green']) && ($color1['blue'] == $color2['blue']))
{
$arrColors[count($arrColors)] = $color1['red'].$color1['green'].$color1['blue'];
}
}
}
$red = rand(0,255);
$green = rand(0,255);
$blue = rand(0,255);
$i = 0;
while ($i < count($arrColors))
{
if ($arrColors[$i] == $red.$green.$blue)
{
$red = rand(0,255);
$green = rand(0,255);
$blue = rand(0,255);
$i = 0;
}
$i++;
}
$imgDestination5 = imagecreatetruecolor(90, 90);
$transparent = imagecolorallocate($imgDestination5, $red, $green, $blue);
imagefill($imgDestination5, 0, 0, $transparent);
imagecolortransparent($imgDestination5, $transparent);
$intImage_Width = imagesx($imgSource);
$intImage_Height = imagesy($imgSource);
imagecopyresampled($imgDestination5, $imgSource, 0, 0, 0, 0, 90, 90, $intImage_Width, $intImage_Height);
imagetruecolortopalette($imgDestination5, false, 256);
imagegif($imgDestination5, 'thumbnail.gif');
basically the plan with this was to find the colors in the resized image that were opaque and then just generate some random color to use as the transparency. when i convert it to a palette image some of the transparent color goes transparent but i get weird spots of color appearing:
i'm out of ideas  lol anybody else got any? 
|