Browse Source

add play watermark to proxied gifs

Andrew Dolgov 1 year ago
parent
commit
5c86ce1d70
2 changed files with 76 additions and 59 deletions
  1. 2 1
      backend.php
  2. 74 58
      include/functions.php

+ 2 - 1
backend.php

@@ -883,6 +883,7 @@
 		$base_url = $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
 		$url = rewrite_relative_url($base_url, $_REQUEST["url"]);
 		@$resize = (int) $_REQUEST['resize'];
+		@$nocache = (bool) $_REQUEST['nocache'];
 
 		$cache_key = "cache/imgproxy/" . sha1($url) . "-$resize.jpg";
 		$mem_key = 'ttirc.imgcache:' . sha1($cache_key);
@@ -914,7 +915,7 @@
 			}
 		}
 
-		if (file_exists($cache_key) && filesize($cache_key) > 0) {
+		if (!$nocache && file_exists($cache_key) && filesize($cache_key) > 0) {
 			@touch($cache_key);
 
 			$ctype = mime_content_type($cache_key);

+ 74 - 58
include/functions.php

@@ -1927,79 +1927,95 @@
 		$o_im = @imagecreatefromstring($data);
 
 		if ($o_im) {
-	        $o_width = imagesx($o_im) ;
-			  $o_height = imagesy($o_im) ;
-
-			  if (max($o_width, $o_height) < max($dim_max_x, $dim_max_y)) {
-					$t_height = $o_height;
-					$t_width = $o_width;
-			  } else {
-					if ($o_height > $o_width) {
-						$t_height = $dim_max_x;
-						$t_width = round($o_width/$o_height * $t_height);
-					} else {
-						$t_width = $dim_max_y;
-						$t_height = round($o_height/$o_width * $t_width);
-					}
-			  }
+			$imageinfo = @getimagesizefromstring($data);
+
+			$o_width = imagesx($o_im) ;
+			$o_height = imagesy($o_im) ;
+
+			if (max($o_width, $o_height) < max($dim_max_x, $dim_max_y)) {
+				$t_height = $o_height;
+				$t_width = $o_width;
+			} else {
+				if ($o_height > $o_width) {
+					$t_height = $dim_max_x;
+					$t_width = round($o_width/$o_height * $t_height);
+				} else {
+					$t_width = $dim_max_y;
+					$t_height = round($o_height/$o_width * $t_width);
+				}
+			}
 
-	//		  print "$o_file : $t_file : $o_height * $o_width -> $t_height * $t_width<br>";
+	//		print "$o_file : $t_file : $o_height * $o_width -> $t_height * $t_width<br>";
 
-	        $t_im = imageCreateTrueColor($t_width, $t_height);
+			$t_im = imageCreateTrueColor($t_width, $t_height);
 
-			  imageFill($t_im, 0, 0, 0xffffff);
+			imageFill($t_im, 0, 0, 0xffffff);
 
-	        imageCopyResampled($t_im, $o_im, 0, 0, 0, 0,
+			imageCopyResampled($t_im, $o_im, 0, 0, 0, 0,
 					$t_width, $t_height, $o_width, $o_height);
 
-			  $exif = @exif_read_data($o_file);
-
-			  if ($exif) {
-				  $ori = @$exif['IFD0']['Orientation'];
-				  if (!$ori) $ori = @$exif['Orientation'];
-
-				  if ($ori) {
-						switch ($ori) {
-						case 3:
-							$angle = -180;
-							break;
-						case 6:
-							$angle = -90;
-							break;
-						case 8:
-							$angle = 90;
-							break;
-						default:
-							$angle = 0;
-						}
+			$exif = @exif_read_data($o_file);
+
+			if ($exif) {
+				$ori = @$exif['IFD0']['Orientation'];
+				if (!$ori) $ori = @$exif['Orientation'];
+
+				if ($ori) {
+					switch ($ori) {
+					case 3:
+						$angle = -180;
+						break;
+					case 6:
+						$angle = -90;
+						break;
+					case 8:
+						$angle = 90;
+						break;
+					default:
+						$angle = 0;
+					}
+
+					if ($angle != 0) $t_im = imagerotate($t_im, $angle, 0xffffff);
+				}
+			}
 
-						if ($angle != 0) $t_im = imagerotate($t_im, $angle, 0xffffff);
-				  }
+			if ($imageinfo && $imageinfo["mime"] == "image/gif") {
+				$stamp = imagecreatefrompng('images/play-outline.png');
 
-			  }
+				if ($stamp) {
+					$sx = imagesx($stamp);
+					$sy = imagesy($stamp);
 
-			  /* Dirty, dirty hack */
+					imagecopy($t_im, $stamp, 
+						imagesx($t_im)/2 - $sx/2, 
+						imagesy($t_im)/2 - $sy/2, 
+						0, 0, 
+						imagesx($stamp), imagesy($stamp));
+				}
+			}
+
+			/* Dirty, dirty hack */
 
-			  $t_width = imagesx($t_im) ;
-			  $t_height = imagesy($t_im) ;
+			$t_width = imagesx($t_im) ;
+			$t_height = imagesy($t_im) ;
 
-			  $tmp = tempnam(sys_get_temp_dir(), 'ggx');
+			$tmp = tempnam(sys_get_temp_dir(), 'ggx');
 
-			  if ($tmp) {
-				  imageJpeg($t_im, $tmp, 85);
-			 	  $thumb_binary = file_get_contents($tmp);
-				  unlink($tmp);
-			  } else {
-				  $thumb_binary = '';
-			  }
+			if ($tmp) {
+				imageJpeg($t_im, $tmp, 85);
+				$thumb_binary = file_get_contents($tmp);
+				unlink($tmp);
+			} else {
+				$thumb_binary = '';
+			}
 
-	        imageDestroy($o_im);
-	        imageDestroy($t_im);
+			imageDestroy($o_im);
+			imageDestroy($t_im);
 
-			  //return array($thumb_binary, $o_width, $o_height, $t_width, $t_height);
+			//return array($thumb_binary, $o_width, $o_height, $t_width, $t_height);
 
-			  return $thumb_binary;
-		 }
+			return $thumb_binary;
+		}
 
 		return false;
 	}