PageRenderTime 25ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/vendors/resizeimage.php

http://github.com/plusglobal/BrowniePHP
PHP | 243 lines | 190 code | 32 blank | 21 comment | 42 complexity | 7d952db43781633c064acc9693f65bec MD5 | raw file
  1. <?php
  2. /**
  3. * helped by http://mediumexposure.com/smart-image-resizing-while-preserving-transparency-php-and-gd-library/
  4. */
  5. function resizeImage($file, $sizes, $quality = 95) {
  6. if (!is_file($file)) {
  7. return false;
  8. }
  9. $s = explode('x', $sizes);
  10. if(count($s == 2) and ctype_digit($s[0]) and ctype_digit($s[1])) {
  11. $newWidth = $s[0];
  12. $newHeight = $s[1];
  13. $crop = 'resizeCrop';
  14. } else {
  15. $s = explode('_', $sizes);
  16. if (count($s == 2) and ctype_digit($s[0]) and ctype_digit($s[1])) {
  17. $newWidth = $s[0];
  18. $newHeight = $s[1];
  19. $crop = 'resize';
  20. } else {
  21. return false;
  22. }
  23. }
  24. list($oldWidth, $oldHeight, $type) = getimagesize($file);
  25. $ext = image_type_to_extension($type, false);
  26. if ($crop == 'resize') {
  27. if ($oldWidth < $newWidth) {
  28. $newWidth = $oldWidth;
  29. }
  30. if ($oldHeight < $newHeight) {
  31. $newHeight = $oldHeight;
  32. }
  33. }
  34. switch ($crop) {
  35. case 'resize':
  36. //Maintains the aspect ratio of the image and makes sure that it fits
  37. //within the maxW(newWidth) and maxH(newHeight) (thus some side will be smaller)
  38. $widthScale = $heightScale = 2;
  39. if ($newWidth) {
  40. $widthScale = $newWidth / $oldWidth;
  41. }
  42. if ($newHeight) {
  43. $heightScale = $newHeight / $oldHeight;
  44. }
  45. if ($widthScale < $heightScale) {
  46. $maxWidth = $newWidth;
  47. $maxHeight = false;
  48. } elseif ($widthScale > $heightScale ) {
  49. $maxHeight = $newHeight;
  50. $maxWidth = false;
  51. } else {
  52. $maxHeight = $newHeight;
  53. $maxWidth = $newWidth;
  54. }
  55. if ($maxWidth > $maxHeight) {
  56. $applyWidth = $maxWidth;
  57. $applyHeight = ($oldHeight*$applyWidth)/$oldWidth;
  58. } elseif ($maxHeight > $maxWidth) {
  59. $applyHeight = $maxHeight;
  60. $applyWidth = ($applyHeight*$oldWidth)/$oldHeight;
  61. } else {
  62. $applyWidth = $maxWidth;
  63. $applyHeight = $maxHeight;
  64. }
  65. $startX = $startY = 0;
  66. break;
  67. case 'resizeCrop':
  68. //resize to max, then crop to center
  69. $ratioX = $newWidth / $oldWidth;
  70. $ratioY = $newHeight / $oldHeight;
  71. if ($ratioX < $ratioY) {
  72. $startX = round(($oldWidth - ($newWidth / $ratioY))/2);
  73. $startY = 0;
  74. $oldWidth = round($newWidth / $ratioY);
  75. $oldHeight = $oldHeight;
  76. } else {
  77. $startX = 0;
  78. $startY = round(($oldHeight - ($newHeight / $ratioX))/2);
  79. $oldWidth = $oldWidth;
  80. $oldHeight = round($newHeight / $ratioX);
  81. }
  82. $applyWidth = $newWidth;
  83. $applyHeight = $newHeight;
  84. break;
  85. /*case 'crop':
  86. //a straight centered crop
  87. $startY = ($oldHeight - $newHeight)/2;
  88. $startX = ($oldWidth - $newWidth)/2;
  89. $oldHeight = $newHeight;
  90. $applyHeight = $newHeight;
  91. $oldWidth = $newWidth;
  92. $applyWidth = $newWidth;
  93. break;*/
  94. }
  95. switch ($ext) {
  96. case 'gif':
  97. $oldImage = imagecreatefromgif($file);
  98. break;
  99. case 'png' :
  100. $oldImage = imagecreatefrompng($file);
  101. break;
  102. case 'jpg': case 'jpeg':
  103. $oldImage = imagecreatefromjpeg($file);
  104. break;
  105. default:
  106. return false;
  107. break;
  108. }
  109. //create new image
  110. if($ext == 'gif') {
  111. $newImage = imagecreate($applyWidth, $applyHeight);
  112. imagetruecolortopalette($newImage, true, imagecolorstotal($oldImage));
  113. } else {
  114. $newImage = imagecreatetruecolor($applyWidth, $applyHeight);
  115. }
  116. //maintain transparency to gif and png
  117. if ($ext == 'gif' and $trnprt_indx = imagecolortransparent($oldImage) and $trnprt_indx != -1) {
  118. $trnprt_color = imagecolorsforindex($oldImage, $trnprt_indx); // Get the original image's transparent color's RGB values
  119. $trnprt_indx = imagecolorallocate($newImage, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); // Allocate the same color in the new image resource
  120. imagefill($newImage, 0, 0, $trnprt_indx); // Completely fill the background of the new image with allocated color.
  121. imagecolortransparent($newImage, $trnprt_indx); // Set the background color for new image to transparent
  122. } elseif ($ext == 'png') {
  123. imagealphablending($newImage, false);
  124. $color = imagecolorallocatealpha($newImage, 0, 0, 0, 127); // Create a new transparent color for image
  125. imagefill($newImage, 0, 0, $color); // Completely fill the background of the new image with allocated color.
  126. imagesavealpha($newImage, true); // Restore transparency blending
  127. }
  128. //put old image on top of new image
  129. imagecopyresampled($newImage, $oldImage, 0, 0, $startX, $startY, $applyWidth, $applyHeight, $oldWidth, $oldHeight);
  130. //save the image to disk
  131. $saved = false;
  132. switch ($ext) {
  133. case 'gif':
  134. $saved = imagegif($newImage, $file, $quality);
  135. break;
  136. case 'png':
  137. $saved = imagepng($newImage, $file, 9);
  138. break;
  139. case 'jpg': case 'jpeg':
  140. $saved = imagejpeg($newImage, $file, $quality);
  141. break;
  142. default:
  143. return false;
  144. break;
  145. }
  146. imagedestroy($newImage);
  147. imagedestroy($oldImage);
  148. return $saved;
  149. }
  150. function is_animated_gif($filename)
  151. {
  152. $filecontents = file_get_contents($filename);
  153. $str_loc = 0;
  154. $count = 0;
  155. while ($count < 2){ // There is no point in continuing after we find a 2nd frame
  156. $where1=strpos($filecontents,"\x00\x21\xF9\x04",$str_loc);
  157. if ($where1 === FALSE) break;
  158. else{
  159. $str_loc=$where1+1;
  160. $where2=strpos($filecontents,"\x00\x2C",$str_loc);
  161. if ($where2 === FALSE) break;
  162. else{
  163. if ($where1+8 == $where2) $count++;
  164. $str_loc=$where2+1;
  165. }
  166. }
  167. }
  168. if ($count > 1) return true;
  169. else return false;
  170. }
  171. if ( !function_exists('image_type_to_extension') ) {
  172. function image_type_to_extension ($type, $dot = true)
  173. {
  174. $e = array ( 1 => 'gif', 'jpeg', 'png', 'swf', 'psd', 'bmp',
  175. 'tiff', 'tiff', 'jpc', 'jp2', 'jpf', 'jb2', 'swc',
  176. 'aiff', 'wbmp', 'xbm');
  177. // We are expecting an integer.
  178. $type = (int)$type;
  179. if (!$type) {
  180. trigger_error( '...come up with an error here...', E_USER_NOTICE );
  181. return null;
  182. }
  183. if ( !isset($e[$type]) ) {
  184. trigger_error( '...come up with an error here...', E_USER_NOTICE );
  185. return null;
  186. }
  187. return ($dot ? '.' : '') . $e[$type];
  188. }
  189. }
  190. if ( !function_exists('image_type_to_mime_type') ) {
  191. function image_type_to_mime_type ($type)
  192. {
  193. $m = array ( 1 => 'image/gif', 'image/jpeg', 'image/png',
  194. 'application/x-shockwave-flash', 'image/psd', 'image/bmp',
  195. 'image/tiff', 'image/tiff', 'application/octet-stream',
  196. 'image/jp2', 'application/octet-stream', 'application/octet-stream',
  197. 'application/x-shockwave-flash', 'image/iff', 'image/vnd.wap.wbmp', 'image/xbm');
  198. // We are expecting an integer.
  199. $type = (int)$type;
  200. if (!$type) {
  201. trigger_error( '...come up with an error here...', E_USER_NOTICE );
  202. return null;
  203. }
  204. if ( !isset($m[$type]) ) {
  205. trigger_error( '...come up with an error here...', E_USER_NOTICE );
  206. return null;
  207. }
  208. return $m[$type];
  209. }
  210. }
  211. ?>