/library/classes/O_Image/class.O_Image.php

https://github.com/janislankovskis/OwlSite · PHP · 298 lines · 171 code · 56 blank · 71 comment · 43 complexity · c52c505065e3628b54ff1890bc841b3d MD5 · raw file

  1. <?php
  2. /**
  3. * @category OwlSite
  4. * @package OwlSite Library
  5. *
  6. * @version 1.1
  7. */
  8. class O_Image
  9. {
  10. /**
  11. * @category OwlSite
  12. * @package OwlSite Library
  13. * @subpackage O_Image
  14. *
  15. *
  16. * Used to resize, crop, cache, dispaly images on the fly
  17. * Main functionality: O_Image::LoadImage($params);
  18. */
  19. public static function LoadImage($params=array())
  20. {
  21. /**
  22. * @category OwlSite
  23. * @package OwlSite Library
  24. * @subpackage O_Image
  25. *
  26. * @returns string OR fasle
  27. *
  28. * Used for returning formated img tag ex <img src="path/to/cached/image.jpg" alt="image alt text" widht="int" height="int" />
  29. * or just url.
  30. *
  31. * $params = array(
  32. * 'file' => string,
  33. * 'from' => string, (folder name in ROOT /content/[folder_name] )
  34. * 'width' => int, [optional] (target width)
  35. * 'height' => int, [optional] (target height)
  36. * 'crop' => bool, [optional] (default false, if true, resizes and crops image to desired width and height dimensions)
  37. * 'alt' => string, [optional] (default is empty)
  38. * 'urlonly' => bool, [optional] (default false, if true, returns only image url ex. "path/to/your/image.jpg" )
  39. * 'ar' => bool, [optional] (default true, if false, aspect ratio of image is ignored )
  40. * 'quality' => int (1-100), [optional] (default 80, sets image quality while generating one)
  41. * );
  42. *
  43. */
  44. global $project;
  45. $params['quality'] = 80;
  46. extract($params);
  47. if(!isset($file) || $file=='')
  48. {
  49. return false;
  50. }
  51. if(!isset($alt))
  52. {
  53. $alt = '';
  54. }
  55. $path = PATH . 'content/' . $from . '/';
  56. $cache_path = PATH . 'cache/images/';
  57. $name = substr($file, 0, strrpos($file,'.'));
  58. $ext = strtolower(substr($file, strrpos($file,'.')));
  59. if(!file_exists($path. $file))
  60. {
  61. return false;
  62. }
  63. $images = array('.jpg', '.jpeg', '.gif', '.bmp', '.png');
  64. if(in_array(strtolower($ext), $images) )
  65. {
  66. if(isset($ar))
  67. {
  68. $strech = 1;
  69. }
  70. else
  71. {
  72. $strech = 0;
  73. }
  74. if(!isset($width))
  75. {
  76. $width = '';
  77. }
  78. if(!isset($height))
  79. {
  80. $height = '';
  81. }
  82. if(isset($crop)&&$crop==true)
  83. {
  84. $crop = 'crop';
  85. }
  86. else
  87. {
  88. $crop = 'nocrop';
  89. }
  90. $fileName = $from . '_' . sha1($name . $width . $height . $strech . $crop . $quality) . $ext;
  91. /* got static server? */
  92. if(isset($project['static_content_server']) && $project['static_content_server']!='')
  93. {
  94. $imageurl = $project['static_content_server'] . 'images/' . $fileName;
  95. }
  96. else
  97. {
  98. $imageurl = WWW . 'cache/images/' . $fileName;
  99. }
  100. if(!file_exists($cache_path . $fileName))
  101. {
  102. $params['fileName'] = $fileName;
  103. self::GenerateThumbnail($params);
  104. }
  105. if(isset($params['urlonly']) && $params['urlonly'] == true)
  106. {
  107. return $imageurl;
  108. }
  109. if(!file_exists($cache_path . $fileName))
  110. {
  111. return false;
  112. }
  113. $geo = getimagesize($cache_path . $fileName);
  114. if(!isset($addClass))
  115. {
  116. $addClass = '';
  117. }
  118. $tag = '<img class="' . $addClass . '" src="' . $imageurl . '" alt="'.$alt.'" width="'.$geo[0].'" height="'.$geo[1].'" />';
  119. return $tag;
  120. }
  121. else
  122. {
  123. return false;
  124. }
  125. }
  126. public static function GenerateThumbnail($params=array())
  127. {
  128. /**
  129. * @category OwlSite
  130. * @package OwlSite Library
  131. * @subpackage O_Image
  132. *
  133. *
  134. * Used to resize, crop images
  135. * Main functionality: O_Image::LoadImage($params);
  136. * @returns bool
  137. *
  138. */
  139. $path = PATH . '/content/' . $params['from'] . '/';
  140. $cache_path = PATH . 'cache/images/';
  141. $limitedext = array(".gif",".jpg",".jpeg",".bmp");
  142. $ext = strrchr($params['file'],'.');
  143. $ext = strtolower($ext);
  144. $fileName = $params['fileName'];
  145. if(!in_array($ext, $limitedext))
  146. {
  147. return;
  148. }
  149. $filePath = $path . $params['file'];
  150. if(!extension_loaded("imagick"))
  151. {
  152. //fallback to phpThumb
  153. $params['target'] = $cache_path . $params['fileName'];
  154. return self::GeneratePhpThumb($params);
  155. }
  156. $img = new Imagick($filePath);
  157. if(!$img) { return false; }
  158. $img->setImageCompressionQuality($params['quality']);
  159. if(!isset($params['width']))
  160. {
  161. $params['width'] = 0;
  162. }
  163. if(!isset($params['height']))
  164. {
  165. $params['height'] = 0;
  166. }
  167. if($params['width']!=0 && $params['height']!=0 && !(isset($params['crop']) && $params['crop']==true))
  168. {
  169. //scale
  170. if(!isset($params['ar']))
  171. {
  172. $params['ar'] = 1;
  173. }
  174. $img->scaleImage($params['width'], $params['height'], $params['ar']);
  175. }
  176. elseif(isset($params['crop']) && $params['crop'] == true && ($params['width']!=0 || $params['height']!=0) )
  177. {
  178. //crop
  179. $img->cropThumbnailImage($params['width'], $params['height']);
  180. }
  181. $img->writeImage($cache_path . $fileName);
  182. return true;
  183. }
  184. public static function GeneratePhpThumb($params)
  185. {
  186. /**
  187. * @category OwlSite
  188. * @package OwlSite Library
  189. * @subpackage O_Image
  190. *
  191. *
  192. * Used for Imagick fallback
  193. * uses phpThumb - http://phpthumb.sourceforge.net/
  194. */
  195. require_once( PATH . 'library/3rdpart/phpThumb/phpthumb.class.php');
  196. $phpThumb = new phpThumb();
  197. $phpThumb->setSourceFilename(PATH . '/content/' . $params['from'] . '/' . $params['file']);
  198. $phpThumb->setParameter('q', $params['quality']);
  199. $phpThumb->setParameter('w', $params['width']);
  200. $phpThumb->setParameter('h', $params['height']);
  201. if(isset($params['ar']) && $params['ar'] == false){
  202. $phpThumb->setParameter('iar', '1');
  203. }
  204. if(isset($params['crop']) && $params['crop']){
  205. $phpThumb->setParameter('zc', '1');
  206. }
  207. if($phpThumb->GenerateThumbnail()){
  208. if ($phpThumb->RenderToFile($params['target'])) {
  209. return true;
  210. }
  211. }
  212. return false;
  213. }
  214. public static function Resize($params = array())
  215. {
  216. /**
  217. * @category OwlSite
  218. * @package OwlSite Library
  219. * @subpackage O_Image
  220. *
  221. *
  222. * Used for resizing in-place. without copying to cache.
  223. * takes 3 params - width, height, file [path-to-file]
  224. */
  225. if(!isset($params['width']) || !isset($params['height']) || !isset($params['file']) )
  226. {
  227. return false;
  228. }
  229. require_once( PATH . 'library/3rdpart/phpThumb/phpthumb.class.php');
  230. $phpThumb = new phpThumb();
  231. $phpThumb->setSourceFilename($params['file']);
  232. $phpThumb->setParameter('w', $params['width']);
  233. $phpThumb->setParameter('h', $params['height']);
  234. if($phpThumb->GenerateThumbnail()){
  235. if ($phpThumb->RenderToFile($params['file'])) {
  236. return true;
  237. }
  238. }
  239. return false;
  240. }
  241. }
  242. ?>