|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
Posted: Thu Oct 21, 2004 1:14 pm Post subject: Thumbnail generator Plugin |
|
|
A plugin which generates a thumbnail from the given image.
GD-LIB required!
to generate thumbnails from 'gif's the latest GD-LIB is required!
Its recommened to use this plugin ONLY WITH caching!
[php:1:6c1e0fe493]<?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Smarty {my_html_thumb_image} function plugin
*
* Type: function<br>
* Name: my_html_thumb_image<br>
* Date: Feb 24, 2003<br>
* Purpose: format HTML tags for the image<br>
* Input:<br>
* - 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 src="images/masthead.gif" border=0 width=400 height=23>
* @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 src="'.$file.'" alt="'.$alt.'" border="'.$border.'" width="'.$width.'" height="'.$height.'"'.$extra.' />' . $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: */
?>
[/php:1:6c1e0fe493] |
|
Back to top |
|
McSodbrenner Smarty Pro
Joined: 19 Sep 2004 Posts: 101 Location: Hamburg, Germany
|
Posted: Mon Nov 29, 2004 8:07 pm Post subject: |
|
|
Will the thumbs be saved in the same directory as the orginal files? What about a temporary direcotry?
Btw.: What are those letters for? Is it for a cvs?
Quote: | * @package Smarty
* @subpackage plugins
*/ |
_________________ Grüzi,
Christoph
My sweet home: www.webboarder.de
Do not visit!! It is private!! And it's mine! |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|