Creating a random signature (images)

  • This example will show you how we can generate images with random backgrounds.

    Let's create a new folder (webroot/sig/images/random), then upload some background images there. For instance, we'll use the following images in this example:

    (380x50)
    (380x39)
    (400x39)

    The script should randomly select any image stored in that folder. Once created such script, all we need to worry about is uploading the images that we want as random backgrounds.

    So, let's create another file called random_images_signature.png and upload it to your webroot/sig folder, with the following contents:

    <?php
    //
    // Load all Images from specified path...
    //
    $images_ary = array();
    $handle = @opendir('./images/random');
    while( 
    $file = @readdir($handle) )
    {
        if( 
    $file <> '.' && $file <> '..' )
        {
            
    $tmpary explode('.'$file);
            
    $extension strtolower($tmpary[count($tmpary)-1]);
            if( 
    in_array($extension, array('gif''jpg''jpeg''png')) )
            {
                
    $images_ary[] = $file;
            }
        }
    }
    @
    closedir($handle);

    if( 
    count($images_ary) <= )
    {
        die(
    "Sorry, no images found!");
    }

    //
    // Randomly select an image...
    //
    $random time() % count($images_ary);
    $image_info = array(
        
    'image'    => 'random/'.$images_ary[$random]
    );

    //
    // Build the text array...
    //
    $image_text = array(
        array(
            
    'x'     => 10,
            
    'y'     => 4,
            
    'color' => array(00255),
            
    'font'  => -5,
            
    'text'  => "Random Image: ".$images_ary[$random]
        )
    );

    include(
    './includes/dynamic_gd_image.php');
    ?>

    This is how it looks like:

    If you wish to see the random effect, just click here to pop it up and refresh the new window several times.