Hudzilla.org - the homepage of Paul Hudson
Contents > Multimedia > Images Wish List | Report Bug | About Me ]

11.2.23     Special FX, Scatter

This is NOT the latest copy of this book; click here for the latest version.

One effect that can look particularly effective if used correctly is scatter, which basically sets the colour of the current pixel to that of another pixel close by, and vice versa, effectively swapping the two pixels. The effect is called scatter because it gives a slightly "disintegrated" look to a picture as pixels are moved around - one pixel could potentially get swapped all the way to the other side of the screen, although it isn't likely!

The scatter algorithm works like this:

  1. Go through each pixel

  2. Get a random X value between some negative number and some positive number

  3. Get a random Y value between some negative number and some positive number

  4. If the values are over the width/height of the image or under 0, continue the loop with the next pixel

  5. Otherwise, get the colour from the current pixel

  6. Get the colour from the nearby pixel

  7. Swap the two.

In PHP it is barely more difficult:

function scatter(&$image) {
    
$imagex = imagesx($image);
    
$imagey = imagesy($image);

    for (
$x = 0; $x < $imagex; ++$x) {
        for (
$y = 0; $y < $imagey; ++$y) {
            
$distx = rand(-4, 4);
            
$disty = rand(-4, 4);

            if (
$x + $distx >= $imagex) continue;
            if (
$x + $distx < 0) continue;
            if (
$y + $disty >= $imagey) continue;
            if (
$y + $disty < 0) continue;

            
$oldcol = imagecolorat($image, $x, $y);
            
$newcol = imagecolorat($image, $x + $distx, $y + $disty);
            
imagesetpixel($image, $x, $y, $newcol);
            
imagesetpixel($image, $x + $distx, $y + $disty, $oldcol);
        }
    }
}

To make your pixels scatter further, change the two values inside the rand() call.







<< 11.2.22 Special FX, Noise   11.2.24 Special FX, Pixelate >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is one plus zero?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow