Guest User

Untitled

a guest
Oct 16th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.33 KB | None | 0 0
  1. <?php
  2.  
  3. class Image
  4. {
  5. const MaxWH = 2048;
  6.  
  7. private static $ext;
  8. private static $image;
  9. private static $newImage;
  10. private static $origWidth;
  11. private static $origHeight;
  12. private static $resizeWidth;
  13. private static $resizeHeight;
  14. private static $newWidth;
  15. private static $newHeight;
  16.  
  17. public static function thin($path, $wAh = [])
  18. {
  19. if (!file_exists($path)) {
  20. return false;
  21. }
  22.  
  23. list($dirname, $basename, $extension, $filename) = array_values(pathinfo($path));
  24.  
  25. $size = getimagesize($path);
  26. self::$ext = $size['mime'];
  27.  
  28. self::detectType($path);
  29.  
  30. self::$origWidth = imagesx(self::$image);
  31. self::$origHeight = imagesy(self::$image);
  32.  
  33. self::resizeTo($wAh);
  34.  
  35. if ($wAh['width'] || $wAh['height']) {
  36. $dir = $dirname . '/' . self::$resizeWidth . 'x' . self::$resizeHeight . '/';
  37. @mkdir($dir, 0777, true);
  38. } else {
  39. $dir = $dirname . '/';
  40. }
  41.  
  42. if (self::save($dir . $basename, 85)) {
  43. return true;
  44. } else {
  45. return false;
  46. }
  47. }
  48.  
  49. private static function detectType($path)
  50. {
  51. switch (self::$ext) {
  52. case 'image/jpg':
  53. case 'image/jpeg':
  54. self::$image = imagecreatefromjpeg($path);
  55. break;
  56. case 'image/gif':
  57. self::$image = @imagecreatefromgif($path);
  58. break;
  59. case 'image/png':
  60. self::$image = @imagecreatefrompng($path);
  61. break;
  62. default:
  63. return false;
  64. }
  65. }
  66.  
  67. private static function resizeTo($args)
  68. {
  69. $width = self::detectWaH($args['width'], self::$origWidth);
  70. $height = self::detectWaH($args['height'], self::$origHeight);
  71. $option = ($args['option']) ?: 'default';
  72.  
  73. $crop_x = 0;
  74. $crop_y = 0;
  75.  
  76. switch (strtolower($option)) {
  77. case 'exact':
  78. self::$resizeWidth = $width;
  79. self::$resizeHeight = $height;
  80.  
  81. $original_aspect = self::$origWidth / self::$origHeight;
  82. $thumb_aspect = $width / $height;
  83.  
  84. if ($original_aspect >= $thumb_aspect) {
  85. $new_width = (self::$origWidth / (self::$origHeight / $height));
  86.  
  87. self::$newWidth = $new_width;
  88. self::$newHeight = $height;
  89. } else {
  90. $new_height = (self::$origHeight / (self::$origWidth / $width));
  91.  
  92. self::$newWidth = $width;
  93. self::$newHeight = $new_height;
  94. }
  95.  
  96. $crop_x = 0 - (self::$newWidth - $width) / 2;
  97. $crop_y = 0 - (self::$newHeight - $height) / 2;
  98. break;
  99.  
  100. case 'maxwidth':
  101. self::$resizeWidth = $width;
  102. self::$resizeHeight = self::resizeHeightByWidth($width);
  103. break;
  104.  
  105. case 'maxheight':
  106. self::$resizeWidth = self::resizeWidthByHeight($height);
  107. self::$resizeHeight = $height;
  108. break;
  109.  
  110. default:
  111. if ((self::$origWidth > $width) || (self::$origHeight > $height)) {
  112. if (self::$origWidth > self::$origHeight) {
  113. self::$resizeHeight = self::resizeHeightByWidth($width);
  114. self::$resizeWidth = $width;
  115. } elseif (self::$origWidth < self::$origHeight) {
  116. self::$resizeWidth = self::resizeWidthByHeight($height);
  117. self::$resizeHeight = $height;
  118. } else {
  119. self::$resizeWidth = $width;
  120. self::$resizeHeight = $height;
  121. }
  122. } else {
  123. self::$resizeWidth = $width;
  124. self::$resizeHeight = $height;
  125. }
  126. break;
  127. }
  128.  
  129. self::$newImage = imagecreatetruecolor(self::$resizeWidth, self::$resizeHeight);
  130.  
  131. imagealphablending(self::$newImage, false);
  132. imagesavealpha(self::$newImage, true);
  133.  
  134. $transparent = imagecolorallocatealpha(self::$newImage, 255, 255, 255, 127);
  135. imagefilledrectangle(self::$newImage, 0, 0, self::$resizeWidth, self::$resizeHeight, $transparent);
  136.  
  137. imagecopyresampled(
  138. self::$newImage,
  139. self::$image,
  140. $crop_x,
  141. $crop_y,
  142. 0,
  143. 0,
  144. (isset(self::$newWidth)) ? self::$newWidth : self::$resizeWidth,
  145. (isset(self::$newHeight)) ? self::$newHeight : self::$resizeHeight,
  146. self::$origWidth,
  147. self::$origHeight
  148. );
  149. }
  150.  
  151. private static function detectWaH($new, $orig)
  152. {
  153. if (!$new) {
  154. if ($orig > self::MaxWH) {
  155. return self::MaxWH;
  156. } else {
  157. return $orig;
  158. }
  159. } else {
  160. if ($new > self::MaxWH) {
  161. return self::MaxWH;
  162. } else {
  163. return $new;
  164. }
  165. }
  166. }
  167.  
  168. private static function resizeHeightByWidth($width)
  169. {
  170. return floor((self::$origHeight / self::$origWidth) * $width);
  171. }
  172.  
  173. private static function resizeWidthByHeight($height)
  174. {
  175. return floor((self::$origWidth / self::$origHeight) * $height);
  176. }
  177.  
  178. private static function save($savePath, $imageQuality = '100')
  179. {
  180. $status = false;
  181.  
  182. switch (self::$ext) {
  183. case 'image/jpg':
  184. case 'image/jpeg':
  185. if (imagetypes() & IMG_JPG) {
  186. imagejpeg(self::$newImage, $savePath, $imageQuality);
  187. $status = true;
  188. }
  189. break;
  190.  
  191. case 'image/gif':
  192. if (imagetypes() & IMG_GIF) {
  193. imagegif(self::$newImage, $savePath);
  194. $status = true;
  195. }
  196. break;
  197.  
  198. case 'image/png':
  199. $invertScaleQuality = 9 - round(($imageQuality / 100) * 9);
  200.  
  201. if (imagetypes() & IMG_PNG) {
  202. imagepng(self::$newImage, $savePath, $invertScaleQuality);
  203. $status = true;
  204. }
  205. break;
  206. }
  207.  
  208. imagedestroy(self::$newImage);
  209.  
  210. return $status;
  211. }
  212. }
Add Comment
Please, Sign In to add comment