Welcome Guest, Not a member yet? Register   Sign In
Preserving transparency when creating thumbnails
#1

[eluser]Unknown[/eluser]
Hey Everyone,

Long time lurker, first time poster Wink

I was wondering if I am missing something or if the Image manipulation class doesn't preserve png transparency when resizing and thumbnail and directly outputting it? I just end up with a black background. Using the GD2 library, here is the relevant code, that works fine (except for the transparency bit):
Code:
function index($image, $max_height=null, $max_width = null)
    {
        $filename = 'system/application/backup/photos/'.$image;
        $config['image_library'] = 'GD2';
        $config['source_image'] = $filename;
        $config['width'] = (is_numeric($max_width) && $max_width != 0 ? $max_width : '');
        $config['height'] = (is_numeric($max_height) && $max_height != 0 ? $max_height : '');
        $config['dynamic_output'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        
        $this->load->library('image_lib', $config);
        
        if(!$this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }
    }

Any advice much appreciated.
#2

[eluser]InsiteFX[/eluser]
Hi,

Here is a work around, the problem is in the GD2 library.

Code:
$image = imagecreatefromjpeg('booh.jpg');
list($image_width, $image_height) = getimagesize('booh.jpg');
$water = imagecreatefrompng('text.png');
list($water_width, $water_height) = getimagesize('text.png');

$water_resized_width = $water_width * 0.5;
$water_resized_height = $water_height * 0.5;

$resized = imagecreatetruecolortransparent($water_resized_width, $water_resized_height);
imagecopyresampled($resized, $water, 0, 0, 0, 0, $water_resized_width, $water_resized_height, $water_width, $water_height);

imagecopy($image, $resized, (($image_width - $water_resized_width) / 2), (($image_height - $water_resized_height) / 2), 0, 0, $water_resized_width, $water_resized_height, 15);
imagejpeg($image, 'finished.jpg', 100);
imagedestroy($image);
imagedestroy($water);
imagedestroy($resized);

function imagecreatetruecolortransparent($x, $y)
{
$i = imagecreatetruecolor($x, $y);
$b = imagecreatefromstring(base64_decode(blankpng()));
imagealphablending($i, false);
imagesavealpha($i, true);
imagecopyresized($i, $b ,0 ,0 ,0 ,0 ,$x, $y, imagesx($b), imagesy($b));
return $i;
}

function blankpng()
{
$c = "iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29m";
$c .= "dHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAADqSURBVHjaYvz//z/DYAYAAcTEMMgBQAANegcCBNCg";
$c .= "dyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAAN";
$c .= "egcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQ";
$c .= "oHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAA";
$c .= "DXoHAgTQoHcgQAANegcCBNCgdyBAgAEAMpcDTTQWJVEAAAAASUVORK5CYII=";
return $c;
}

It should also work with png, this is not tested by me.

Enjoy
InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB