<?php

 function blur (&$image) {
    $imagex = imagesx($image);
    $imagey = imagesy($image);
    $dist = 1;

    for ($x = 1; $x < $imagex-1; ++$x) {
        for ($y = 1; $y < $imagey-1; ++$y) {
            $newr = 0;
            $newg = 0;
            $newb = 0;

            $colours = array();
            $thiscol = imagecolorat($image, $x, $y);

            for ($k = $x - 1; $k <= $x + 1; ++$k) {
                for ($l = $y - 1; $l <= $y + 1; ++$l) {
                    $colours[] = imagecolorat($image, $k, $l);
                }
            }

	    for($k = 0; $k <7; $k++)
	        $colours[] = $thiscol;
            foreach($colours as $colour) {
                $newr += ($colour >> 16) & 0xFF;
                $newg += ($colour >> 8) & 0xFF;
                $newb += $colour & 0xFF;
            }

            $numelements = count($colours);
	    $newr = $newr >> 4;
	    $newg = $newg >> 4;
	    $newb = $newb >> 4;
            //$newr /= $numelements;
            //$newg /= $numelements;
            //$newb /= $numelements;

            $newcol = imagecolorallocate($image, $newr, $newg, $newb);
            imagesetpixel($image, $x, $y, $newcol);
        }
    }
} 

function DrawLines($imgb, $width, $height, $border, $count)
{
    for ($line = 0; $line < $count; $line++)
    {
        $color = ImageColorAllocateAlpha($imgb, rand(176, 208), rand(176, 208), rand(176, 208), 24);
        $x = rand($border, $width-$border);
        $y = rand($border, $height-$border);
        //$dx = rand(-$border, $border);
        //$dy = rand(-$border, $border);
        $dx = rand(-$width, $width);
        $dy = rand(-$height, $height);
        if ($x+$dx>$width-1)
          $dx=$width-1-$x;
        if ($x+$dx<0)
          $dx=-$x;
        if ($y+$dy>$height-1)
          $dy=$height-1-$y; 
        if ($y+$dy<0)
          $dy=-$y; 
        ImageLine($imgb, $x, $y, $x+$dx, $y+$dy, $color);
    }
}

function GetCodeImage($width, $height, $border, $code)
{
    $imgb = imagecreatetruecolor($width,$height);
    $gray = ImageColorAllocate ($imgb, 128, 128, 128);
    $silver = ImageColorAllocate ($imgb, 192, 192, 192);
    $white = ImageColorAllocate ($imgb, 255, 255, 255);

    $len = strlen($code);

    DrawLines($imgb,$width,$height,$border,25);

    $font = 0;
    $y = -3;//-$border;
    $xx = ($width - $border*2) / $len;
    $x = $border +1;
    if (file_exists('tahoma.gdf'))
      $font=imageloadfont('tahoma.gdf');
    $img = $imgb;
    if ($font==0)
    {
        $y=0;
        $font=4;
        $xx = ($width/2 - $border*2) / $len; 
        $img = imagecreatetruecolor($width/2,$height/2);
    }
    for ($pos = 0; $pos < $len; $pos++)
    {
        $shift = rand(5, 10) * ((rand(0,2)>1)?(-1):(1));
        $rotation = rand(-15, 15);
        //$gray = ImageColorAllocate ($img, rand(112, 144), rand(112, 144), rand(112, 144));
        $gray = ImageColorAllocate ($img, rand(90, 112), rand(90, 112), rand(90, 112));
        $silver = ImageColorAllocate ($img, rand(176, 208), rand(176, 208), rand(176, 208));
        $white = ImageColorAllocate ($img, rand(224, 255), rand(224, 255), rand(224, 255));
        {
            //imagechar($img, $font, $x+1, $y+1, $code[$pos], $gray);
            imagechar($img, $font, $x, $y, $code[$pos], $white);
            //ImageTTFText($img, $fontsize, $rotation-$shift, $x, $y, $gray, $font, $code[$pos]);
            //ImageTTFText($img, $fontsize, $rotation+$shift, $x, $y, $silver, $font, $code[$pos]);
            //ImageTTFText($img, $fontsize, $rotation, $x, $y, $white, 5, $code[$pos]);
        }
        $x += $xx;
    }
    if ($font<=5)
    {
        if (function_exists('imagecopyresampled'))
            imagecopyresized($imgb, $img, 0,0, 0,0,$width, $height, $width/2, $height/2);
        else
            imagecopyresampled($imgb, $img, 0,0, 0,0,$width, $height, $width/2, $height/2);
        imagedestroy($img);
    }
    //DrawLines($imgb,$width,$height,$border,20);


    $blurMatrix = array(array(1,1,1), array(1,16,1), array(1,1,1));
    $divisor = 24;
    $offset = 0;

    if (function_exists('ImageConvolution'))
        ImageConvolution($imgb, $blurMatrix, $divisor, $offset);
    else
	blur($imgb);
    return $imgb;
}
{
    $width = 80;
    $height = 28;
    $border = 4;
    $code = strval(rand(1000,9999));
    
    header('P3P: CP="CAO PSA OUR"');
    setcookie("zpstest",md5(intval($code)), time()+1200);
    setcookie("zpscntr",2, time()+1200);
    
    $img = GetCodeImage($width, $height, $border, $code);
    header("Content-type: image/png");
    header("Expires: 0");
    imagepng($img);
    imagedestroy($img);
}
?> 
