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

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?

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

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.

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: