TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Script Giveaway (http://www.talkphp.com/script-giveaway/)
-   -   Simple pixelate + negative functions (http://www.talkphp.com/script-giveaway/3176-simple-pixelate-negative-functions.html)

Kalle 07-26-2008 12:11 PM

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 ;)

ETbyrne 07-27-2008 02:49 AM

Cool! I've got to check this out right now!


All times are GMT. The time now is 05:29 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0