vendredi 29 mai 2015

Resize Image Canvas with CodeIgniter Image Library - how to preserve transparency

I am trying to resize image canvas (as in Photoshop) by adding transparency around it. Somehow added part of the image is always black.

if ($this->image_library == 'gd2' AND function_exists('imagecreatetruecolor'))
{
     $create = 'imagecreatetruecolor';
     $copy = 'imagecopyresampled';
}
else
{
     $create = 'imagecreate';
     $copy    = 'imagecopyresized';
}

$dst_img = $create($this->width, $this->height);

if ($this->image_type == 3) // png we can actually preserve transparency
{
    //teorethicaly image should be transparent?
    $trans_colour = imagecolorallocatealpha($dst_img, 0, 0 ,0, 127); 
    imagefill($dst_img, 0, 0, $trans_colour);
    imagealphablending($dst_img, FALSE);
    imagesavealpha($dst_img, TRUE);
}

$copy($dst_img, $src_img, 0, 0, $this->x_axis, $this->y_axis, $this->width, $this->height, $this->orig_width, $this->orig_height);

If I remove $copy and save new image only, it is transparent but if I merge both images the background is always black:

enter image description here

How I can have transparent background in that situation?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire