View Single Post
Old 07-26-2008, 01:11 PM   #1 (permalink)
Kalle
The Addict
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 249
Thanks: 6
Kalle is on a distinguished road
Default Simple pixelate + negative functions

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$yimagecolorallocate($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 ;)
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
The Following User Says Thank You to Kalle For This Useful Post:
ETbyrne (07-27-2008)