<?php
/**
 * Codimage class
 * 
 * Provides methods related to captcha generation
 * 
 * @package linea21\core\system
 * @author $Author$ - linea21 <info@linea21.com>
 * @version $Id$ 
 * @access public
 * @license http://opensource.org/licenses/gpl-3.0.html
 * Generate code-image
 */

class codimage {
  /* @parameters
   * */
  var $CURRENT_IMG;
  var $IMG_SRC=__dir__."/../../lib/images/watermark.png";
  var $FONT_SIZE=16; // taille de la police
  var $FONT=__dir__."/../../lib/fonts/verdana.ttf"; // repertoire Police
  var $ANGLE=0;
  var $PADDING_X=60;
  var $PADDING_Y=20;
  var $TEXT_LENGTH=5;
  var $TEXT=5;
  var $BACKGROUND_COLOR;
  var $FONT_COLOR;

  function __construct() {
    $this->GetRandomText();
    $size = imagettfbbox($this->FONT_SIZE,$this->ANGLE,$this->FONT,$this->TEXT);
    $dx = abs($size[2]-$size[0]);
    $dy = abs($size[5]-$size[3]);
    $this->CURRENT_IMG = imagecreate($dx+$this->PADDING_X,$dy+$this->PADDING_Y);
    $picsrc = imagecreatefrompng($this->IMG_SRC);
    Imagecopymerge ( $this->CURRENT_IMG  ,  $picsrc , 0  , 0  , 0  , 0  , imagesx($this->CURRENT_IMG)  , imagesy($this->CURRENT_IMG),100);
    $this->GetFontColor();
    ImageTTFText($this->CURRENT_IMG, $this->FONT_SIZE, $this->ANGLE, (int)($this->PADDING_X/2)+1, $dy+(int)($this->PADDING_Y/2), $this->FONT_COLOR, $this->FONT, $this->TEXT);
  }

  function Output() {
    Header("Content-type: image/png");
    ImagePng($this->CURRENT_IMG);
    ImageDestroy($this->CURRENT_IMG);
  }

  function GetBackgroundColor() {
    if(empty($this->BACKGROUND_COLOR))	$this->SetBackgroundColor();
    return true;
  }

  function SetBackgroundColor($r=200, $v=200, $b=200) {
    $this->BACKGROUND_COLOR= ImageColorAllocate($this->CURRENT_IMG, $r, $v, $b);
    return true;
  }

  function GetRandomText()
  {
    $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    $text = '';
    for ($i = 0; $i < $this->TEXT_LENGTH; $i++) {
      $text .= substr($str, rand(0, strlen($str) - 1), 1);
    }
    $this->TEXT=$text;
  }

  function SetFontSize($size)
  {
    $this->FONT_SIZE=$size;
    return true;
  }

  function SetPaddingX($pad)
  {
    $this->PADDING_X=$pad;
    return true;
  }

  function SetPaddingY($pad)
  {
    $this->PADDING_Y=$pad;
    return true;
  }

  function GetFontColor() {
    if(empty($this->FONT_COLOR))	$this->SetFontColor();
    return true;
  }

  function SetFontColor($r=150, $v=150, $b=150)
  {
    $this->FONT_COLOR= ImageColorAllocate($this->CURRENT_IMG, $r,$v, $b);
    return true;
  }

}

?>