Boutells GD-Library essentials - GD PART I

The one thing I've had most fun with in php this far, is the GD-lib. I guess the reason is the ease with wich rather spectacular results are made. To begin lightly, I'll show just how to make a .php-document act as an image and how to show that.

Firstly a normal HTML-document contains an IMG-tag, such as
<img src="dot_image.php">

That calls HTML to load an image but source set the .php-document of my choice instead.
To take a look at dot_image.php

: dot_image.php
<?
    header
('Content-type: image/png');

    
$im imagecreatefrompng("images/dots.png");
    
imagepng($im);
?>
In this above case, dot_image.php does nothing more than set headers as image, read the image into a variable followed by outputting it. This does however leave us with the option of interacting with the image between those two occurences. So what can we do with a image that's read into php? (in this case a 24-bit PNG as I've found them to give the best results with GD)

A complete list of the features available can be found at http://www.php.net/manual/en/ref.image.php

Below, I'll just show you how to use floodfills by HTTP GET argumentswhen calling images (you know, adding the document.php?x=y directive) - doing nothing but reuesting the image by different querystrings.

<?
    
//set header right
    
header('Content-type: image/png');
    
    
//load up image
    
$im imagecreatefrompng("images/dots.png");

    
//Only work image on request
    
if(isset($_GET['color'])){
        
$color $_GET['color'];
        if(
$color == "red"||$color=="blue"||$color=="yellow"||$color=="green"){
            
$red imagecolorclosest($im15044);
            
$blue imagecolorclosest($im3419127);
            
$yellow imagecolorclosest($im24624197);
            
$green imagecolorclosest($im2415724);
        
            
imagefill($im22, $$color);
        }    
    }

    
//output
    
imagepng($im);
?>
This above example floodfills the image with any of the four selected colors upon request. On links below you can see this for yourself, just keep in mind the image originates from the same source - fill color is applied programatically. I'd say it's quite cool ^^

Green fill
Red fill
Blue fill
Yellow fill



Seems I'm crap with images at times, this image looks like shit after it's filled ... and it's not supposed to. Dunno what I did wrong. Bleh!