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

11.2.18     Special FX, Interlacing

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

In image terms, "interlacing" means two quite different things. One is a special effect, and the other is a form of progressive image download that allows users to see part of the image before it is fully downloaded. As this is the special effects section, we will be looking at the former meaning: interlacing is when an image is only visible on alternate lines. Although this might sound odd, it has a very cinematic look to it and you will often see it in movies and games where they try to depict electronic viewing devices such as sniper aiming scopes.

In code terms, we need to loop through the image row by row, and, if the row is odd-numbered (i.e. row 1, 3, 5, 7, etc) we should colour all pixels on that row black. As with the imagetruecolortopalette() function, our interlacing function should accept the image to interlace as a reference so that all changes are made directly onto the image.

Here is how that looks in PHP:

<?php
    
function interlace (&$image) {
        
$imagex = imagesx($image);
        
$imagey = imagesy($image);
        GLOBAL
$black;
        
// loop through all rows in the image

        
for ($y = 0; $y < $imagey; ++$y) {
            
// if it is even...
            
if ($y % 2) {
                
// loop through all pixels in this row
                
for ($x = 0; $x < $imagex; ++$x) {
                    
// set them to black
                    
ImageSetPixel($image, $x, $y, $black);
                }
            }
        }
    }

    
$image = imagecreatefrompng("space.png");
    
$black = imagecolorallocate($image, 0, 0, 0);
    
interlace($image);

    
header("image/png");
    
imagepng($image);
    
imagedestroy($image);
?>

Note that the decision whether to blacken a row is dependent on the $y % 2 condition, which means, "if the remainder of $y divided by 2 is equal to 1". As $y is set to the current row, then this will evaluate as true for all odd rows - 0 / 2 = remainder 0, 1 / 2 = remainder 1, 2 / 2 = remainder 0, 3 / 2 = remainder 1, etc.

Now, although the script above is quite fast, you can make it faster by eliminating the $x loop. Rather than individually setting all pixels on a row to black, it would be easier to change the entire row at once by drawing a line. For even more speed, you can drop the if statement altogether and just change the loop to $y = $y + 2 rather than ++$y - this means PHP will always draw the black lines, but skips a line in the loop automatically. These two changes would make the interlace() function look like this:

function interlace (&$image) {
    
$imagex = imagesx($image);
    
$imagey = imagesy($image);
    GLOBAL
$black;

    for (
$y = 1; $y < $imagey; $y += 2) {
        
imageline($image, 0, $y, $imagex, $y, $black);
    }
}

If you recall, the parameters to imageline() are the image to draw on, the starting X and Y co-ordinates, and the ending X and Y co-ordinates. As such, that above line of code draws from X = 0 to X = the width of the image, all in black - just like our loop did, except much faster.

Keep the previous code in mind, however - looping through rows and columns is a skill that will be heavily used in later image effects.







<< 11.2.17 Special FX, Colour reduction: imagetruecolortopalette()   11.2.19 Special FX, Screen >>
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 one?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow