Voting

: max(nine, five)?
(Example: nine)

The Note You're Voting On

Leo
13 years ago
The following function creates mask of a true color image for a given color. Beforehand for creating an image mask I used to loop over all image pixels and check their color using imagecolorat and copy if the color matches with imagesetpixel. This was very slow for large images, so the following code improves the process.

<?php

function getOppositeColor ($color)
{
return (
( (
255 - ($color >> 16) & 0xFF) << 16 ) +
( (
255 - ($color >> 8 ) & 0xFF) << 8 ) +
( (
255 - ($color ) & 0xFF) )
);
}

function
createImageMask (&$image, $color)
{
$w = imagesx ($image);
$h = imagesy ($image);

$tmpImage = imagecreatetruecolor ($w, $h);

imagecopy ($tmpImage, $image, 0, 0, 0, 0, $w, $h);

imagefilter ($image, IMG_FILTER_NEGATE);

imagecolortransparent ($image, getOppositeColor ($color));

imagecopymerge ($tmpImage, $image, 0, 0, 0, 0, $w, $h, 50);

imagedestroy ($image);

$image = $tmpImage;
}

?>

So for example, if we have a photo and we specify color = (255, 0, 0), i.e. red, the result will be image of the same size with red pixels everywhere the original photo was red and grey pixels exerywhere else.

<< Back to user notes page

To Top