<?php

@session_start();
$random_img = rand_alpha();
$_SESSION ["random_img"] = $random_img;
show_gd_img($random_img);

function show_gd_img($content = "") {
    flush();
    @header("Content-Type: image/gif");
    $font_style = 6;
    $no_chars = strlen($content);
    $charheight = ImageFontHeight($font_style);
    $charwidth = ImageFontWidth($font_style);
    $strwidth = $charwidth * intval($no_chars);
    $strheight = $charheight;
    $imgwidth = $strwidth + 15;
    $imgheight = $strheight + 5;
    $img_c_x = $imgwidth / 2;
    $img_c_y = $imgheight / 2;
    $im = ImageCreate($imgwidth, $imgheight);
    $back_col = ImageColorAllocate($im, 222, 244, 255);
    $text_col = ImageColorAllocate($im, 255, 0, 0);
    ImageFilledRectangle($im, 3, 3, $imgwidth, $imgheight, $back_col);
    $draw_pos_x = $img_c_x - ($strwidth / 2) + 1;
    $draw_pos_y = $img_c_y - ($strheight / 2) + 1;
    ImageString($im, $font_style, $draw_pos_x, $draw_pos_y, $content, $text_col);
    Imagegif($im);
    ImageDestroy($im);
    exit();
}

function rand_alpha() {
    $a = array('1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'X', 'W', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'x', 'w', 'y', 'z');
    $b = "";
    for ($i = 0; $i < 6; $i++)
        $b .= $a [rand(0, count($a) - 1)];
    return $b;
}

?>  
