Yet another PHP based Funko Pop collection tracker, about as bare bones as you can get, but it's functional.
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.
 
 
 
 

130 lines
5.2 KiB

  1. <?php
  2. /**
  3. * easy image resize function
  4. * @param $file - file name to resize
  5. * @param $string - The image data, as a string
  6. * @param $width - new image width
  7. * @param $height - new image height
  8. * @param $proportional - keep image proportional, default is no
  9. * @param $output - name of the new file (include path if needed)
  10. * @param $delete_original - if true the original image will be deleted
  11. * @param $use_linux_commands - if set to true will use "rm" to delete the image, if false will use PHP unlink
  12. * @param $quality - enter 1-100 (100 is best quality) default is 100
  13. * @param $grayscale - if true, image will be grayscale (default is false)
  14. * @return boolean|resource
  15. */
  16. function smart_resize_image($file,
  17. $string = null,
  18. $width = 0,
  19. $height = 0,
  20. $proportional = false,
  21. $output = 'file',
  22. $delete_original = true,
  23. $use_linux_commands = false,
  24. $quality = 100,
  25. $grayscale = false
  26. ) {
  27. if ( $height <= 0 && $width <= 0 ) return false;
  28. if ( $file === null && $string === null ) return false;
  29. # Setting defaults and meta
  30. $info = $file !== null ? getimagesize($file) : getimagesizefromstring($string);
  31. $image = '';
  32. $final_width = 0;
  33. $final_height = 0;
  34. list($width_old, $height_old) = $info;
  35. $cropHeight = $cropWidth = 0;
  36. # Calculating proportionality
  37. if ($proportional) {
  38. if ($width == 0) $factor = $height/$height_old;
  39. elseif ($height == 0) $factor = $width/$width_old;
  40. else $factor = min( $width / $width_old, $height / $height_old );
  41. $final_width = round( $width_old * $factor );
  42. $final_height = round( $height_old * $factor );
  43. }
  44. else {
  45. $final_width = ( $width <= 0 ) ? $width_old : $width;
  46. $final_height = ( $height <= 0 ) ? $height_old : $height;
  47. $widthX = $width_old / $width;
  48. $heightX = $height_old / $height;
  49. $x = min($widthX, $heightX);
  50. $cropWidth = ($width_old - $width * $x) / 2;
  51. $cropHeight = ($height_old - $height * $x) / 2;
  52. }
  53. # Loading image to memory according to type
  54. switch ( $info[2] ) {
  55. case IMAGETYPE_JPEG: $file !== null ? $image = imagecreatefromjpeg($file) : $image = imagecreatefromstring($string); break;
  56. case IMAGETYPE_GIF: $file !== null ? $image = imagecreatefromgif($file) : $image = imagecreatefromstring($string); break;
  57. case IMAGETYPE_PNG: $file !== null ? $image = imagecreatefrompng($file) : $image = imagecreatefromstring($string); break;
  58. default: return false;
  59. }
  60. # Making the image grayscale, if needed
  61. if ($grayscale) {
  62. imagefilter($image, IMG_FILTER_GRAYSCALE);
  63. }
  64. # This is the resizing/resampling/transparency-preserving magic
  65. $image_resized = imagecreatetruecolor( $final_width, $final_height );
  66. if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
  67. $transparency = imagecolortransparent($image);
  68. $palletsize = imagecolorstotal($image);
  69. if ($transparency >= 0 && $transparency < $palletsize) {
  70. $transparent_color = imagecolorsforindex($image, $transparency);
  71. $transparency = imagecolorallocate($image_resized, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
  72. imagefill($image_resized, 0, 0, $transparency);
  73. imagecolortransparent($image_resized, $transparency);
  74. }
  75. elseif ($info[2] == IMAGETYPE_PNG) {
  76. imagealphablending($image_resized, false);
  77. $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
  78. imagefill($image_resized, 0, 0, $color);
  79. imagesavealpha($image_resized, true);
  80. }
  81. }
  82. imagecopyresampled($image_resized, $image, 0, 0, $cropWidth, $cropHeight, $final_width, $final_height, $width_old - 2 * $cropWidth, $height_old - 2 * $cropHeight);
  83. # Taking care of original, if needed
  84. if ( $delete_original ) {
  85. if ( $use_linux_commands ) exec('rm '.$file);
  86. else @unlink($file);
  87. }
  88. # Preparing a method of providing result
  89. switch ( strtolower($output) ) {
  90. case 'browser':
  91. $mime = image_type_to_mime_type($info[2]);
  92. header("Content-type: $mime");
  93. $output = NULL;
  94. break;
  95. case 'file':
  96. $output = $file;
  97. break;
  98. case 'return':
  99. return $image_resized;
  100. break;
  101. default:
  102. break;
  103. }
  104. # Writing image according to type to the output destination and image quality
  105. switch ( $info[2] ) {
  106. case IMAGETYPE_GIF: imagegif($image_resized, $output); break;
  107. case IMAGETYPE_JPEG: imagejpeg($image_resized, $output, $quality); break;
  108. case IMAGETYPE_PNG:
  109. $quality = 9 - (int)((0.9*$quality)/10.0);
  110. imagepng($image_resized, $output, $quality);
  111. break;
  112. default: return false;
  113. }
  114. return true;
  115. }
  116. ?>