Browse Source

exp: pass image embeds via backend to lower mem usage

Andrew Dolgov 1 year ago
parent
commit
bf65441b2d
3 changed files with 103 additions and 9 deletions
  1. 19 5
      backend.php
  2. 82 0
      include/functions.php
  3. 2 4
      js/functions.js

+ 19 - 5
backend.php

@@ -884,14 +884,23 @@
 	case "imgproxy";
 		$base_url = $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
 		$url = rewrite_relative_url($base_url, $_REQUEST["url"]);
+		@$resize = (int) $_REQUEST['resize'];
 
-		$cache_key = "cache/imgproxy/" . sha1($url) . ".jpg";
+		$cache_key = "cache/imgproxy/" . sha1($url) . "-$resize.jpg";
 
-		$files = glob("cache/imgproxy/*.jpg");
+		if (rand(1) > 0.5) {
+			$files = glob("cache/imgproxy/*.jpg");
+			$processed = 0;
 
-		foreach ($files as $file) {
-			if (time() - filemtime($file) > 86400*7) {
-				unlink($file);
+			foreach ($files as $file) {
+				++$processed;
+
+				if (time() - filemtime($file) > 86400*7) {
+					unlink($file);
+				}
+
+				if ($processed > 25)
+					break;
 			}
 		}
 
@@ -923,11 +932,16 @@
 				$options['http']['proxy'] = _HTTP_PROXY;
 			}
 
+			sleep(rand(2, 5));
+
 			$ctx = stream_context_create($options);
 
 			$data = @file_get_contents($url, false, $ctx);
 
 			if ($data) {
+				if ($resize)
+					$data = make_thumbnail($data, $resize, $resize);
+
 				if (@file_put_contents($cache_key, $data)) {
 					$ctype = mime_content_type($cache_key);
 

+ 82 - 0
include/functions.php

@@ -1936,4 +1936,86 @@
 		return str_repeat('?,', count($arr) - 1) . '?';
 	}
 
+	function make_thumbnail($data, $dim_max_x = 600, $dim_max_y = 600) {
+
+		$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);
+					}
+			  }
+
+	//		  print "$o_file : $t_file : $o_height * $o_width -> $t_height * $t_width<br>";
+
+	        $t_im = imageCreateTrueColor($t_width, $t_height);
+
+			  imageFill($t_im, 0, 0, 0xffffff);
+
+	        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;
+						}
+
+						if ($angle != 0) $t_im = imagerotate($t_im, $angle, 0xffffff);
+				  }
+
+			  }
+
+			  /* Dirty, dirty hack */
+
+			  $t_width = imagesx($t_im) ;
+			  $t_height = imagesy($t_im) ;
+
+			  $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 = '';
+			  }
+
+	        imageDestroy($o_im);
+	        imageDestroy($t_im);
+
+			  //return array($thumb_binary, $o_width, $o_height, $t_width, $t_height);
+
+			  return $thumb_binary;
+		 }
+
+		return false;
+	}
+
 ?>

+ 2 - 4
js/functions.js

@@ -1,4 +1,5 @@
 const EMBED_MAX = 2048;
+const IMAGE_THUMB = 600;
 
 var spinner_refs = 0;
 var emoticon_cache = [];
@@ -426,10 +427,7 @@ function rewrite_urls_real(s, allow_embed) {
 					if (is_img && allow_embed) {
 						var res = $("<span>");
 
-						var real_src = m[1];
-
-						if (real_src.indexOf("http:") == 0)
-							real_src = "backend.php?op=imgproxy&url=" + encodeURIComponent(real_src);
+						var real_src = "backend.php?op=imgproxy&resize="+IMAGE_THUMB+"&url=" + param_escape(m[1]);
 
 						var img = $("<img>")
 									.addClass('img-embed img-responsive')