检查图像是否为白色;如果这样做透明

时间:2011-10-15 12:03:21

标签: php transparency gd

我有一个图像,如果它是完全白色的话我想让它变得透明。我已经为GD获得了以下代码来获取图像的一部分:

$srcp = imagecreatefrompng("../images/".$_GET['b'].".png");
$destp = imagecreate(150, 150);
imagecopyresampled($destp, $srcp, 0, 0, 40, 8, 150, 150, 8, 8);
header('Content-type: image/png');
imagepng($destp);

但是我该怎样才能首先检查图像是否完全是白色,如果是,则将其更改为透明,并将其应用于$destp

3 个答案:

答案 0 :(得分:4)

修改

基于重新阅读问题以及下面的讨论,我相信这正是您所寻找的:

$width = 150;
$height = 150;

$srcp = imagecreatefrompng("../images/".$_GET['b'].".png");
$destp = imagecreatetruecolor(150, 150);

$white = 0;

for ($y = 0; $y < $height; $y++)
{
    for ($x = 0; $x < $width; $x++)
    {
        $currentColor = imagecolorat($srcp, $x, $y);
        $colorParts = imagecolorsforindex($srcp, $currentColor);

        if (($colorParts['red'] == 255 &&
            $colorParts['green'] == 255 &&
            $colorParts['blue'] == 255))
        {
            $white++;
        }
    }
}

if ($white == ($width * $height))
{
    imagecopyresampled($destp, $srcp, 0, 0, 40, 8, 150, 150, 8, 8);
}
else
{
    imagesavealpha($destp, true);
    imagefill($destp, 0, 0, imagecolorallocatealpha($destp, 0, 0, 0, 127));
}

header('Content-type: image/png');
imagepng($destp);

如果原始图像的8x8切片全部为白色,则会生成空白图像,否则会将8x8切片重新采样为150x150。


<强>原始

我以前从未对PHP GD做过任何事情,并认为这将是一个有趣的挑战。以下是我最终实现这一目标的方式:

$filename = 'test.png'; // input image
$image = imagecreatefrompng($filename);

// grab the input image's size
$size = getimagesize($filename);
$width = $size[0];
$height = $size[1];

$newImage = imagecreatetruecolor($width, $height);

// make sure that the image will retain alpha when saved
imagesavealpha($newImage, true);
// fill with transparent pixels first or else you'll
// get black instead of transparent
imagefill($newImage, 0, 0, imagecolorallocatealpha($newImage, 0, 0, 0, 127));

// loop through all the pixels
for ($y = 0; $y < $height; $y++)
{
    for ($x = 0; $x < $width; $x++)
    {
        // get the current pixel's colour
        $currentColor = imagecolorat($image, $x, $y);

        // then break it into easily parsed bits
        $colorParts = imagecolorsforindex($image, $currentColor);

        // if it's NOT white
        if (!($colorParts['red'] == 255 &&
            $colorParts['green'] == 255 &&
            $colorParts['blue'] == 255))
        {
            // then keep the same colour
            imagesetpixel($newImage, $x, $y, $currentColor);
        }
    }
}

// final output, the second arg is the filename
imagepng($newImage, 'newImage.png');

它允许我转过来:

Test image before processing

进入此(很难看到这里的alpha,但你可以打开它看看):

Image after processing

答案 1 :(得分:0)

一个简单明了的解决方案是使用imagecolorat并迭代png的所有像素,如果所有像素都是白色,则将其更改为透明。

希望这有帮助。

答案 2 :(得分:0)

在快速浏览GD手册页后,我认为以下内容适用于您(无论如何,它都在我的测试服务器上完成):

<?php
// Create image instance
$im = imagecreatefromgif('test.gif');
if (imagecolorstotal($im) == 1) {

    $rgb = imagecolorat($im, 1, 1); // feel free to test any pixel, but if they're all
                                    // the same colour, then it shouldn't really matter
    $colors = imagecolorsforindex($im, $rgb);

    if ($colors['red'] == 255 && $colors['green'] == 255 && $colors['blue'] == 255) {
        // I'm ignoring $colors['alpha'] but that's optional.
        echo "Yup; it's white...";
    }
} else {
    // if you've got more than one colour then the image has to be at least
    // two colours, so not really worth checking if any pixels are white
}

// Free image
imagedestroy($im);
?>

参考文献: