PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/concreteOLD/helpers/image.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 248 lines | 149 code | 30 blank | 69 comment | 50 complexity | d4fa3a869261a92ac547dc62441b7c11 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Helpers
  4. * @category Concrete
  5. * @author Andrew Embler <andrew@concrete5.org>
  6. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  7. * @license http://www.concrete5.org/license/ MIT License
  8. */
  9. /**
  10. * Functions useful for working with images.
  11. * @package Helpers
  12. * @category Concrete
  13. * @author Andrew Embler <andrew@concrete5.org>
  14. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  15. * @license http://www.concrete5.org/license/ MIT License
  16. * Now includes cropping functionality (thanks to Jordan Lev and Kirk Roberts)
  17. */
  18. defined('C5_EXECUTE') or die("Access Denied.");
  19. class ImageHelper {
  20. /**
  21. * Creates a new image given an original path, a new path, a target width and height.
  22. * Optionally crops image to exactly match given width and height.
  23. * @params string $originalPath, string $newpath, int $width, int $height, bool $crop
  24. * @return void
  25. */
  26. public function create($originalPath, $newPath, $width, $height, $crop = false) {
  27. // first, we grab the original image. We shouldn't ever get to this function unless the image is valid
  28. $imageSize = @getimagesize($originalPath);
  29. $oWidth = $imageSize[0];
  30. $oHeight = $imageSize[1];
  31. $finalWidth = 0; //For cropping, this is really "scale to width before chopping extra height"
  32. $finalHeight = 0; //For cropping, this is really "scale to height before chopping extra width"
  33. $do_crop_x = false;
  34. $do_crop_y = false;
  35. $crop_src_x = 0;
  36. $crop_src_y = 0;
  37. // first, if what we're uploading is actually smaller than width and height, we do nothing
  38. if ($oWidth < $width && $oHeight < $height) {
  39. $finalWidth = $oWidth;
  40. $finalHeight = $oHeight;
  41. $width = $oWidth;
  42. $height = $oHeight;
  43. } else if ($crop && ($height >= $oHeight && $width <= $oWidth)) {
  44. //crop to width only -- don't scale anything
  45. $finalWidth = $oWidth;
  46. $finalHeight = $oHeight;
  47. $height = $oHeight;
  48. $do_crop_x = true;
  49. } else if ($crop && ($width >= $oWidth && $height <= $oHeight)) {
  50. //crop to height only -- don't scale anything
  51. $finalHeight = $oHeight;
  52. $finalWidth = $oWidth;
  53. $width = $oWidth;
  54. $do_crop_y = true;
  55. } else {
  56. // otherwise, we do some complicated stuff
  57. // first, we divide original width and height by new width and height, and find which difference is greater
  58. $wDiff = $oWidth / $width;
  59. $hDiff = ($height != 0 ? $oHeight / $height : 0);
  60. if (!$crop && ($wDiff > $hDiff)) {
  61. //no cropping, just resize down based on target width
  62. $finalWidth = $width;
  63. $finalHeight = ($wDiff != 0 ? $oHeight / $wDiff : 0);
  64. } else if (!$crop) {
  65. //no cropping, just resize down based on target height
  66. $finalWidth = ($hDiff != 0 ? $oWidth / $hDiff : 0);
  67. $finalHeight = $height;
  68. } else if ($crop && ($wDiff > $hDiff)) {
  69. //resize down to target height, THEN crop off extra width
  70. $finalWidth = ($hDiff != 0 ? $oWidth / $hDiff : 0);
  71. $finalHeight = $height;
  72. $do_crop_x = true;
  73. } else if ($crop) {
  74. //resize down to target width, THEN crop off extra height
  75. $finalWidth = $width;
  76. $finalHeight = ($wDiff != 0 ? $oHeight / $wDiff : 0);
  77. $do_crop_y = true;
  78. }
  79. }
  80. //Calculate cropping to center image
  81. if ($do_crop_x) {
  82. /*
  83. //Get half the difference between scaled width and target width,
  84. // and crop by starting the copy that many pixels over from the left side of the source (scaled) image.
  85. $nudge = ($width / 10); //I have *no* idea why the width isn't centering exactly -- this seems to fix it though.
  86. $crop_src_x = ($finalWidth / 2.00) - ($width / 2.00) + $nudge;
  87. */
  88. $crop_src_x = round(($oWidth - ($width * $oHeight / $height)) * 0.5);
  89. }
  90. if ($do_crop_y) {
  91. /*
  92. //Calculate cropping...
  93. //Get half the difference between scaled height and target height,
  94. // and crop by starting the copy that many pixels down from the top of the source (scaled) image.
  95. $crop_src_y = ($finalHeight / 2.00) - ($height / 2.00);
  96. */
  97. $crop_src_y = round(($oHeight - ($height * $oWidth / $width)) * 0.5);
  98. }
  99. //create "canvas" to put new resized and/or cropped image into
  100. if ($crop) {
  101. $image = @imageCreateTrueColor($width, $height);
  102. } else {
  103. $image = @imageCreateTrueColor($finalWidth, $finalHeight);
  104. }
  105. $im = false;
  106. switch($imageSize[2]) {
  107. case IMAGETYPE_GIF:
  108. $im = @imageCreateFromGIF($originalPath);
  109. break;
  110. case IMAGETYPE_JPEG:
  111. $im = @imageCreateFromJPEG($originalPath);
  112. break;
  113. case IMAGETYPE_PNG:
  114. $im = @imageCreateFromPNG($originalPath);
  115. break;
  116. }
  117. if ($im) {
  118. // Better transparency - thanks for the ideas and some code from mediumexposure.com
  119. if (($imageSize[2] == IMAGETYPE_GIF) || ($imageSize[2] == IMAGETYPE_PNG)) {
  120. $trnprt_indx = imagecolortransparent($im);
  121. // If we have a specific transparent color
  122. if ($trnprt_indx >= 0) {
  123. // Get the original image's transparent color's RGB values
  124. $trnprt_color = imagecolorsforindex($im, $trnprt_indx);
  125. // Allocate the same color in the new image resource
  126. $trnprt_indx = imagecolorallocate($image, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
  127. // Completely fill the background of the new image with allocated color.
  128. imagefill($image, 0, 0, $trnprt_indx);
  129. // Set the background color for new image to transparent
  130. imagecolortransparent($image, $trnprt_indx);
  131. } else if ($imageSize[2] == IMAGETYPE_PNG) {
  132. // Turn off transparency blending (temporarily)
  133. imagealphablending($image, false);
  134. // Create a new transparent color for image
  135. $color = imagecolorallocatealpha($image, 0, 0, 0, 127);
  136. // Completely fill the background of the new image with allocated color.
  137. imagefill($image, 0, 0, $color);
  138. // Restore transparency blending
  139. imagesavealpha($image, true);
  140. }
  141. }
  142. $res = @imageCopyResampled($image, $im, 0, 0, $crop_src_x, $crop_src_y, $finalWidth, $finalHeight, $oWidth, $oHeight);
  143. if ($res) {
  144. switch($imageSize[2]) {
  145. case IMAGETYPE_GIF:
  146. $res2 = imageGIF($image, $newPath);
  147. break;
  148. case IMAGETYPE_JPEG:
  149. $compression = defined('AL_THUMBNAIL_JPEG_COMPRESSION') ? AL_THUMBNAIL_JPEG_COMPRESSION : 80;
  150. $res2 = imageJPEG($image, $newPath, $compression);
  151. break;
  152. case IMAGETYPE_PNG:
  153. $res2 = imagePNG($image, $newPath);
  154. break;
  155. }
  156. }
  157. }
  158. @chmod($newPath, FILE_PERMISSIONS_MODE);
  159. }
  160. /**
  161. * Returns a path to the specified item, resized and/or cropped to meet max width and height. $obj can either be
  162. * a string (path) or a file object.
  163. * Returns an object with the following properties: src, width, height
  164. * @param mixed $obj
  165. * @param int $maxWidth
  166. * @param int $maxHeight
  167. * @param bool $crop
  168. */
  169. public function getThumbnail($obj, $maxWidth, $maxHeight, $crop = false) {
  170. if ($obj instanceof File) {
  171. $path = $obj->getPath();
  172. } else {
  173. $path = $obj;
  174. }
  175. $fh = Loader::helper('file');
  176. $prefix = ($crop ? 'cropped:' : ''); //Name cropped images different from resized images so they don't get mixed up in the cache
  177. if (file_exists($path)) {
  178. $filename = md5($prefix . $path . ':' . $maxWidth . ':' . $maxHeight . ':' . filemtime($path)) . '.' . $fh->getExtension($path);
  179. } else {
  180. $filename = md5($prefix . $path . ':' . $maxWidth . ':' . $maxHeight . ':') . '.' . $fh->getExtension($path);
  181. }
  182. if (!file_exists(DIR_FILES_CACHE . '/' . $filename)) {
  183. // create image there
  184. $this->create($path, DIR_FILES_CACHE . '/' . $filename, $maxWidth, $maxHeight, $crop);
  185. }
  186. $src = REL_DIR_FILES_CACHE . '/' . $filename;
  187. $abspath = DIR_FILES_CACHE . '/' . $filename;
  188. $thumb = new stdClass;
  189. if (isset($abspath) && file_exists($abspath)) {
  190. $thumb->src = $src;
  191. $dimensions = getimagesize($abspath);
  192. $thumb->width = $dimensions[0];
  193. $thumb->height = $dimensions[1];
  194. return $thumb;
  195. }
  196. }
  197. /**
  198. * Runs getThumbnail on the path, and then prints it out as an XHTML image
  199. */
  200. public function outputThumbnail($obj, $maxWidth, $maxHeight, $alt = null, $return = false, $crop = false) {
  201. $thumb = $this->getThumbnail($obj, $maxWidth, $maxHeight, $crop);
  202. $html = '<img class="ccm-output-thumbnail" alt="' . $alt . '" src="' . $thumb->src . '" width="' . $thumb->width . '" height="' . $thumb->height . '" />';
  203. if ($return) {
  204. return $html;
  205. } else {
  206. print $html;
  207. }
  208. }
  209. public function output($obj, $alt = null, $return = false) {
  210. $s = @getimagesize($obj->getPath());
  211. $html = '<img class="ccm-output-image" alt="' . $alt . '" src="' . $obj->getRelativePath() . '" width="' . $s[0] . '" height="' . $s[1] . '" />';
  212. if ($return) {
  213. return $html;
  214. } else {
  215. print $html;
  216. }
  217. }
  218. }