I have created a script which takes x amount of images and places them on top of each other and then flattens them and saves the image out as an png. The problem I am having is that the first image is supposed to be transparent and is displaying black. The first image is actually a transparent png to start with too.
See Example http://www.e2save.com/index_gw.php
I have set imagealphablending to false but still it turns it black.
//-------------- My Code File (sorry rather long) --------------------------
class combiImage
{
var $width;
var $height;
var $images;
var $image;
function combiImage($width, $height)
{
$this->width = $width;
$this->height = $height;
$this->images = array();
$this->image = false;
}
function addImage($img, $x=0, $y=0)
{
$img = new Image($img, $x, $y);
if(!$img->image)
{
//printR($img);
//printR("Error loading file ".$img);
return false;
}
if(!count($this->images))
$img->resizeTo($this->width, $this->height);
$this->images[] = $img;
return count($this->images)-1;
}
function flatten()
{
$this->image = $this->images[0];
for($i=1; $i<count($this->images); $i++)
$this->image->image = $this->combine($this->image, $this->images[$i]);
}
function combine($bgImg, $fgImg)
{
$tmp = imagecreatetruecolor($bgImg->width, $bgImg->height);
if(!imagealphablending($tmp, false)) return false;
if(!imagesavealpha($tmp, true)) return false;
if(!imagecopyresampled($tmp, $bgImg->image, 0, 0, 0, 0, $bgImg->width, $bgImg->height, $bgImg->width, $bgImg->height)) return false;
//Work out position
$fgX = ($bgImg->width - $fgImg->width) * $fgImg->destX;
$fgY = ($bgImg->height - $fgImg->height) * $fgImg->destY;
if(!imagealphablending($tmp, true)) return false;
if($fgImg->alpha != 1)
{
$tmp2 = imagecreatetruecolor($fgImg->width, $fgImg->height);
for($y=0; $y<$fgImg->height; $y++)
{
for($x=0; $x<$fgImg->width; $x++)
{
$col = imagecolorat($fgImg->image, $x, $y);
$col = imagecolorsforindex($fgImg->image, $col);
$col["alpha"] = min(($col["alpha"] + (127*$fgImg->alpha)), 127);
$col = imagecolorallocatealpha($tmp, $col["red"], $col["green"], $col["blue"], $col["alpha"]);
imagesetpixel($tmp, $fgX+$x, $fgY+$y, $col);
}
}
}
else
{
imagecopy($tmp, $fgImg->image, $fgX, $fgY, 0, 0, $fgImg->width, $fgImg->height);
}
imagealphablending($tmp, false);
imagesavealpha($tmp, true);
return $tmp;
}
function saveToFile($type, $name, $colours=256, $quality=50)
{
//Don't dither gifs
if($type == "gif")
imagetruecolortopalette2($this->image, false, $colours);
if($type == "png")
{
//Don't save alpha channel
imagesavealpha($this->image->image, false);
//Decrease colour depth (reduces file size significantly)
imagetruecolortopalette2($this->image, false, $colours);
}
$func = "image".$type;
if($type == "jpeg")
{
if($name) $func($this->image->image, $name, $quality);
else $func($this->image->image, '', $quality);
}
else
{
if($name) $func($this->image->image, $name);
else $func($this->image->image);
}
}
function output($type)
{
$this->saveToFile($type, false);
}
function destroy()
{
for($i=0; $i<count($this->images); $i++)
imagedestroy($this->image->image);
imagedestroy($this->image);
}
}
//-----------------------------------------------------------------------