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