Combining images in PHP while retaining transparen

2020-05-03 13:34发布

问题:

Have a look here: http://tyilo.jbusers.com/PNG/progress.php?l=100&p=20

I want to remove the white-thingy at the end of the blue part, but I have tried many different things that didn't work.

If needed the pngs can be found in http://tyilo.jbusers.com/PNG/ folder (http://tyilo.jbusers.com/PNG/Empty.png)

header('Content-type: image/png');
echo imagepng(progressbar($_GET['l'], $_GET['p']));
function progressbar($length, $percentage)
{
$length = round($length / 2) * 2;
$percentage = min(100, max(0, $percentage));
if($length > 0)
{
    $bar = imagecreate($length, 14);
    $empty = imagecreatefrompng('Empty.png');
    $fill = imagecreatefrompng('Fill.png');
    $lempty = imagecreatefrompng('LeftEmpty.png');
    $lfill = imagecreatefrompng('LeftFill.png');
    $rempty = imagecreatefrompng('RightEmpty.png');
    $rfill = imagecreatefrompng('RightFill.png');
    $emptycaplength = min(7, $length / 2); //5 
    imagecopy($bar, $lempty, 0, 0, 0, 0, $emptycaplength, 14);
    imagecopy($bar, $rempty, $length - $emptycaplength, 0, 7 - $emptycaplength, 0, $emptycaplength, 14);
    if($length > 14)
    {
        imagecopyresized($bar, $empty, 7, 0, 0, 0, $length - 14, 14, 1, 14);
    }
    $filllength = round(($length * ($percentage / 100)) / 2) * 2;
    $fillcaplength = min(7, $filllength / 2);
    imagecopy($bar, $lfill, 0, 0, 0, 0, $fillcaplength, 14);
    imagecopy($bar, $rfill, $filllength - $fillcaplength, 0, 7 - $fillcaplength, 0, $fillcaplength, 14);
    if($filllength > 14)
    {
        imagecopyresized($bar, $fill, 7, 0, 0, 0, $filllength - 14, 14, 1, 14);
    }
    return $bar;
}
else
{
    return false;
}
}

回答1:

try using imagecreatetruecolor() to create your image.

http://www.php.net/manual/en/function.imagecreatetruecolor.php



回答2:

I think you need to set the Alpha blend of the image...

 imagealphablending($cropimg, false);
 imagesavealpha($cropimg, true);

I've also found that it helps to set the Color Allocation.

imageColorAllocate ($cropimg, 0, 0, 0);

I think you will need to call those three functions on all your filled images. Sorry for the incomplete answer, I'm in a rush but thought I'd throw you a bone.