fork download
  1. <?php
  2. /**
  3.  * Уменьшение изображений до заданного размера.
  4.  */
  5.  
  6. class ImageResampler {
  7. public function createThumbnail ($image_file, $tn_file, $max_width, $max_height = 0, $what2do = 'TN', $output_format = '') {
  8.  
  9. if (!file_exists ($image_file)) return;
  10. if (!$tn_file) return;
  11.  
  12. $image_file_ext = ''; if (strrpos ($image_file, '.')) $image_file_ext = substr ($image_file, strrpos ($image_file, '.'));
  13. $tn_file_ext = ''; if (strrpos ($tn_file, '.')) $tn_file_ext = substr ($tn_file, strrpos ($tn_file, '.'));
  14.  
  15. $img_picture = '';
  16. if ($image_file_ext == '.jpg') $img_picture = imageCreateFromJPEG ($image_file);
  17. if ($image_file_ext == '.png') $img_picture = imageCreateFromPNG ($image_file);
  18. if ($image_file_ext == '.gif') $img_picture = imageCreateFromGIF ($image_file);
  19. if (!$img_picture) return;
  20.  
  21. $size = getImageSize ($image_file);
  22. if (!is_array ($size)) return;
  23.  
  24. $width = $size[0]; if (!$width) return;
  25. $height = $size[1]; if (!$height) return;
  26.  
  27. /* * */
  28.  
  29. if (!$max_height) $max_height = $max_width * 1024;
  30.  
  31. $required_width = $width;
  32. $required_height = $height;
  33.  
  34. for ($zoom = 0; $zoom <= 1000; $zoom ++) {
  35. if ($required_width <= $max_width &&
  36. $required_height <= $max_height) break;
  37.  
  38. if ($zoom == 0) {
  39. $required_width = (int) ($width / 3 * 2);
  40. $required_height = (int) ($height / 3 * 2);
  41. } else {
  42. $required_width = (int) ($width / $zoom);
  43. $required_height = (int) ($height / $zoom);
  44. }
  45. }
  46. unset ($zoom);
  47.  
  48. if ($what2do == 'IMG' && $required_width == $width) return;
  49.  
  50. $img_resampled = imageCreateTrueColor ($required_width, $required_height);
  51. imageCopyResampled ($img_resampled, $img_picture, 0, 0, 0, 0, $required_width, $required_height, $width, $height);
  52.  
  53. if (file_exists ($tn_file)) unlink ($tn_file);
  54.  
  55. if ($output_format && $output_format == 'PNG') {
  56. $tn_file = strreplace ($tn_file, $tn_file_ext, '.png');
  57.  
  58. imagePNG ($img_resampled, $tn_file);
  59. } else {
  60. $quality = 1;
  61. if ($what2do == 'TN') $quality = 75;
  62. if ($what2do == 'IMG') $quality = 95;
  63.  
  64. $tn_file = strreplace ($tn_file, $tn_file_ext, '.jpg');
  65.  
  66. imageJPEG ($img_resampled, $tn_file, $quality);
  67. }
  68.  
  69. /* * */
  70.  
  71. imageDestroy ($img_resampled); unset ($img_resampled);
  72. imageDestroy ($img_picture); unset ($img_picture);
  73. }
  74. }
  75. ?>
Success #stdin #stdout 0s 82880KB
stdin
Standard input is empty
stdout
Standard output is empty