com.lokorin.lokorin.lib
[ class tree: com.lokorin.lokorin.lib ] [ index: com.lokorin.lokorin.lib ] [ all elements ]

Source for file lib_images.php

Documentation is available at lib_images.php

  1. <?php
  2. /*
  3. * Lokorin.com
  4. * Copyright 2004-2006
  5. * Licensed under the GNU LGPL. See COPYING for full terms.
  6. */
  7. /**
  8. * A library that contains everything that has to do with images and loading
  9. * graphical elements.
  10. * @author Andreas Launila
  11. * @version $Revision: 1.11 $
  12. * @package com.lokorin.lokorin.lib
  13. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  14. */
  15.  
  16. /**
  17. * For access to common constants and functions.
  18. */
  19. require_once('common.php');
  20.  
  21. /**
  22. * The default maximum width to allow image to have.
  23. * @var integer
  24. * @access private
  25. */
  26. define('MAX_WIDTH', 500);
  27. /**
  28. * The default maximum file size in kB to allow image to have.
  29. * @var integer
  30. * @access private
  31. */
  32. define('MAX_FILE_SIZE', 40);
  33. /**
  34. * The default width a produced thumbnail image should have.
  35. * @var integer
  36. * @access private
  37. */
  38. define('THUMB_WIDTH', 450);
  39. /**
  40. * The default file size in kB that a produced thumbnail image should aim at.
  41. * @var integer
  42. * @access private
  43. */
  44. define('THUMB_SIZE', 25);
  45.  
  46. /**
  47. * Gets the XHTML code for an image within the defined paramaters. If the
  48. * default source of the specified image does not meet the define parameters
  49. * then the function created an resized version of the image so that does.
  50. * @param string $imageName The name of the image i.e. 'foo.jpg' that should
  51. * be retrived.
  52. * @param string $alt An optional alternative text to be used with the image.
  53. * The text will be displayed if the image can not, for some reason, be
  54. * displayed.
  55. * @param integer $maxWidth The maximum width in pixels that the image is
  56. * allowed to have.
  57. * @param integer $maxSize The maximum size in kilobytes that the image is
  58. * allowed to have.
  59. * @param integer $thumbWidth The width in pixels that a resized image should
  60. * have.
  61. * @param integer $thumbSize The size in kilobytes that a resized image should
  62. * aim for.
  63. * @return string XHTML compliant code representing the specified image within
  64. * the specified parameters.
  65. * @access public
  66. */
  67. function getImage($imageName, $alt = '', $maxWidth = MAX_WIDTH,
  68. $maxSize = MAX_FILE_SIZE, $thumbWidth = THUMB_WIDTH, $thumbSize = THUMB_SIZE) {
  69. $img = array();
  70. $img['path'] = ROOT.FOLDER_IMAGES.$imageName;
  71. if(!file_exists($img['path'])) {
  72. logException(new FileNotFoundException('The image does not exist: ('.
  73. $img['path'].')'));
  74. }
  75. list($img['width'], $img['height']) = getimagesize($img['path']);
  76. $img['size'] = filesize($img['path']);
  77. if($img['width'] > $maxWidth || $img['size'] > $maxSize*1024) {
  78. //We replace it with a thumbnail.
  79. $thumb = array();
  80. $thumb['path'] = FOLDER_THUMBNAILS.preg_replace("/(.*)\..*$/",
  81. "$1_".$maxWidth."_".$thumbWidth."_".$thumbSize.".jpg",basename($img['path']));
  82. if(!file_exists($thumb['path'])) {
  83. //We have to create the thumbnail.
  84. /*
  85. * Check if a resized version of the image exists, if so then
  86. * use the resized version. This is used if the original image
  87. * is too large to be resized (because of memory limits).
  88. */
  89. $imgToResize = preg_replace('/(.*?)(\.[^.]*)$/', '$1.resized$2', $img['path']);
  90. if(!file_exists($imgToResize)) {
  91. //There is no resized version, use the real image instead.
  92. $imgToResize = $img['path'];
  93. }
  94. list($img['width'], $img['height']) = getimagesize($imgToResize);
  95. $thumb['src'] = getImageResource($imgToResize);
  96. if($thumb['src'] === false) {
  97. logException(new IOException('Failed to create a thumbnail for: ('.
  98. $imgToResize.')'));
  99. }
  100. if($img['width'] > $maxWidth) {
  101. //Resize it but keep the proportions
  102. $thumb['height'] = $thumbWidth*($img['height']/$img['width']);
  103. $thumb['width'] = $thumbWidth;
  104. } else {
  105. //No resize needed
  106. $thumb['height'] = $img['height'];
  107. $thumb['width'] = $img['width'];
  108. }
  109. //Resample it
  110. $thumb['dest'] = imagecreatetruecolor($thumb['width'], $thumb['height']);
  111. imagecopyresampled($thumb['dest'], $thumb['src'], 0, 0, 0, 0, $thumb['width'], $thumb['height'], $img['width'], $img['height']);
  112. imageinterlace($thumb['dest'], 1);
  113. touch($thumb['path']);
  114. imagejpeg($thumb['dest'], $thumb['path']);
  115. //Make sure that the thumbnail is below the thumb file size limit
  116. $thumb['size'] = filesize($thumb['path']);
  117. if($thumb['size'] > $thumbSize*1024) {
  118. //It's not small enough, reduce the thumb's quality
  119. //The file size and the quality might not be linearly connected but it should work
  120. //as a decent approximation.
  121. $quality = (int)(75*$thumbSize*1024/$thumb['size']);
  122. imagejpeg($thumb['dest'], $thumb['path'], $quality);
  123. }
  124. }
  125. //The file is now in the cache, use it.
  126. $imageCode = getImageCode($thumb['path'], $alt, 'Click for a larger image');
  127. return '<a href="'.ROOT_PATH.FOLDER_IMAGES.$imageName.'">'.$imageCode.'</a>';
  128. }
  129. return getImageCode($img['path'], $alt);
  130. }
  131.  
  132. /**
  133. * Gets the XHTML code for a specified image.
  134. * @param string $imagePath The path to the image relative to the page root.
  135. * @param string $alt An optional alternative text to be used with the image.
  136. * The text will be displayed if the image can not, for some reason, be
  137. * displayed.
  138. * @param string $title An optional title for the image. The title will show up
  139. * if the user holds the cursor over the image.
  140. * @param string $class An optional css class to use for the image element.
  141. * @return string XHTML compliant code representing the image.
  142. * @access public
  143. */
  144. function getImageCode($imagePath, $alt = '', $title = '', $cssClass = '') {
  145. list($width, $height) = getimagesize(ROOT.$imagePath);
  146. if($title != '') {
  147. $title = ' title="'.htmlspecialchars($title).'"';
  148. }
  149. if($cssClass != '') {
  150. $class = ' class="'.$cssClass.'"';
  151. } else {
  152. $class = '';
  153. }
  154. //<div class="alpha_shadow"><div>...</div></div><br style="clear: both" />
  155. return '<img src="'.ROOT_PATH.$imagePath.'" width="'.$width.
  156. '" height="'.$height.'" '.'alt="'.htmlspecialchars($alt).'"'.
  157. $title.$class.' />';
  158. }
  159.  
  160. /**
  161. * Gets the image resource of the specified image file.
  162. * @param string $path The path to the image file.
  163. * @return imageResource An image resource or a boolean false if something went
  164. * wrong.
  165. * @access private
  166. */
  167. function getImageResource($path) {
  168. if(!file_exists($path)) {
  169. return false;
  170. }
  171. $match = array();
  172. preg_match("/.*\.(.*)$/", $path, $match);
  173. $format = $match[1];
  174. $format = strtolower($format);
  175. if($format == "jpg" || $format == "jpeg") {
  176. return imagecreatefromjpeg($path);
  177. } else if($format == "png") {
  178. return imagecreatefrompng($path);
  179. } else if($format == "gif") {
  180. return imagecreatefromgif($path);
  181. } else if($format == "wbmp") {
  182. return imagecreatefromwbmp($path);
  183. } else {
  184. return false;
  185. }
  186. }
  187. ?>

Documentation generated on Sun, 16 Apr 2006 21:03:34 +0200 by phpDocumentor 1.3.0RC4