fastImageCopyResampled.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. function fastImageCopyResampled(&$dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $quality = 4) {
  3. ###
  4. # Plug-and-Play fastImageCopyResampled function replaces much slower imagecopyresampled.
  5. # Just include this function and change all "imagecopyresampled" references to "fastImageCopyResampled".
  6. # Typically from 30 to 60 times faster when reducing high resolution images down to thumbnail size using the default quality setting.
  7. # Author: Tim Eckel - Date: 09/07/07 - Version: 1.1 - Project: FreeRingers.net - Freely distributable - These comments must remain.
  8. #
  9. # Optional "quality" parameter (defaults is 3). Fractional values are allowed, for example 1.5. Must be greater than zero.
  10. # Between 0 and 1 = Fast, but mosaic results, closer to 0 increases the mosaic effect.
  11. # 1 = Up to 350 times faster. Poor results, looks very similar to imagecopyresized.
  12. # 2 = Up to 95 times faster. Images appear a little sharp, some prefer this over a quality of 3.
  13. # 3 = Up to 60 times faster. Will give high quality smooth results very close to imagecopyresampled, just faster.
  14. # 4 = Up to 25 times faster. Almost identical to imagecopyresampled for most images.
  15. # 5 = No speedup. Just uses imagecopyresampled, no advantage over imagecopyresampled.
  16. ###
  17. if (empty($src_image) || empty($dst_image) || $quality <= 0) { return false; }
  18. if ($quality < 5 && (($dst_w * $quality) < $src_w || ($dst_h * $quality) < $src_h)) {
  19. $temp = imagecreatetruecolor($dst_w * $quality + 1, $dst_h * $quality + 1);
  20. imagecopyresized($temp, $src_image, 0, 0, $src_x, $src_y, $dst_w * $quality + 1, $dst_h * $quality + 1, $src_w, $src_h);
  21. imagecopyresampled($dst_image, $temp, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h, $dst_w * $quality, $dst_h * $quality);
  22. imagedestroy($temp);
  23. } else imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
  24. return true;
  25. }
  26. ?>