检查图像是否为白色;如果是这样,请使其透明

发布于 2024-12-09 20:57:30 字数 347 浏览 1 评论 0原文

我有一个图像,如果它是全白色的,我想使它透明。我已经有用于 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?

I have an image, and I want to make it transparent if it's completely white. I have the following code already for GD for getting a part of the image:

$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);

But how can I have it first check if the image is completely white, if so change it to transparent, and apply that to $destp?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋凉 2024-12-16 20:57:30

编辑:

根据重新阅读问题和下面的讨论,我相信这就是您正在寻找的内容:

$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');

它允许我将其转变为:

处理前的测试图像

变成这个(很难看到alpha 在这里,但你可以打开它来查看):

处理后的图像

EDIT:

Based on re-reading the question, and the discussion below, I believe this is what you're looking for:

$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);

This produces a blank image if the original image's 8x8 slice is all white, otherwise it resamples the 8x8 slice to 150x150.


Original:

I haven't ever done anything with PHP GD before, and thought it would be a fun challenge. Here's how I ended up making this happen:

$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');

It allowed me to turn this:

Test image before processing

Into this (hard to see the alpha here, but you can open it to see):

Image after processing

温折酒 2024-12-16 20:57:30

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

希望这有帮助。

A simple strightforward solution would be to use imagecolorat and iterate through all the pixels of the png and if all are white change it to transparent.

Hope this helps.

你又不是我 2024-12-16 20:57:30

快速浏览完 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);
?>

参考文献:

After a quick look through the GD manual pages, I think that the following should work for you (it did on my test server, anyway):

<?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);
?>

References:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文