Guest User

Untitled

a guest
May 24th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. <?php
  2. function smart_resize_image( $file, $width = 0, $height = 0, $proportional = false, $output = 'file', $delete_original = true, $use_linux_commands = false )
  3. {
  4. if ( $height <= 0 && $width <= 0 ) {
  5. return false;
  6. }
  7.  
  8.  
  9. $info = getimagesize($file);
  10. $image = '';
  11.  
  12.  
  13. $final_width = 0;
  14. $final_height = 0;
  15. list($width_old, $height_old) = $info;
  16.  
  17. if (
  18. $proportional) {
  19. if ($width == 0) $factor = $height/$height_old;
  20. elseif ($height == 0) $factor = $width/$width_old;
  21. else $factor = min ( $width / $width_old, $height / $height_old);
  22.  
  23.  
  24. $final_width = round ($width_old * $factor);
  25. $final_height = round ($height_old * $factor);
  26.  
  27. }
  28. else {
  29.  
  30. $final_width = ( $width <= 0 ) ? $width_old : $width;
  31. $final_height = ( $height <= 0 ) ? $height_old : $height;
  32. }
  33.  
  34. switch (
  35. $info[2] ) {
  36. case IMAGETYPE_GIF:
  37. $image = imagecreatefromgif($file);
  38. break;
  39. case IMAGETYPE_JPEG:
  40. $image = imagecreatefromjpeg($file);
  41. break;
  42. case IMAGETYPE_PNG:
  43. $image = imagecreatefrompng($file);
  44. break;
  45. default:
  46. return false;
  47. }
  48.  
  49. $image_resized = imagecreatetruecolor( $final_width, $final_height );
  50.  
  51. if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
  52. $trnprt_indx = imagecolortransparent($image);
  53.  
  54. // If we have a specific transparent color
  55. if ($trnprt_indx >= 0) {
  56.  
  57. // Get the original image's transparent color's RGB values
  58. $trnprt_color = imagecolorsforindex($image, $trnprt_indx);
  59.  
  60. // Allocate the same color in the new image resource
  61. $trnprt_indx = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
  62.  
  63. // Completely fill the background of the new image with allocated color.
  64. imagefill($image_resized, 0, 0, $trnprt_indx);
  65.  
  66. // Set the background color for new image to transparent
  67. imagecolortransparent($image_resized, $trnprt_indx);
  68.  
  69.  
  70. }
  71. // Always make a transparent background color for PNGs that don't have one allocated already
  72. elseif ($info[2] == IMAGETYPE_PNG) {
  73.  
  74. // Turn off transparency blending (temporarily)
  75. imagealphablending($image_resized, false);
  76.  
  77. // Create a new transparent color for image
  78. $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
  79.  
  80. // Completely fill the background of the new image with allocated color.
  81. imagefill($image_resized, 0, 0, $color);
  82.  
  83. // Restore transparency blending
  84. imagesavealpha($image_resized, true);
  85. }
  86. }
  87.  
  88.  
  89. imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);
  90.  
  91. if ( $delete_original ) {
  92. if ( $use_linux_commands )
  93. exec('rm '.$file);
  94. else
  95. @unlink($file);
  96. }
  97.  
  98. switch ( strtolower($output) ) {
  99. case 'browser':
  100. $mime = image_type_to_mime_type($info[2]);
  101. header("Content-type: $mime");
  102. $output = NULL;
  103. break;
  104. case 'file':
  105. $output = $file;
  106. break;
  107. case 'return':
  108. return $image_resized;
  109. break;
  110. default:
  111. break;
  112. }
  113.  
  114. switch (
  115. $info[2] ) {
  116. case IMAGETYPE_GIF:
  117. imagegif($image_resized, $output);
  118. break;
  119. case IMAGETYPE_JPEG:
  120. imagejpeg($image_resized, $output,100);
  121. break;
  122. case IMAGETYPE_PNG:
  123. imagepng($image_resized, $output);
  124. break;
  125. default:
  126. return false;
  127. }
  128.  
  129. return
  130. true;
  131. }
  132.  
  133. smart_resize_image('1.jpg', 170,0, true, $output = 'file',false);
  134.  
  135. ?>
Add Comment
Please, Sign In to add comment