Search code examples
phptransparencygd

Check if image is white; if so make transparent


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?


Solution

  • 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