- <?php
- /*
- * Lokorin.com
- * Copyright 2004-2006
- * Licensed under the GNU LGPL. See COPYING for full terms.
- */
- /**
- * A library that contains everything that has to do with images and loading
- * graphical elements.
- * @author Andreas Launila
- * @version $Revision: 1.11 $
- * @package com.lokorin.lokorin.lib
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
- */
-
- /**
- * For access to common constants and functions.
- */
- require_once('common.php');
-
- /**
- * The default maximum width to allow image to have.
- * @var integer
- * @access private
- */
- define('MAX_WIDTH', 500);
- /**
- * The default maximum file size in kB to allow image to have.
- * @var integer
- * @access private
- */
- define('MAX_FILE_SIZE', 40);
- /**
- * The default width a produced thumbnail image should have.
- * @var integer
- * @access private
- */
- define('THUMB_WIDTH', 450);
- /**
- * The default file size in kB that a produced thumbnail image should aim at.
- * @var integer
- * @access private
- */
- define('THUMB_SIZE', 25);
-
- /**
- * Gets the XHTML code for an image within the defined paramaters. If the
- * default source of the specified image does not meet the define parameters
- * then the function created an resized version of the image so that does.
- * @param string $imageName The name of the image i.e. 'foo.jpg' that should
- * be retrived.
- * @param string $alt An optional alternative text to be used with the image.
- * The text will be displayed if the image can not, for some reason, be
- * displayed.
- * @param integer $maxWidth The maximum width in pixels that the image is
- * allowed to have.
- * @param integer $maxSize The maximum size in kilobytes that the image is
- * allowed to have.
- * @param integer $thumbWidth The width in pixels that a resized image should
- * have.
- * @param integer $thumbSize The size in kilobytes that a resized image should
- * aim for.
- * @return string XHTML compliant code representing the specified image within
- * the specified parameters.
- * @access public
- */
- function getImage($imageName, $alt = '', $maxWidth = MAX_WIDTH,
- $maxSize = MAX_FILE_SIZE, $thumbWidth = THUMB_WIDTH, $thumbSize = THUMB_SIZE) {
- $img = array();
- $img['path'] = ROOT.FOLDER_IMAGES.$imageName;
- if(!file_exists($img['path'])) {
- logException(new FileNotFoundException('The image does not exist: ('.
- $img['path'].')'));
- }
- list($img['width'], $img['height']) = getimagesize($img['path']);
- $img['size'] = filesize($img['path']);
-
- if($img['width'] > $maxWidth || $img['size'] > $maxSize*1024) {
- //We replace it with a thumbnail.
- $thumb = array();
- $thumb['path'] = FOLDER_THUMBNAILS.preg_replace("/(.*)\..*$/",
- "$1_".$maxWidth."_".$thumbWidth."_".$thumbSize.".jpg",basename($img['path']));
- if(!file_exists($thumb['path'])) {
- //We have to create the thumbnail.
- /*
- * Check if a resized version of the image exists, if so then
- * use the resized version. This is used if the original image
- * is too large to be resized (because of memory limits).
- */
- $imgToResize = preg_replace('/(.*?)(\.[^.]*)$/', '$1.resized$2', $img['path']);
- if(!file_exists($imgToResize)) {
- //There is no resized version, use the real image instead.
- $imgToResize = $img['path'];
- }
- list($img['width'], $img['height']) = getimagesize($imgToResize);
- $thumb['src'] = getImageResource($imgToResize);
- if($thumb['src'] === false) {
- logException(new IOException('Failed to create a thumbnail for: ('.
- $imgToResize.')'));
- }
- if($img['width'] > $maxWidth) {
- //Resize it but keep the proportions
- $thumb['height'] = $thumbWidth*($img['height']/$img['width']);
- $thumb['width'] = $thumbWidth;
- } else {
- //No resize needed
- $thumb['height'] = $img['height'];
- $thumb['width'] = $img['width'];
- }
- //Resample it
- $thumb['dest'] = imagecreatetruecolor($thumb['width'], $thumb['height']);
- imagecopyresampled($thumb['dest'], $thumb['src'], 0, 0, 0, 0, $thumb['width'], $thumb['height'], $img['width'], $img['height']);
- imageinterlace($thumb['dest'], 1);
- touch($thumb['path']);
- imagejpeg($thumb['dest'], $thumb['path']);
- //Make sure that the thumbnail is below the thumb file size limit
- $thumb['size'] = filesize($thumb['path']);
- if($thumb['size'] > $thumbSize*1024) {
- //It's not small enough, reduce the thumb's quality
- //The file size and the quality might not be linearly connected but it should work
- //as a decent approximation.
- $quality = (int)(75*$thumbSize*1024/$thumb['size']);
- imagejpeg($thumb['dest'], $thumb['path'], $quality);
- }
- }
- //The file is now in the cache, use it.
- $imageCode = getImageCode($thumb['path'], $alt, 'Click for a larger image');
- return '<a href="'.ROOT_PATH.FOLDER_IMAGES.$imageName.'">'.$imageCode.'</a>';
- }
- return getImageCode($img['path'], $alt);
- }
-
- /**
- * Gets the XHTML code for a specified image.
- * @param string $imagePath The path to the image relative to the page root.
- * @param string $alt An optional alternative text to be used with the image.
- * The text will be displayed if the image can not, for some reason, be
- * displayed.
- * @param string $title An optional title for the image. The title will show up
- * if the user holds the cursor over the image.
- * @param string $class An optional css class to use for the image element.
- * @return string XHTML compliant code representing the image.
- * @access public
- */
- function getImageCode($imagePath, $alt = '', $title = '', $cssClass = '') {
- list($width, $height) = getimagesize(ROOT.$imagePath);
- if($title != '') {
- $title = ' title="'.htmlspecialchars($title).'"';
- }
- if($cssClass != '') {
- $class = ' class="'.$cssClass.'"';
- } else {
- $class = '';
- }
- //<div class="alpha_shadow"><div>...</div></div><br style="clear: both" />
- return '<img src="'.ROOT_PATH.$imagePath.'" width="'.$width.
- '" height="'.$height.'" '.'alt="'.htmlspecialchars($alt).'"'.
- $title.$class.' />';
- }
-
- /**
- * Gets the image resource of the specified image file.
- * @param string $path The path to the image file.
- * @return imageResource An image resource or a boolean false if something went
- * wrong.
- * @access private
- */
- function getImageResource($path) {
- if(!file_exists($path)) {
- return false;
- }
-
- $match = array();
- preg_match("/.*\.(.*)$/", $path, $match);
- $format = $match[1];
- $format = strtolower($format);
- if($format == "jpg" || $format == "jpeg") {
- return imagecreatefromjpeg($path);
- } else if($format == "png") {
- return imagecreatefrompng($path);
- } else if($format == "gif") {
- return imagecreatefromgif($path);
- } else if($format == "wbmp") {
- return imagecreatefromwbmp($path);
- } else {
- return false;
- }
- }
- ?>