PHP-Code:
<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Smarty {my_html_thumb_image} function plugin
*
* Type: function
* Name: my_html_thumb_image
* Date: Feb 24, 2003
* Purpose: format HTML tags for the image
* Input:
* - file = file (and path) of image (required)
* - border = border width (optional, default 0)
* - height = image height (optional, default actual height)
* - image =image width (optional, default actual width)
* - basedir = base directory for absolute paths, default
* is environment variable DOCUMENT_ROOT
*
* Examples: {my_html_thumb_image file="images/masthead.gif"}
* Output: [img]images/masthead.gif[/img]
* @author Markus Staab <kills@t-online.de>
*
* @version 1.0
* @param array
* @param Smarty
* @return string
* @uses smarty_function_escape_special_chars()
*/
function smarty_function_my_html_thumb_image($params, &$smarty)
{
if (!extension_loaded('gd')) {
trigger_error( 'my_html_thumb_image: gd-extension not loaded!');
return;
}
require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
$alt = '';
$file = '';
$border = 0;
$height = '';
$maxheight = 0;
$width = '';
$maxwidth = 0;
$extra = '';
$prefix = '';
$suffix = '';
$server_vars = ($smarty->request_use_auto_globals) ? $_SERVER : $GLOBALS['HTTP_SERVER_VARS'];
$basedir = isset($server_vars['DOCUMENT_ROOT']) ? $server_vars['DOCUMENT_ROOT'] : '';
$thumb_dir = 'images/thumbnails/';
foreach($params as $_key => $_val) {
switch($_key) {
case 'file':
case 'border':
case 'height':
case 'maxheight':
case 'width':
case 'maxwidth':
case 'dpi':
case 'basedir':
$$_key = $_val;
break;
case 'alt':
if(!is_array($_val)) {
$$_key = smarty_function_escape_special_chars($_val);
} else {
$smarty->trigger_error("my_html_thumb_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
}
break;
case 'link':
case 'href':
$prefix = '<a href="' . $_val . '">';
$suffix = '</a>';
break;
default:
if(!is_array($_val)) {
$extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
} else {
$smarty->trigger_error("my_html_thumb_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
}
break;
}
}
if (empty($file)) {
$smarty->trigger_error("my_html_thumb_image: missing 'file' parameter", E_USER_NOTICE);
return;
}
if (substr($file,0,1) == '/') {
$_image_path = $basedir . $file;
} else {
$_image_path = $file;
}
if(!isset($params['width']) && !isset($params['height'])) {
$smarty->trigger_error("my_html_thumb_image: missing 'width' or 'height' parameter", E_USER_ERROR);
return;
}
if(!isset($params['width']) || !isset($params['height'])) {
if ($smarty->security &&
($_params = array('resource_type' => 'file', 'resource_name' => $_image_path)) &&
(require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.is_secure.php')) &&
(!smarty_core_is_secure($_params, $smarty)) ) {
$smarty->trigger_error("my_html_thumb_image: (secure) '$_image_path' not in secure directory", E_USER_ERROR);
}
$oImg = imageFactory :: getImage( $file);
$thumb_dir .= $oImg->getFilePath();
if(!isset($params['width'])) {
$oThumb = $oImg->getThumbnail( $thumb_dir, null, $params['height']);
$width = $oThumb->getWidth();
}
if(!isset($params['height'])) {
$oThumb = $oImg->getThumbnail( $thumb_dir, $params['width'], null);
$height = $oThumb->getHeight();
}
$file = $oThumb->getFile();
unset( $oImg, $oThumb);
}
if(isset($params['dpi'])) {
if(strstr($server_vars['HTTP_USER_AGENT'], 'Mac')) {
$dpi_default = 72;
} else {
$dpi_default = 96;
}
$_resize = $dpi_default/$params['dpi'];
$width = round($width * $_resize);
$height = round($height * $_resize);
}
return $prefix . '[img]'.$file.'[/img]' . $suffix;
}
// Image-Basis-Objekt
class image {
var $sFile;
var $oImage = null;
function Image($sFile) {
$this->sFile = $sFile;
$this->oImage = $this->getResource();
$this->validateFile();
}
function validateFile() {
if ( !$this->getResource()) {
$sFile = $this->getFile();
if( !file_exists( $sFile)) {
trigger_error("unable to find '$sFile'", E_USER_ERROR);
} else if( !is_readable( $sFile)) {
trigger_error("unable to read '$sFile'", E_USER_ERROR);
} else {
trigger_error("'$sFile' is not a valid image file", E_USER_ERROR);
}
}
}
function unsupportedImage() {
trigger_error("Unsupported imageformat! File: '". $this->getFile() ."'", E_USER_ERROR);
}
function getResource() {
$this->unsupportedImage();
}
function getHeight() {
return imagesy($this->getResource());
}
function getWidth() {
return imagesx($this->getResource());
}
function getFile() {
return $this->sFile;
}
function getFileName() {
return substr($this->getFile(), strrpos($this->getFile(), '/') + 1, strrpos($this->getFile(), '.') - 1);
}
function getFilePath() {
return substr($this->getFile(), 0, strrpos($this->getFile(), "/") + 1 );
}
function getFileSuffix() {
return strrchr($this->getFile(), '.');
}
function getImage() {
$this->unsupportedImage();
}
function saveImage() {
$this->unsupportedImage();
}
function getThumbnail($sDestination, $iWidth = null, $iHeight = null) {
if ($sDestination { strlen($sDestination) - 1 } != "/") $sDestination .= "/";
$iImgWidth = $this->getWidth();
$iImgHeight = $this->getHeight();
// Ordnerstruktur anlegen
$this->makedir($sDestination);
if ($iImgHeight > $iImgWidth) {
// Hochkanntbilder proportional verkleinern
$iHeight = $iWidth;
$iWidth = null;
}
if ((is_null($iWidth) && is_null($iHeight)) || ($iImgWidth <= $iWidth && $iImgHeight <= $iHeight)) {
// Weder Höhe noch Breite wurden angegeben oder
// Die Höhe und Breite des Original Images ist schon kleiner als das Thumbnail das erstellt würde
return $this;
} else if (is_null($iWidth)) {
// Breite wurde angegebene, dazu die Höhe errechen
$iWidth = round(($iImgWidth / $iImgHeight) * $iHeight);
} else if (is_null($iHeight)) {
// Höhe wurde angegeben, dazu die Breite errechnen
$iHeight = round(($iImgHeight / $iImgWidth) * $iWidth);
}
$sFileDestination = $sDestination . $this->getFileName() .'_'. $iWidth .'x'. $iHeight . $this->getFileSuffix();
// Wenn schon ein thumbnail vorhanden ist, den Pfad zu diesem zurückgeben
if ( file_exists( $sFileDestination)) {
return imageFactory :: getImage($sFileDestination);
}
$oSrcImg = $this->getResource();
$oThumbnail = imagecreatetruecolor($iWidth, $iHeight);
imagecopyresized($oThumbnail, $oSrcImg, 0, 0, 0, 0, $iWidth, $iHeight, $iImgWidth, $iImgHeight);
$this->saveImage ($oThumbnail, $sFileDestination);
return imageFactory :: getImage($sFileDestination);
}
function makedir($sDir) {
$aDir = explode("/", $sDir);
$sParentDir = "";
foreach ($aDir as $sDirPart) {
if ( $sDirPart == "") continue;
$sCurrentDir = $sParentDir . $sDirPart . "/";
if (!is_dir($sCurrentDir)) {
mkdir($sCurrentDir);
}
$sParentDir = $sCurrentDir;
}
}
}
// GIF-Format Objekt
class imageGif extends image {
function getResource() {
if (is_null($this->oImage)) {
$this->oImage = @imagecreatefromgif($this->getFile());
}
return $this->oImage;
}
function getImage() {
return imagegif($this->getResource(), $this->getFile());
}
function saveImage($oImage, $sDestination) {
imagegif($oImage, $sDestination);
}
}
// JPG-Format Objekt
class imageJpg extends image {
function getResource() {
if (is_null($this->oImage)) {
$this->oImage = @imagecreatefromjpeg($this->getFile());
}
return $this->oImage;
}
function getImage() {
return imagejpeg($this->getResource(), $this->getFile());
}
function saveImage($oImage, $sDestination) {
imagejpeg($oImage, $sDestination);
}
}
// PNG-Format Objekt
class imagePng extends image {
function getResource() {
if (is_null($this->oImage)) {
$this->oImage = @imagecreatefrompng($this->getFile());
}
return $this->oImage;
}
function getImage() {
return imagepng($this->getResource(), $this->getFile());
}
function saveImage( $oImage, $sDestination) {
imagepng($oImage, $sDestination);
}
}
// Factory zum erstellen von Image-Objekten
class imageFactory {
function getImage( $sFileName) {
$sFileType = substr(strrchr($sFileName, "."), 1);
switch ($sFileType) {
case "jpeg" :
case "jpg" : return new imageJpg($sFileName);
case "gif" : return new imageGif($sFileName);
case "png" : return new imagePng($sFileName);
default : return new image($sFileName);
}
}
}
/* vim: set expandtab: */
?>