在保持透明度的同时在PHP中组合图像

时间:2011-03-07 20:26:12

标签: php png transparency gd

看看这里:http://tyilo.jbusers.com/PNG/progress.php?l=100&p=20

我想在蓝色部分的末尾删除白色东西,但我尝试了许多不起作用的不同东西。

如果需要,可在http://tyilo.jbusers.com/PNG/文件夹(http://tyilo.jbusers.com/PNG/Empty.png)中找到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;
}
}

2 个答案:

答案 0 :(得分:1)

尝试使用imagecreatetruecolor()来创建图像。

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

答案 1 :(得分:0)

我认为您需要设置图像的Alpha混合...

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

我还发现设置颜色分配很有帮助。

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

我认为您需要在所有填充的图像上调用这三个函数。对不完整的答案很抱歉,我很匆忙,但我想我会给你一块骨头。

相关问题