<?
/*
Class: IMGOut
Simple image processor.
Cyrillic edition (KOI-8).
(C) m0Ray 2005
Russia, Samara
*/
//-----------------
//IMGOut variable command assignment
define("IMGO_cmd_type","type"); //output type
define("IMGO_cmd_src","src"); //relative path of base image in local filesystem (checked)
define("IMGO_cmd_w","w"); // output image
define("IMGO_cmd_h","h"); // dimensions
define("IMGO_cmd_fast","fast"); // disable interpolation
define("IMGO_cmd_bgcolor","bgcolor"); // image background color
define("IMGO_cmd_bgtrans","bgtrans"); // do background color transparent in PNG and GIF
define("IMGO_cmd_color","color"); // text color
define("IMGO_cmd_glowcolor","tglowcolor"); // text shadow color
define("IMGO_cmd_exact","exact"); // keep base image aspect ratio
define("IMGO_cmd_text","text"); // text string to write on image
define("IMGO_cmd_ttf","ttf"); // ttf font to use
define("IMGO_cmd_tx","tx"); // text
define("IMGO_cmd_ty","ty"); // offset
define("IMGO_cmd_textal","tal"); // text alignment
define("IMGO_cmd_tsize","tsize"); // text size
define("IMGO_cmd_tglow","tglow"); // text glow
//-----------------
//IMGOut defaults
define("IMGO_def_type","JPEG"); //JPEG by default
define("IMGO_def_w",320); // default image size is
define("IMGO_def_h",200); // 320x200
define("IMGO_def_tsize",14); // default text size
define("IMGO_def_tx",0); // default text
define("IMGO_def_ty",0); // offset
define("IMGO_def_textal","lb"); // default text alignment
define("IMGO_def_ttf","default.ttf"); // coordinates
define("IMGO_def_bgR",0); // default
define("IMGO_def_bgG",0); // background
define("IMGO_def_bgB",0); // color
define("IMGO_def_tR",255); // default
define("IMGO_def_tG",255); // text
define("IMGO_def_tB",255); // color
define("IMGO_def_tsR",160); // default
define("IMGO_def_tsG",160); // text shadow
define("IMGO_def_tsB",160); // color
//-----------------
//IMGOut messages
define("IMGO_errmsg","IMGOut error: ");
//Source path
define("IMGO_msg_badpath","Source path seems to be incorrect. Trying to hack? String: ");
define("IMGO_msg_badfontpath","Font path seems to be incorrect. Trying to hack? String: ");
define("IMGO_msg_pathnotexist","Source path does not exist or permission denied: ");
//Image types
define("IMGO_msg_badouttype","Unknown output image type specified: ");
define("IMGO_msg_gdbadouttype","Unsupported by libgd output image type: ");
define("IMGO_msg_gdbadintype","libgd cannot read images of type ");
define("IMGO_msg_gdnonimage","libgd cannot determine image type. Not an image? Path: ");
//The Class
class IMGOut{
var $imgtypes=array(1=>"GIF",2=>"JPEG",3=>"PNG",4=>"SWF",5=>"PSD",6=>"WBMP",10=>"JP2","GIF"=>1,"JPEG"=>2,"PNG"=>3,"SWF"=>4,"PSD"=>5,"WBMP"=>6,"JP2"=>10);
var $imgtype=IMGO_def_type;
function IMGOut( $vars ){
//initialize image output type
if( $vars[IMGO_cmd_type] ) $this->SetOutputType(strtoupper($vars[IMGO_cmd_type]));
//determine base image properties
if( $src=$vars[IMGO_cmd_src] ){
if( ($src{0}=="/") or preg_match('/\.\./',$src) ) die(IMGO_errmsg.IMGO_msg_badpath.$src);
if( !is_readable($src) ) die(IMGO_errmsg.IMGO_msg_pathnotexist.$src);
$is=GetImageSize($src);
$srcw=$is[0];
$srch=$is[1];
if( @array_key_exists($is[2],$this->imgtypes) )
$intype=$this->imgtypes[$is[2]];
else
die(IMGO_errmsg.IMGO_msg_gdnonimage.$src);
//load base image
if( function_exists("ImageCreateFrom".$intype) )
eval('$img=ImageCreateFrom'.$intype.'("'.$src.'");');
else
die(IMGO_errmsg.IMGO_msg_gdbadintype.$intype);
}
//decide about output image dimensions
if( $vars[IMGO_cmd_w] )
$dstw=intval($vars[IMGO_cmd_w]);
else
$dstw=$srcw?$srcw:IMGO_def_w;
if( $vars[IMGO_cmd_h] )
$dsth=intval($vars[IMGO_cmd_h]);
else
$dsth=$srch?$srch:IMGO_def_h;
//create image
$this->dstimg= ( ($this->imgtype=='JPEG') or ($this->imgtype=='PNG') )?
ImageCreateTrueColor($dstw,$dsth):
ImageCreate($dstw,$dsth);
$this->width=$dstw;
$this->height=$dsth;
//work with colors
$colorstr=$vars[IMGO_cmd_bgcolor]; //background color
if($colorstr){
$colR=intval(substr($colorstr,0,2),16);
$colG=intval(substr($colorstr,2,2),16);
$colB=intval(substr($colorstr,4,2),16);
}
$bg=$colorstr?ImageColorAllocate($this->dstimg,$colR,$colG,$colB):ImageColorAllocate($this->dstimg,IMGO_def_bgR,IMGO_def_bgG,IMGO_def_bgB);
if($vars[IMGO_cmd_bgtrans] and (($this->imgtype=="GIF")or($this->imgtype=="PNG")))$bg=ImageColorTransparent($this->dstimg,$bg);
//fill image with background color
if( (!$img) or ($vars[IMGO_cmd_exact]) )ImageFill($this->dstimg,0,0,$bg);
//apply base image resized
if( $img ){
$nw=$dstw;
$nh=$dsth;
$xpos=0;
$ypos=0;
if( $vars[IMGO_cmd_exact] ) {
$aspect=min($dstw/$srcw,$dsth/$srch);
$nw=round($srcw*$aspect);
$nh=round($srch*$aspect);
$xpos=round(($dstw-$nw)/2);
$ypos=round(($dsth-$nh)/2);
}
if($vars[IMGO_cmd_fast])
ImageCopyResized($this->dstimg,$img,$xpos,$ypos,0,0,$nw,$nh,$srcw,$srch);
else
ImageCopyResampled($this->dstimg,$img,$xpos,$ypos,0,0,$nw,$nh,$srcw,$srch);
}
//apply text
if( $vars[IMGO_cmd_text] ) $this->Text($vars);
}
function SetOutputType( $type ) {
if( array_key_exists($type,$this->imgtypes) ) {
if( intval($type) ) $type=$this->imgtypes[$type];
if( !function_exists("Image".$type) )
die(IMGO_errmsg.IMGO_msg_gdbadouttype.$type);
else
$this->imgtype=$type;
}
}
function Text ( $vars ) {
if( $colorstr=$vars[IMGO_cmd_color] ) {
$colR=intval(substr($colorstr,0,2),16);
$colG=intval(substr($colorstr,2,2),16);
$colB=intval(substr($colorstr,4,2),16);
ImageColorAllocate($this->dstimg,$colR,$colG,$colB);
}else
$color=ImageColorAllocate($this->dstimg,IMGO_def_tR,IMGO_def_tG,IMGO_def_tB);
//apply text
if($vars[IMGO_cmd_text]){
$tfont=$vars[IMGO_cmd_ttf]?$vars[IMGO_cmd_ttf]:IMGO_def_ttf;
if( ($tfont{0}=="/") or preg_match('/\.\./',$tfont) ) die(IMGO_errmsg.IMGO_msg_badfontpath.$tfont);
$tsize=intval($vars[IMGO_cmd_tsize])?intval($vars[IMGO_cmd_tsize]):IMGO_def_tsize;
$txpos=intval($vars[IMGO_cmd_tx])?intval($vars[IMGO_cmd_tx]):IMGO_def_tx;
$typos=(intval($vars[IMGO_cmd_ty])?intval($vars[IMGO_cmd_ty]):IMGO_def_ty);
$tal=$vars[IMGO_cmd_textal]?strtolower($vars[IMGO_cmd_textal]):IMGO_def_textal;
$tb=ImageTTFBBox($tsize,0,$tfont,$vars[IMGO_cmd_text]);
if($tal{0}=="r"){
$txpos=$this->width-$txpos-$tb[2];
}
if($tal{1}=="b"){
$typos=$this->height-$typos-$tb[3];
}else{
$typos=$typos+$tb[3]-$tb[5];
}
if($vars[IMGO_cmd_tglow]){ // glow processing
$colorstr2=$vars[IMGO_cmd_glowcolor];
if($colorstr2){
$col2R=intval(substr($colorstr2,0,2),16);
$col2G=intval(substr($colorstr2,2,2),16);
$col2B=intval(substr($colorstr2,4,2),16);
}
$color2=$colorstr2?ImageColorAllocate($this->dstimg,$col2R,$col2G,$col2B):ImageColorAllocate($this->dstimg,IMGO_def_tsR,IMGO_def_tsG,IMGO_def_tsB);
imagettftext($this->dstimg,$tsize,0,$txpos-1,$typos-1,$color2,$tfont,convert_cyr_string($vars[IMGO_cmd_text],"k","w"));
imagettftext($this->dstimg,$tsize,0,$txpos+1,$typos-1,$color2,$tfont,convert_cyr_string($vars[IMGO_cmd_text],"k","w"));
imagettftext($this->dstimg,$tsize,0,$txpos-1,$typos+1,$color2,$tfont,convert_cyr_string($vars[IMGO_cmd_text],"k","w"));
imagettftext($this->dstimg,$tsize,0,$txpos+1,$typos+1,$color2,$tfont,convert_cyr_string($vars[IMGO_cmd_text],"k","w"));
}
imagettftext($this->dstimg,$tsize,0,$txpos,$typos,$color,$tfont,convert_cyr_string($vars[IMGO_cmd_text],"k","w"));
}
}
function Output ( ) {
if( !headers_sent() ) {
header("Content-type: image/".strtolower($this->imgtype));
eval("Image".$this->imgtype.'($this->dstimg);');
}
}
}
?>