tags:

views:

23

answers:

1

Hi,

I want to get images ID's and creat from files a merged image according to the given ID's. This code is called by ajax and return the image file name (which is the server time to prevent browser caching). code:

if (isset($_REQUEST['items'])){
$req_items = $_REQUEST['items'];
} else {
    $req_items = 'a';
}
$items = explode(',',$req_items);

$bg_img = imagecreatefrompng('bg.png');

for ($i=0; $i<count($items); $i++){

$main_img = $items[$i].'-large.png';

$image      = imagecreatefrompng($main_img);

$image_tc = imagecreatetruecolor(300, 200);
imagecopy($image_tc,$image,0,0,0,0,300,200);
$black = imagecolorallocate($image_tc, 0, 0, 0);
imagecolortransparent($image_tc, $black);

$opacity        = 100;  
$bg_width   = 300;  
$bg_height  = 200;  

$dest_x         = 0;//$image_size[0] - $bg_width - $padding;  
$dest_y         = 0;//$image_size[1] - $bg_height - $padding;

imagecopymerge($bg_img, $image_tc, $dest_x, $dest_y, 0, 0, $bg_width, $bg_height, $opacity)

;

}
$file = $_SERVER['REQUEST_TIME'].'.jpg';
imagejpeg($bg_img, $file, 100);
echo $file;
imagedestroy($bg_img);
imagedestroy($image);
die();

The images are shown exactly as I want but with wrong colors. I lately added the part with imagecreatetruecolor and imagecolortransparent, and still got wrong results.

I also saved the PNG itself on a 24 bit format and also later as 8 bit - not helping. every ideas is very welcomed ! Thanks

+1  A: 

After long time of trying... as always the solution was very simple:

Just make the background image a 24 bit as well. So if someone is looking for a way to make layered transparent images this is the complete code:

<?php  

if (isset($_REQUEST['items'])){
    $req_items = $_REQUEST['items'];
} else {
    $req_items = 'a';
}

$items = explode(',',$req_items);

$bg_img = imagecreatefrompng('bg.png');
$bg_tc = imagecreatetruecolor(300, 200);
imagecopy($bg_tc,$bg_img,0,0,0,0,300,200);


for ($i=0; $i<count($items); $i++){

    $main_img = $items[$i].'-large.png';

    $image      = imagecreatefrompng($main_img);
    $image_tc = imagecreatetruecolor(300, 200);



    imagecopy($image_tc,$image,0,0,0,0,300,200);
    $black = imagecolorallocate($image_tc, 0, 0, 0);
    imagecolortransparent($image_tc, $black);


    $opacity        = 100;  
    $bg_width   = 300;  
    $bg_height  = 200;  

    $dest_x         = 0;//$image_size[0] - $bg_width - $padding;  
    $dest_y         = 0;//$image_size[1] - $bg_height - $padding;

    imagecopymerge($bg_tc, $image_tc, $dest_x, $dest_y, 0, 0, $bg_width, $bg_height, $opacity);

}
$file = $_SERVER['REQUEST_TIME'].'.jpg';
imagejpeg($bg_tc, $file, 100);
echo $file;
imagedestroy($image);
imagedestroy($bg_img);
imagedestroy($bg_tc);
imagedestroy($image_tc);
die();
?>
OfficeJet