PageRenderTime 63ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/includes/modules/ImageResize.class.php

https://github.com/pedramphp/alhussain
PHP | 252 lines | 164 code | 46 blank | 42 comment | 13 complexity | 127e74233aeeb613332d808449a85a5e MD5 | raw file
  1. <?php
  2. # ========================================================================#
  3. #
  4. # Author: Jarrod Oberto
  5. # Version: 1.0
  6. # Date: 17-Jan-10
  7. # Purpose: Resizes and saves image
  8. # Requires : Requires PHP5, GD library.
  9. # Usage Example:
  10. # include("classes/resize_class.php");
  11. # $resizeObj = new ImageResize('images/cars/large/input.jpg');
  12. # $resizeObj -> resizeImage(150, 100, 0);
  13. # $resizeObj -> saveImage('images/cars/large/output.jpg', 100);
  14. #
  15. #
  16. # ========================================================================#
  17. Class ImageResize{
  18. // *** Class variables
  19. private $image;
  20. private $width;
  21. private $height;
  22. private $imageResized;
  23. function __construct($fileName)
  24. {
  25. // *** Open up the file
  26. $this->image = $this->openImage($fileName);
  27. // *** Get width and height
  28. $this->width = imagesx($this->image);
  29. $this->height = imagesy($this->image);
  30. }
  31. ## --------------------------------------------------------
  32. private function openImage($file)
  33. {
  34. // *** Get extension
  35. $extension = strtolower(strrchr($file, '.'));
  36. switch($extension)
  37. {
  38. case '.jpg':
  39. case '.jpeg':
  40. $img = @imagecreatefromjpeg($file);
  41. break;
  42. case '.gif':
  43. $img = @imagecreatefromgif($file);
  44. break;
  45. case '.png':
  46. $img = @imagecreatefrompng($file);
  47. break;
  48. default:
  49. $img = false;
  50. break;
  51. }
  52. return $img;
  53. }
  54. ## --------------------------------------------------------
  55. public function resizeImage($newWidth, $newHeight, $option="auto")
  56. {
  57. // *** Get optimal width and height - based on $option
  58. $optionArray = $this->getDimensions($newWidth, $newHeight, $option);
  59. $optimalWidth = $optionArray['optimalWidth'];
  60. $optimalHeight = $optionArray['optimalHeight'];
  61. // *** Resample - create image canvas of x, y size
  62. $this->imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight);
  63. imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $this->width, $this->height);
  64. // *** if option is 'crop', then crop too
  65. if ($option == 'crop') {
  66. $this->crop($optimalWidth, $optimalHeight, $newWidth, $newHeight);
  67. }
  68. }
  69. ## --------------------------------------------------------
  70. private function getDimensions($newWidth, $newHeight, $option)
  71. {
  72. switch ($option)
  73. {
  74. case 'exact':
  75. $optimalWidth = $newWidth;
  76. $optimalHeight= $newHeight;
  77. break;
  78. case 'portrait':
  79. $optimalWidth = $this->getSizeByFixedHeight($newHeight);
  80. $optimalHeight= $newHeight;
  81. break;
  82. case 'landscape':
  83. $optimalWidth = $newWidth;
  84. $optimalHeight= $this->getSizeByFixedWidth($newWidth);
  85. break;
  86. case 'auto':
  87. $optionArray = $this->getSizeByAuto($newWidth, $newHeight);
  88. $optimalWidth = $optionArray['optimalWidth'];
  89. $optimalHeight = $optionArray['optimalHeight'];
  90. break;
  91. case 'crop':
  92. $optionArray = $this->getOptimalCrop($newWidth, $newHeight);
  93. $optimalWidth = $optionArray['optimalWidth'];
  94. $optimalHeight = $optionArray['optimalHeight'];
  95. break;
  96. }
  97. return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
  98. }
  99. ## --------------------------------------------------------
  100. private function getSizeByFixedHeight($newHeight)
  101. {
  102. $ratio = $this->width / $this->height;
  103. $newWidth = $newHeight * $ratio;
  104. return $newWidth;
  105. }
  106. private function getSizeByFixedWidth($newWidth)
  107. {
  108. $ratio = $this->height / $this->width;
  109. $newHeight = $newWidth * $ratio;
  110. return $newHeight;
  111. }
  112. private function getSizeByAuto($newWidth, $newHeight)
  113. {
  114. if ($this->height < $this->width)
  115. // *** Image to be resized is wider (landscape)
  116. {
  117. $optimalWidth = $newWidth;
  118. $optimalHeight= $this->getSizeByFixedWidth($newWidth);
  119. }
  120. elseif ($this->height > $this->width)
  121. // *** Image to be resized is taller (portrait)
  122. {
  123. $optimalWidth = $this->getSizeByFixedHeight($newHeight);
  124. $optimalHeight= $newHeight;
  125. }
  126. else
  127. // *** Image to be resizerd is a square
  128. {
  129. if ($newHeight < $newWidth) {
  130. $optimalWidth = $newWidth;
  131. $optimalHeight= $this->getSizeByFixedWidth($newWidth);
  132. } else if ($newHeight > $newWidth) {
  133. $optimalWidth = $this->getSizeByFixedHeight($newHeight);
  134. $optimalHeight= $newHeight;
  135. } else {
  136. // *** Sqaure being resized to a square
  137. $optimalWidth = $newWidth;
  138. $optimalHeight= $newHeight;
  139. }
  140. }
  141. return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
  142. }
  143. ## --------------------------------------------------------
  144. private function getOptimalCrop($newWidth, $newHeight)
  145. {
  146. $heightRatio = $this->height / $newHeight;
  147. $widthRatio = $this->width / $newWidth;
  148. if ($heightRatio < $widthRatio) {
  149. $optimalRatio = $heightRatio;
  150. } else {
  151. $optimalRatio = $widthRatio;
  152. }
  153. $optimalHeight = $this->height / $optimalRatio;
  154. $optimalWidth = $this->width / $optimalRatio;
  155. return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
  156. }
  157. ## --------------------------------------------------------
  158. private function crop($optimalWidth, $optimalHeight, $newWidth, $newHeight)
  159. {
  160. // *** Find center - this will be used for the crop
  161. $cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 );
  162. $cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 );
  163. $crop = $this->imageResized;
  164. //imagedestroy($this->imageResized);
  165. // *** Now crop from center to exact requested size
  166. $this->imageResized = imagecreatetruecolor($newWidth , $newHeight);
  167. imagecopyresampled($this->imageResized, $crop , 0, 0, $cropStartX, $cropStartY, $newWidth, $newHeight , $newWidth, $newHeight);
  168. }
  169. ## --------------------------------------------------------
  170. public function saveImage($savePath, $imageQuality="100")
  171. {
  172. // *** Get extension
  173. $extension = strrchr($savePath, '.');
  174. $extension = strtolower($extension);
  175. switch($extension)
  176. {
  177. case '.jpg':
  178. case '.jpeg':
  179. if (imagetypes() & IMG_JPG) {
  180. imagejpeg($this->imageResized, $savePath, $imageQuality);
  181. }
  182. break;
  183. case '.gif':
  184. if (imagetypes() & IMG_GIF) {
  185. imagegif($this->imageResized, $savePath);
  186. }
  187. break;
  188. case '.png':
  189. // *** Scale quality from 0-100 to 0-9
  190. $scaleQuality = round(($imageQuality/100) * 9);
  191. // *** Invert quality setting as 0 is best, not 9
  192. $invertScaleQuality = 9 - $scaleQuality;
  193. if (imagetypes() & IMG_PNG) {
  194. imagepng($this->imageResized, $savePath, $invertScaleQuality);
  195. }
  196. break;
  197. // ... etc
  198. default:
  199. // *** No extension - No save.
  200. break;
  201. }
  202. imagedestroy($this->imageResized);
  203. }
  204. ## --------------------------------------------------------
  205. }
  206. ?>