Browse Source

Added fastimagecopyresampled

Tobias Reich 10 years ago
parent
commit
119d674002
2 changed files with 34 additions and 2 deletions
  1. 2 2
      php/modules/Photo.php
  2. 32 0
      php/modules/misc.php

+ 2 - 2
php/modules/Photo.php

@@ -216,12 +216,12 @@ class Photo extends Module {
 			}
 
 			# Create thumb
-			imagecopyresampled($thumb, $sourceImg, 0, 0, $startWidth, $startHeight, $width, $height, $newSize, $newSize);
+			fastimagecopyresampled($thumb, $sourceImg, 0, 0, $startWidth, $startHeight, $width, $height, $newSize, $newSize);
 			imagejpeg($thumb, $newUrl, $this->settings['thumbQuality']);
 			imagedestroy($thumb);
 
 			# Create retina thumb
-			imagecopyresampled($thumb2x, $sourceImg, 0, 0, $startWidth, $startHeight, $width*2, $height*2, $newSize, $newSize);
+			fastimagecopyresampled($thumb2x, $sourceImg, 0, 0, $startWidth, $startHeight, $width*2, $height*2, $newSize, $newSize);
 			imagejpeg($thumb2x, $newUrl2x, $this->settings['thumbQuality']);
 			imagedestroy($thumb2x);
 

+ 32 - 0
php/modules/misc.php

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