errorlog_num_errors = 0; $this->initConfig(); if($this->getConfig("clearlog")){ $this->clearErrorLog(); } $this->initFiletypes(); if($image_or_filename !== false){ if(is_string($image_or_filename)){ $filename = $image_or_filename; if($filename != ""){ $this->load($filename); } }else{ $this->setImage($image_or_filename); } }else{ $this->setImage(false); } }//end function dynamicImage(); function free(){ if($this->image){ imageDestroy($this->image); } }//end function free(); function setImage($image){ $this->image = $image; if($this->getConfig("forcetruecolor") and !imageistruecolor($this->image)){ $this->convertToTruecolor(); } }//end function setImage(); function getImage(){ return $this->image; }//end function getImage(); function refresh(){ $this->setImage($this->image); }//end function refresh(); function initFiletypes(){ $this->filetypes = array(); $this->filetypes["JPEG"] = array("suffix"=>"jpg"); $this->filetypes["GIF"] = array("suffix"=>"png"); $this->filetypes["PNG"] = array("suffix"=>"png"); }//end function initFileTypes(); function initConfig(){ $this->config = array(); $this->setConfig("logerrors",true); $this->setConfig("logfile","dynimg-error.log"); $this->setConfig("clearlog",true); $this->setConfig("outputerrorimage",false); $this->setConfig("errorimagefontfile","arial.ttf"); $this->setConfig("outputerrorspacer",true); $this->setConfig("jpegdefaultquality",80); $this->setConfig("outputdefaulttype","JPEG"); $this->setConfig("forcetruecolor",false); $this->setConfig("cachedirectory","./imgcache/"); $this->setConfig("validdefaulttime",2592000); }//end function initConfig(); function setConfig ($key,$value){ if(!$key or $key == ""){ return false; } $this->config[strtolower($key)] = $value; return true; }//end function setConfig(); function getConfig ($key,$ignoreerrors=false){ if(!$key or $key == ""){ if(!$ignoreerrors){ $this->logError("getConfig() failed! key '$key' not found!"); } return false; } return $this->config[strtolower($key)]; }//end function getConfig(); function getFileType ($filename){ $fn = strtolower($filename); $type = false; if(substr($fn,-4) == ".jpg" or substr($fn,-5) == ".jpeg" or substr($fn,-4) == ".jpe") $type = "JPEG"; if(substr($fn,-4) == ".png") $type = "PNG"; if(substr($fn,-4) == ".gif") $type = "GIF"; return $type; }//end function getFileType(); function load ($filename){ if(!file_exists($filename)){ $this->logError("load() failed! file '$filename' not found!"); return false; } if(!is_file($filename)){ $this->logError("load() failed! '$filename' is not a file!"); return false; } $type = $this->getFileType($filename); if(!$type){ $this->logError("load() failed! '$filename': unsupported filetype!"); return false; } $image = false; switch($type){ case "JPEG": $image = @imageCreateFromJpeg($filename); break; case "GIF": $image = @imageCreateFromGif($filename); break; case "PNG": $image = @imageCreateFromPng($filename); break; default: $this->logError("load() failed! '$filename': unsupported filetype!"); return false; } if(!$image){ $this->logError("load() failed! image from '$filename' could not be created!"); return false; } $this->setImage($image); return $image; }//end function load(); function save ($filename,$jpgquality=false){ if(!$this->image){ $this->logError("save() failed! no image to save!"); return false; } $type = $this->getFileType($filename); if(!$type){ $this->logError("save() failed! '$filename': unsupported filetype!"); return false; } if($jpgquality === false){ $jpgquality = $this->getConfig("jpegdefaultquality"); } if($jpgquality < 0) $jpgquality = 0; if($jpgquality > 100) $jpgquality = 100; switch($type){ case "JPEG": imageJpeg($this->image,$filename,$jpgquality); break; case "GIF": $newfilename = substr($filename,0,strlen($filename)-4).".png"; imagePng($this->image,$newfilename); break; case "PNG": imagePng($this->image,$filename); break; default: $this->logError("save() failed! '$filename': unsupported filetype!"); return false; } return true; }//end function saveImage(); function output ($sendheader=true,$type=false,$jpgquality=false){ if(!$this->image){ $this->logError("output() failed! no image to output!"); return false; } if($type === false){ $type = $this->getConfig("outputdefaulttype"); } if($jpgquality === false){ $jpgquality = $this->getConfig("jpegdefaultquality"); } if($jpgquality < 0) $jpgquality = 0; if($jpgquality > 95) $jpgquality = 95; switch($type){ case "JPEG": if($sendheader){ header("Content-Type: image/jpeg"); } imageJpeg($this->image,"",$jpgquality); break; case "GIF": if($sendheader){ header("Content-Type: image/png"); } imagePng($this->image); break; case "PNG": if($sendheader){ header("Content-Type: image/png"); } imagePng($this->image); break; default: $this->logError("output() failed! type '$type' is not supported!"); return false; } return true; }//end function output(); function cachedOutput ($callback="",$parameterstring="",$newexpirydate=false,$type=false,$jpgquality=false,$forcenew=false){ if($callback != ""){ if(!function_exists($callback)){ $this->logError("cachedOutput() failed! no callback function specified!"); return false; } } if(!$this->image){ $this->logError("cachedOutput() failed! no image to output!"); return false; } if($type === false){ $type = $this->getConfig("outputdefaulttype"); } if(!isset($this->filetypes[$type]["suffix"])){ $suffix = $type; }else{ $suffix = $this->filetypes[$type]["suffix"]; } if($jpgquality === false){ $jpgquality = $this->getConfig("jpegdefaultquality"); } if($jpgquality < 0) $jpgquality = 0; if($jpgquality > 95) $jpgquality = 95; $cimg_filename = "dicf_".md5($parameterstring)."_.".$suffix; $info_filename = "dici_".md5($parameterstring)."_.dat"; $cachedir = $this->getConfig("cachedirectory"); if($cachedir != ""){ if(!@file_exists($cachedir)){ $this->logError("cachedOutput() failed! cache directory does not exist!"); return false; } if(!is_dir($cachedir)){ $this->logError("cachedOutput() failed! invalid cache directory!"); return false; } if(substr($cachedir,-1) != "/"){ $cachedir .= "/"; } } $full_filename = $cachedir.$cimg_filename; if(!@file_exists($cachedir.$info_filename)){ $forcenew = true; }else{ $infofile = $this->_parse_cacheInfofile($cachedir.$info_filename); if(!$infofile){ $forcenew = true; }else{ if(time() > $infofile["expirytimestamp"]){ $forcenew = true; } } } if(!@file_exists($full_filename) or $forcenew){ if(!$this->save($full_filename,$jpgquality)){ $this->logError("cachedOutput() failed! could not save cache image!"); $this->safeDeleteFile($cachedir.$info_filename); return false; } $infoh = fopen($cachedir.$info_filename,"w"); if(!$infoh){ $this->logError("error in cachedOutput(). could not save cache info file '".$cachedir.$info_filename."'!"); }else{ if($newexpirydate === false){ $newexpirydate = time() + $this->getConfig("validdefaulttime"); } fwrite($infoh,$newexpirydate."\n"); fwrite($infoh,$cimg_filename); fclose($infoh); } } if($callback != ""){ return eval($callback."(\$full_filename);"); }else{ return $full_filename; } }//end function cachedOutput(); function loadFromCache($parameterstring,$suffix){ $cimg_filename = "dicf_".md5($parameterstring)."_.".$suffix; $info_filename = "dici_".md5($parameterstring)."_.dat"; $cachedir = $this->getConfig("cachedirectory"); if($cachedir != ""){ if(!@file_exists($cachedir)){ $this->logError("loadFromCache() failed! cache directory does not exist!"); return false; } if(!is_dir($cachedir)){ $this->logError("loadFromCache() failed! invalid cache directory!"); return false; } if(substr($cachedir,-1) != "/"){ $cachedir .= "/"; } } $full_filename = $cachedir.$cimg_filename; $forcenew = false; if(!@file_exists($cachedir.$info_filename)){ $forcenew = true; }else{ $infofile = $this->_parse_cacheInfofile($cachedir.$info_filename); if(!$infofile){ $forcenew = true; }else{ if(time() > $infofile["expirytimestamp"]){ $forcenew = true; } } } if(!@file_exists($full_filename) or $forcenew){ return false; }else{ $this->load($full_filename); return true; } }//end function loadFromCache(); function safeDeleteFile($filename){ if(@file_exists($filename)){ if(!@unlink($filename)){ $this->logError("safeDeleteFile() failed! could not delete file '$filename'!"); return false; } } return true; }//end function safeDeleteFile(); function cleanupCache($recursive=false,$startdir=false){ if($startdir){ $cachedir = $startdir; }else{ $cachedir = $this->getConfig("cachedirectory"); } if($cachedir != ""){ if(!@file_exists($cachedir)){ $this->logError("cleanupCache() failed! cache directory '".$cachedir."' does not exist!"); return false; } if(!@is_dir($cachedir)){ $this->logError("cleanupCache() failed! invalid cache directory '".$cachedir."'!"); return false; } if(substr($cachedir,-1) != "/"){ $cachedir .= "/"; } }else{ $cachedir = "./"; } return $this->_cleanupCache_processDir($cachedir,$recursive); }//end function cleanupCache(); function _cleanupCache_processDir($dir,$recursive){ $dirh = @opendir($dir); if(!$dirh){ $this->logError("_cleanupCache_processDir() failed! could not open cache directory '".$dir."'!"); return false; } while($file = readdir($dirh)){ if($file != "." and $file != ".."){ if(is_dir($dir.$file)){ if($recursive){ $this->_cleanupCache_processDir($dir.$file."/",true); } }else{ $deletefile = false; if(strtolower(substr($file,0,5)) == "dicf_"){ $md5end = strpos($file,"_.",5); if($md5end !== false){ $basefilename = substr($file,5,$md5end-4); $imagefilename = $file; $cachefilename = "dici_".$basefilename.".dat"; if(!@file_exists($dir.$cachefilename)){ $deletefile = true; }else{ $infofile = $this->_parse_cacheInfofile($dir.$cachefilename); if(!$infofile){ $deletefile = true; }else{ if(time() > $infofile["expirytimestamp"]){ $deletefile = true; } } } } } if(strtolower(substr($file,0,5)) == "dici_"){ $md5end = strpos($file,"_.",5); if($md5end !== false){ $basefilename = substr($file,5,$md5end-4); $cachefilename = $file; $infofile = $this->_parse_cacheInfofile($dir.$cachefilename); if($infofile["imagefilename"]){ $imagefilename = $infofile["imagefilename"]; if(!@file_exists($dir.$imagefilename)){ $deletefile = true; } }else{ $deletefile = true; } } } if($deletefile){ $this->safeDeleteFile($dir.$imagefilename); $this->safeDeleteFile($dir.$cachefilename); } } } } return true; }//end function _cleanupCache_processDir(); function _parse_cacheInfofile($filename){ $infoh = @fopen($filename,"r"); if(!$infoh){ $this->logError("error in _parse_cacheInfofile(). could not read cache info file '".$filename."'!"); return false; } $infofile = fread($infoh,filesize($filename)); $lines = explode("\n",$infofile); fclose($infoh); return array("expirytimestamp"=>$lines[0], "imagefilename"=>$lines[1]); }//end function _parse_cacheInfofile(); function scale ($width,$height,$keepproportions=true,$crop=false,$usetruecolor=true,$allowblowup=true){ if(!$this->image){ $this->logError("scale() failed! no image to scale!"); return false; } if(!imageistruecolor($this->image)){ $usetruecolor = false; } $orig_width = imageSx($this->image); $orig_height = imageSy($this->image); if(is_string($width) and substr($width,-1) == "%"){ $scalewidth = substr($width,0,-1); $width = $orig_width * $scalewidth / 100; } if(is_string($height) and substr($height,-1) == "%"){ $scaleheight = substr($height,0,-1); $height = $orig_height * $scaleheight / 100; } $width = abs($width); $height = abs($height); if(!$allowblowup){ if($width > $orig_width) $width = $orig_width; if($height > $orig_height) $height = $orig_height; } if($width == $orig_width and $height == $orig_height) return $this->image; if($keepproportions){ if($crop){ $inv_ratio = $orig_width / $width; $src_width = ceil($width * $inv_ratio); if($src_width > $orig_width){ $src_width = $orig_width; $ratio = $height / $width; $src_height = $src_width * $ratio; } $src_height = ceil($height * $inv_ratio); if($src_height > $orig_height){ $src_height = $orig_height; $ratio = $width / $height; $src_width = $src_height * $ratio; } $dest_width = $width; $dest_height = $height; $new_width = $width; $new_height = $height; }else{ $ratio = $width / $orig_width; if( ceil($orig_height * $ratio) > $height){ $ratio = $height / $orig_height; } $new_width = ceil($orig_width * $ratio); $new_height = ceil($orig_height * $ratio); $src_width = $orig_width; $src_height = $orig_height; $dest_width = $new_width; $dest_height = $new_height; } }else{ $new_width = $width; $new_height = $height; $src_width = $orig_width; $src_height = $orig_height; $dest_width = $width; $dest_height = $height; } if($usetruecolor){ $destimage = imageCreateTruecolor($new_width,$new_height); }else{ $destimage = imageCreate($new_width,$new_height); } if(!$destimage){ $this->logError("scale() failed! couldn't create destination image!"); return false; } if($usetruecolor){ imageCopyResampled($destimage,$this->image,0,0,0,0,$dest_width,$dest_height,$src_width,$src_height); }else{ imageCopyResized($destimage,$this->image,0,0,0,0,$dest_width,$dest_height,$src_width,$src_height); } if($destimage){ $this->setImage($destimage); return $destimage; } return false; }//end function scale(); function convertToTruecolor(){ if(!$this->image){ $this->logError("convertToTruecolor() failed! no image to convert!"); return false; } if(imageistruecolor($this->image)){ return true; } $width = imageSx($this->image); $height = imageSy($this->image); $tmpimage = imageCreateTruecolor($width,$height); if(!$tmpimage){ $this->logError("convertToTruecolor() failed! could not create temporary truecolor image!"); return false; } if(@imageCopy($tmpimage,$this->image,0,0,0,0,$width,$height)){ if(!imageistruecolor($tmpimage)){ $this->logError("convertToTruecolor() failed! temporary image is not truecolor!"); return false; } $this->setImage($tmpimage); }else{ $this->logError("convertToTruecolor() failed! could not copy image!"); return false; } }//end function convertToTruecolor(); function clearErrorLog(){ $this->safeDeleteFile($this->getConfig("logfile")); }//end function clearErrorLog(); function logError($message){ $this->errorlog_num_errors++; if($this->getConfig("logerrors",true)){ $fileh = @fopen($this->getConfig("logfile"),"a"); if($fileh){ if($this->errorlog_num_errors <= 1){ fwrite($fileh,"Log Started: ".date("Y-m-d, H:i:s")."\n"); } fwrite($fileh,$message."\n"); fclose($fileh); } } if($this->getConfig("outputerrorimage")){ if(function_exists("imagettfbbox") and function_exists("imagecreate")){ $m = imagettfbbox (10,0,$this->getConfig("errorimagefontfile"),$message); $width = $m[2]-$m[6]; $height = $m[3]-$m[7]; $errorimage = @imageCreate($width,$height); if($errorimage){ $bcol = imageColorAllocate($errorimage,255,255,255); $tcol = imageColorAllocate($errorimage,0,0,0); imagerectangle($errorimage,0,0,$width,$height,$bcol); imagettftext($errorimage,10,0,0,0,$tcol,$this->getConfig("errorimagefontfile"),$message); header("Content-Type: image/png"); imagePng($errorimage); exit; } } } }//end function logError(); } ?> [:: GSCHAIDER WERBUNG | DRUCKZENTRUM SEEKIRCHEN | WERBER - DRUCKER - PLAKATIERER ::]
User:
Password: