<?PHP
/*
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html or see MPL-1.1.txt in directory "license"
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.
The Initial Developers of the Original Code are:
Copyright (c) 2003-2012, CR-Solutions (http://www.cr-solutions.net), Ricardo Cescon
All Rights Reserved.
Contributor(s): Ricardo Cescon
crVCL PHP Framework Version 2.4
*/
############################################################
if(!defined("CAP_LIB")){
define ("CAP_LIB", 1);
############################################################
//-------------------------------------------------------------------------------------------------------------------------------------
/**
* create a catcha picture (Completely Automated Public Turing to tell Computers from Humans Apart)
*
*/
class cap_str
{
// private
var $m_img = null;
// public
/**
* fontname like "arial or /font/arial.ttf
*
* @var string
*/
var $m_font = 'arial';
/**
* fontsize
*
* @var int
*/
var $m_fsize = 15;
/**
* fontcolor as hexcode like #000000
*
* @var string
*/
var $m_fcolor = "#000000";
/**
* backgroundcolor as hexcode like #F0F0F0
*
* @var string
*/
var $m_bgcolor = "#F0F0F0";
/**
* gridcolor as hexcode like #C8C8C8
*
* @var string
*/
var $m_gridcolor = "#C8C8C8";
/**
* imagetype like "jpg" or "bmp"
*
* @var string
*/
var $m_type = 'jpg';
/**
* height of image
*
* @var int
*/
var $m_height = 40;
/**
* width of image
*
* @var int
*/
var $m_width = 80;
/**
* grain size of grid
*
* @var int
*/
var $m_grid_grain_size = 10;
/**
* transparent color for gif as hexcode like #C8C8C8
*
* @var string
*/
var $m_transparentcolor = "";
/**
* true if you like to use version 2 of GD lib
*
* @var bool
*/
var $m_useGD2 = false;
//-------------------------------------------------------------------------------------------------------------------------------------
// constructor
function cap_str(){
}
//-------------------------------------------------------------------------------------------------------------------------------------
function __destruct(){
$this->destroy();
}
//-------------------------------------------------------------------------------------------------------------------------------------
function destroy(){
if($this->m_img){ImageDestroy($this->m_img);}
}
//-------------------------------------------------------------------------------------------------------------------------------------
// public
function create($string){
if($this->m_type == 'jpg'){$this->m_type = 'jpeg';}
$this->destroy();
if($this->m_useGD2 == true && !is_callable("ImageCreateTrueColor")){
$this->m_useGD2 = false;
}
if(!is_callable("ImageCreate")){
showFrameworkError("GD module for PHP not installed or loaded, please see php.ini");
return;
}
$this->m_width = (int)str_replace("px", "", $this->m_width);
$this->m_height = (int)str_replace("px", "", $this->m_height);
if(!$this->m_useGD2){
$this->m_img = ImageCreate($this->m_width, $this->m_height) or showFrameworkError("can´t init new GD image stream");
}else{
$this->m_img = ImageCreateTrueColor($this->m_width, $this->m_height) or showFrameworkError("can´t init new GD image stream");
}
$bgcolor = hex2rgb($this->m_bgcolor);
$bgcolor = ImageColorAllocate($this->m_img, $bgcolor[0], $bgcolor[1], $bgcolor[2]);
$gridcolor = hex2rgb($this->m_gridcolor);
$gridcolor = ImageColorAllocate($this->m_img, $gridcolor[0], $gridcolor[1], $gridcolor[2]);
$fcolor = hex2rgb($this->m_fcolor);
$fcolor = ImageColorAllocate($this->m_img, $fcolor[0], $fcolor[1], $fcolor[2]);
// paint grid
if($this->m_grid_grain_size > 0){
for($i = $this->m_grid_grain_size; $i < $this->m_width; $i += $this->m_grid_grain_size){
ImageLine($this->m_img, $i, 0, $i, $this->m_height, $gridcolor);
}
for($i = $this->m_grid_grain_size; $i < $this->m_height; $i += $this->m_grid_grain_size){
ImageLine($this->m_img, 0, $i, $this->m_width, $i, $gridcolor);
}
}
$x = $this->m_height / 2 - $this->m_fsize / 2;
ImageTTFText($this->m_img, $this->m_fsize, 0, $x, $this->m_fsize + $x, $fcolor, $this->m_font, $string);
}
//-------------------------------------------------------------------------------------------------------------------------------------
function save($filename){
switch ($this->m_type) {
case 'jpeg':
imagejpeg($this->m_img, $filename);
break;
case 'png':
imagepng($this->m_img, $filename);
break;
case 'gif':
if(!empty($this->m_transparentcolor)){
$tcolor = hex2rgb($this->m_transparentcolor);
$tcolor = imagecolorallocate($this->m_img, $tcolor[0], $tcolor[1], $tcolor[2]);
ImageColorTransparent($this->m_img, $tcolor);
}
imagegif($this->m_img, $filename);
break;
default:
showFrameworkError("can´t save captcha, type error");
break;
}
}
//-------------------------------------------------------------------------------------------------------------------------------------
/**
* trim the image size to the witdh and height of the text
*
*/
function trim(){
/**
* special thanks to zavaboy (http://zavaboy.com)
*/
$im = &$this->m_img;
$bg = &$this->m_bgcolor;
$p = array_fill(0, 4, 0);
// Get the image width and height.
$imw = imagesx($im);
$imh = imagesy($im);
// Set the X variables.
$xmin = $imw;
$xmax = 0;
// Start scanning for the edges.
for ($iy=0; $iy<$imh; $iy++){
$first = true;
for ($ix=0; $ix<$imw; $ix++){
$ndx = imagecolorat($im, $ix, $iy);
if ($ndx != $bg){
if ($xmin > $ix){ $xmin = $ix; }
if ($xmax < $ix){ $xmax = $ix; }
if (!isset($ymin)){ $ymin = $iy; }
$ymax = $iy;
if ($first){ $ix = $xmax; $first = false; }
}
}
}
// The new width and height of the image. (not including padding)
$imw = 1+$xmax-$xmin; // Image width in pixels
$imh = 1+$ymax-$ymin; // Image height in pixels
// Make another image to place the trimmed version in.
$im2 = imagecreatetruecolor($imw+$p[1]+$p[3], $imh+$p[0]+$p[2]);
// Make the background of the new image the same as the background of the old one.
$bg2 = imagecolorallocate($im2, ($bg >> 16) & 0xFF, ($bg >> 8) & 0xFF, $bg & 0xFF);
imagefill($im2, 0, 0, $bg2);
// Copy it over to the new image.
imagecopy($im2, $im, $p[3], $p[0], $xmin, $ymin, $imw, $imh);
// To finish up, we replace the old image which is referenced.
$im = $im2;
}
//-------------------------------------------------------------------------------------------------------------------------------------
}
//-------------------------------------------------------------------------------------------------------------------------------------
############################################################
}
############################################################
?>