From a0c0b6e3d186923174b8b098a0ca5141f144e920 Mon Sep 17 00:00:00 2001 From: Matthew Barbour <matthew@darkholme.net> Date: Tue, 25 Jun 2019 18:43:57 -0500 Subject: [PATCH] Various fixes --- core/imageboard/misc.php | 7 ++++++- ext/handle_video/theme.php | 12 +++++++++++- ext/media/main.php | 39 ++++++++++++++++++++++++++++++++------ ext/resize/main.php | 19 +++++++++++++------ 4 files changed, 63 insertions(+), 14 deletions(-) diff --git a/core/imageboard/misc.php b/core/imageboard/misc.php index cec051d1..c82d9f98 100644 --- a/core/imageboard/misc.php +++ b/core/imageboard/misc.php @@ -178,6 +178,11 @@ function create_image_thumb(string $hash, string $type, string $engine = null) { $engine = $config->get_string(ImageConfig::THUMB_ENGINE); } + $output_format = $config->get_string(ImageConfig::THUMB_TYPE); + if($output_format=="webp") { + $output_format = Media::WEBP_LOSSY; + } + send_event(new MediaResizeEvent( $engine, $inname, @@ -186,7 +191,7 @@ function create_image_thumb(string $hash, string $type, string $engine = null) { $tsize[0], $tsize[1], false, - $config->get_string(ImageConfig::THUMB_TYPE), + $output_format, $config->get_int(ImageConfig::THUMB_QUALITY), true, $config->get_bool('thumb_upscale', false) diff --git a/ext/handle_video/theme.php b/ext/handle_video/theme.php index 64251769..c304eff7 100644 --- a/ext/handle_video/theme.php +++ b/ext/handle_video/theme.php @@ -13,6 +13,15 @@ class VideoFileHandlerTheme extends Themelet $loop = $config->get_bool("video_playback_loop"); $player = make_link('vendor/bower-asset/mediaelement/build/flashmediaelement.swf'); + $width="auto"; + if($image->width>1) { + $width = $image->width."px"; + } + $height="auto"; + if($image->height>1) { + $height = $image->height."px"; + } + $html = "Video not playing? <a href='$ilink'>Click here</a> to download the file.<br/>"; //Browser media format support: https://developer.mozilla.org/en-US/docs/Web/HTML/Supported_media_formats @@ -48,7 +57,8 @@ class VideoFileHandlerTheme extends Themelet $loop = ($loop ? ' loop' : ''); $html .= " - <video controls class='shm-main-image' id='main_image' alt='main image' poster='$thumb_url' {$autoplay} {$loop} style='max-width: 100%'> + <video controls class='shm-main-image' id='main_image' alt='main image' poster='$thumb_url' {$autoplay} {$loop} + style='height: $height; width: $width; max-width: 100%'> <source src='{$ilink}' type='{$supportedExts[$ext]}'> <!-- If browser doesn't support filetype, fallback to flash --> diff --git a/ext/media/main.php b/ext/media/main.php index 6b87c60a..a51e513e 100644 --- a/ext/media/main.php +++ b/ext/media/main.php @@ -726,6 +726,17 @@ class Media extends Extension // } // } + public static function is_lossless(string $filename, string $format) { + if(in_array($format, self::LOSSLESS_FORMATS)) { + return true; + } + switch($format) { + case "webp": + return self::is_lossless_webp($filename); + break; + } + return false; + } public static function image_resize_convert( String $input_path, @@ -752,6 +763,10 @@ class Media extends Extension $output_type = $input_type; } + if($output_type=="webp" && self::is_lossless($input_path, $input_type)) { + $output_type = self::WEBP_LOSSLESS; + } + $bg = "black"; if (self::supports_alpha($output_type)) { $bg = "none"; @@ -759,7 +774,8 @@ class Media extends Extension if (!empty($input_type)) { $input_type = $input_type . ":"; } - $args = ""; + + $args = " -flatten "; if ($minimize) { $args = " -strip -thumbnail"; } @@ -772,8 +788,19 @@ class Media extends Extension $resize_args .= "\!"; } - $format = '"%s" -flatten %s %ux%u%s -quality %u -background %s %s"%s[0]" %s:"%s" 2>&1'; - $cmd = sprintf($format, $convert, $args, $new_width, $new_height, $resize_args, $output_quality, $bg, $input_type, $input_path, $output_type, $output_filename); + switch ($output_type) { + case Media::WEBP_LOSSLESS: + $args .= '-define webp:lossless=true'; + break; + case "png": + $args .= '-define png:compression-level=9'; + break; + } + + $output_ext = self::determine_ext($output_type); + + $format = '"%s" %s -resize %ux%u%s -quality %u -background %s %s"%s[0]" %s:"%s" 2>&1'; + $cmd = sprintf($format, $convert, $args, $new_width, $new_height, $resize_args, $output_quality, $bg, $input_type, $input_path, $output_ext, $output_filename); $cmd = str_replace("\"convert\"", "convert", $cmd); // quotes are only needed if the path to convert contains a space; some other times, quotes break things, see github bug #27 exec($cmd, $output, $ret); if ($ret != 0) { @@ -1024,7 +1051,7 @@ class Media extends Extension return in_array(self::normalize_format($format), self::ALPHA_FORMATS); } - public static function is_input_supported($engine, $format, ?bool $lossless = null): bool + public static function is_input_supported(string $engine, string $format, ?bool $lossless = null): bool { $format = self::normalize_format($format, $lossless); if (!in_array($format, MediaEngine::INPUT_SUPPORT[$engine])) { @@ -1033,9 +1060,9 @@ class Media extends Extension return true; } - public static function is_output_supported($engine, $format): bool + public static function is_output_supported(string $engine, string $format, ?bool $lossless = false): bool { - $format = self::normalize_format($format); + $format = self::normalize_format($format, $lossless); if (!in_array($format, MediaEngine::OUTPUT_SUPPORT[$engine])) { return false; } diff --git a/ext/resize/main.php b/ext/resize/main.php index 9290f08b..212890fe 100644 --- a/ext/resize/main.php +++ b/ext/resize/main.php @@ -170,12 +170,12 @@ class ResizeImage extends Extension } } - private function can_resize_format($format, ?bool $lossless): bool + private function can_resize_format($format, ?bool $lossless = null): bool { global $config; $engine = $config->get_string(ResizeConfig::ENGINE); - return Media::is_input_supported($engine, $format) - && Media::is_output_supported($engine, $format); + return Media::is_input_supported($engine, $format, $lossless) + && Media::is_output_supported($engine, $format, $lossless); } @@ -183,12 +183,19 @@ class ResizeImage extends Extension /* ----------------------------- */ private function resize_image(Image $image_obj, int $width, int $height) { - global $database; + global $database, $config; if (($height <= 0) && ($width <= 0)) { throw new ImageResizeException("Invalid options for height and width. ($width x $height)"); } - + + $engine = $config->get_string(ResizeConfig::ENGINE); + + + if(!$this->can_resize_format($image_obj->ext, $image_obj->lossless)) { + throw new ImageResizeException("Engine $engine cannot resize selected image"); + } + $hash = $image_obj->hash; $image_filename = warehouse_path(Image::IMAGE_DIR, $hash); @@ -206,7 +213,7 @@ class ResizeImage extends Extension } send_event(new MediaResizeEvent( - MediaEngine::GD, + $engine, $image_filename, $image_obj->ext, $tmp_filename,