![]() |
Pixelate algorithm using GD
Long time no script for here, so I decided that a function I was working on the other day might be useful for posting here.
Its an pixelate algorithm written in PHP using GD, it includes a basic algorithm that works by coloring the pixel block the first color found in a block. The advanced algorithm iterates through all pixels and finds the average color in a block and colors it like that, through meaning more execution time. Source: PHP Code:
The code is licensed under the Ms-Pl license which can be found at: http://www.opensource.org/licenses/ms-pl.html Prototype: imagepixelate(resource $image, integer $blocksize [, boolean $advanced = false])The $image parameter must be a resource valid resource, invalid resources NULL is returned. The $blocksize parameter defines the blocksize in pixels. If a blocksize of 0 or lower is supplied the function will return false. Note that a blocksize of 1 wouldn't have any effect as each block already is one pixel so therefore the function just returns true. The $advanced parameter is used to specify which algorithm to use, by default its using the simple algorithm, descriptions can be found above. Return values, NULL on invalid resource specified, FALSE on error and TRUE on success. Example code (basic): PHP Code:
PHP Code:
![]() Example output (advanced): ![]() A clear difference is visable, enjoy! :) C Version I've made a version, also written for gd. So in order to get it to work you have to be a minor libgd hacker. The C code is as follows: c Code:
Put that in the bottom of gd.c, and in the gd.h header after the other function prototypes then add the following: c Code:
Its usage are exactly the same as the php version, except for this is on C level and much faster ofcourse. Furthermore for people who also hacks php a little you can alter the gd binding to support this function, I assume people who do this knows how the basic PHPAPI works so I'll only post the PHP_FUNCTION macro's body: c Code:
I've submitted it as a feature request to GD itself and yet again, enjoy! :) |
Nice script, but can't you just scale an image down and then scale it up to get the same effect?...
|
I'm sorry if this is a weird question, but I really don't understand what this is for :O
|
A similar technique using imagecopyresized/imagecopyresampled (with a block size of 5) would produce something like:
Good work with the function Kalle, did you first write it in 2006 (looking at the copyright). |
I initially wrote the simple pixelate effect in late 2006 yea, but the advanced version was written a few weeks ago :)
@Tanax im an image effect, could for example be used to style an image dynamiclly, or maybe on an verification image to blur it. @Sjaq in theory I think its possible yea, but I didn't really look at it. |
Okey, I understand now what it does.
Now I have a new question. Why would you want to do this? I mean, the original image is better quality? :O |
Quote:
|
Quote:
Theres no exact usage for it, as people's need vary :) |
Updated the original post with a C version for use with GD, enjoy! :)
|
As an update for this, since PHP 5.3.0 RC1, this pixelation algorithm have been apart of the GD binding for PHP, and its available as of GD 2.1.0.
For more information and documentation see: http://php.net/imagefilter |
| All times are GMT. The time now is 04:06 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0