Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

resize.class.php 6.2KB

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