<?php
class ImageViewer {
// php_include/classes/ImageViewer.class.php
var $db;
var $fontPath;
var $basePath;
var $error;
var $font;
var $fontSize ;
var $debOutput ;
//only alphanumeric with dashes,dots allowed
//just a nice security check
function isValidImageName($str){
return true;
$pattern ='-_0-9\.A-Za-z\/';
return preg_match("/^[$pattern]+$/", $str) ;
}
function __construct($img, $xOffset =150 , $yOffset =3, $fontSize=10, $theFont='', $pathToFont=''){
$this->debOutput ='';
$this->db = 1 ;
$this->error = 0 ;
if( strcmp($theFont,'') ===0)
$this->font= 'ArialN.TTF';
else
$this->font= theFont ;
$this->basePath = $_SERVER['DOCUMENT_ROOT'] ;
if( $pathToFont == '')
$this->fontPath = $this->basePath .'/fonts/'.$this->font ;
else //user supplied as
$this->fontPath = $pathToFont;
$this->fontSize = $fontSize;
//add initial slash if necessary
$img = (substr($img,0,1) === '/' )? $img : '/'. $img ;
$fullImagePath = $this->basePath . $img;
if( (int) $this->db === 1){
$this->debOutput .= '<h1>Debug Output is On</h1>';
$this->debOutput .= 'path ' . $fullImagePath .' <br />';
$this->debOutput .= 'font ' . $this->fontPath . '<br />';
}
if( 0 === (int)$this->isValidImageName($img))
{
die($img . ' is not a valid image name.');
}
if(0 === (int)is_file($fullImagePath))
{
die( $img . ' is not a valid file');
}
else if( (int) $this->db === 1)
$this->debOutput .=('<br / >'. $fullImagePath . ' is a valid file <br />');
if( 0 === (int) getimagesize ($fullImagePath) )
{
$this->debOutput .= '<br />' . $fullImagePath . '<br />';
}
if( (int) $this->db === 1 && (int) $this->error === 1)
{
$this->debOutput .= '<br />Error : ' . $this->error . ' <br />';
}
$extension = substr($img, strrpos($img,'.')+1 );
if($extension =='jpg' || $extension =='jpeg'){
header('Content-Type: image/jpeg');
$image = imagecreatefromjpeg($fullImagePath);
}
else if($extension =='gif' )
{
header('Content-Type: image/gif');
$image = imagecreatefromgif($fullImagePath);
}
else if($extension =='png'){
header('Content-Type: image/png');
$image = imagecreatefrompng($fullImagePath);
}
$color = imagecolorallocate($image, 0x00, 0x00, 0x00);
$fontRotation = '0';
$text = 'www.mathwarehouse.com ';// . date('m/j/Y');
$x=imagesx($image)- $xOffset ;
$y=imagesy($image)- $yOffset ;
imagettftext($image, $this->fontSize , $fontRotation, $x, $y, $color, $this->fontPath, $text);
//always output png which has clearer text anti-aliasing
imagepng($image);
imagedestroy($image);
//echo $this->debOutput;
}
}
?>