So over the last few months I've developed a few helper functions for some image related stuff I've been working on using PHP and GD and thought I would share them...
The filters simply introduces a function to pixelate an image resource and the second one makes all colors negative just like the imagefilter($im, IMG_FILTER_NEGATE) function, however since this is written in PHP theres really a difference in using my version and the C version, so use that if possible.
Sources:
PHP Code:
<?php
function imagepixelate($im, $blocksize)
{
$blocksize = (int) $blocksize;
if($blocksize < 0)
{
return(false);
}
if($blocksize == 1)
{
return(true);
}
for($x = 0; $x < imagesx($im); $x += $blocksize)
{
for($y = 0; $y < imagesy($im); $y += $blocksize)
{
if(!@ImageFilledRectangle($im, $x, $y, ($x + $blocksize - 1), ($y + $blocksize -1), ImageColorAt($im, $x, $y)))
{
return(false);
}
}
}
return(true);
}
function imagenegate($im)
{
for($x = 0; $x < imagesx($im); ++$x)
{
for($y = 0; $y < imagesy($im); ++$y)
{
$rgb = imagecolorat($im, $x, $y);
imagesetpixel($im, $x, $y, imagecolorallocate($im, (255 - (($rgb >> 16) & 0xFF)), (255 - (($rgb >> 8) & 0xFF)), (255 -($rgb & 0xFF))));
}
}
return(true);
}
?>
Note that I removed my checks for valid resource streams as these were apart of a smaller framework/package, so be sure to pass a valid image stream to them.
Prototypes:
bool imagepixelate( resource $im, integer $blocksize )
Pixelates an image
$im - A valid resource to a GD image
$blocksize - How many pixels that should go on each block, 1 = no effect because 1 = 1 pixel
bool imagenegate( resource $im )
Reverses color and creates a negative effect
$im - A valid resource to a GD image
Cheers, hope this will help someone ;)