From ed4b6bc4a0e6f5391f5204c1f48d6ec5e5564956 Mon Sep 17 00:00:00 2001 From: Matthew Barbour Date: Fri, 14 Jun 2019 12:34:53 -0500 Subject: [PATCH] Updated handle_ico to use new common image thumbnailing and to inherit DataHandlerExtension --- core/extension.php | 6 +-- core/imageboard/misc.php | 10 +++-- ext/handle_flash/main.php | 2 +- ext/handle_ico/main.php | 83 +++++++++++---------------------------- ext/handle_mp3/main.php | 2 +- ext/handle_pixel/main.php | 7 ++-- ext/handle_svg/main.php | 2 +- ext/handle_video/main.php | 2 +- 8 files changed, 40 insertions(+), 74 deletions(-) diff --git a/core/extension.php b/core/extension.php index b7472583..af7bb6ad 100644 --- a/core/extension.php +++ b/core/extension.php @@ -222,13 +222,13 @@ abstract class DataHandlerExtension extends Extension $result = false; if ($this->supported_ext($event->type)) { if ($event->force) { - $result = $this->create_thumb($event->hash); + $result = $this->create_thumb($event->hash, $event->type); } else { $outname = warehouse_path("thumbs", $event->hash); if (file_exists($outname)) { return; } - $result = $this->create_thumb($event->hash); + $result = $this->create_thumb($event->hash, $event->type); } } if ($result) { @@ -256,5 +256,5 @@ abstract class DataHandlerExtension extends Extension abstract protected function supported_ext(string $ext): bool; abstract protected function check_contents(string $tmpname): bool; abstract protected function create_image_from_data(string $filename, array $metadata); - abstract protected function create_thumb(string $hash): bool; + abstract protected function create_thumb(string $hash, string $type): bool; } diff --git a/core/imageboard/misc.php b/core/imageboard/misc.php index 6336f8bb..e3e31576 100644 --- a/core/imageboard/misc.php +++ b/core/imageboard/misc.php @@ -164,9 +164,10 @@ function get_thumbnail_max_size_scaled(): array * Creates a thumbnail file using ImageMagick's convert command. * * @param $hash + * @param string $input_type Optional, allows specifying the input format. Usually not necessary. * @return bool true is successful, false if not. */ -function create_thumbnail_convert($hash): bool +function create_thumbnail_convert($hash, $input_type = ""): bool { global $config; @@ -200,8 +201,11 @@ function create_thumbnail_convert($hash): bool if ($type=="webp") { $bg = "none"; } - $format = '"%s" -flatten -strip -thumbnail %ux%u%s -quality %u -background %s "%s[0]" %s:"%s"'; - $cmd = sprintf($format, $convert, $w, $h, $options, $q, $bg, $inname, $type, $outname); + if(!empty($input_type)) { + $input_type = $input_type.":"; + } + $format = '"%s" -flatten -strip -thumbnail %ux%u%s -quality %u -background %s %s"%s[0]" %s:"%s" 2>&1'; + $cmd = sprintf($format, $convert, $w, $h, $options, $q, $bg,$input_type, $inname, $type, $outname); $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); diff --git a/ext/handle_flash/main.php b/ext/handle_flash/main.php index 8da00583..9476499e 100644 --- a/ext/handle_flash/main.php +++ b/ext/handle_flash/main.php @@ -8,7 +8,7 @@ class FlashFileHandler extends DataHandlerExtension { - protected function create_thumb(string $hash): bool + protected function create_thumb(string $hash, string $type): bool { global $config; diff --git a/ext/handle_ico/main.php b/ext/handle_ico/main.php index 56e3f373..ab005c96 100644 --- a/ext/handle_ico/main.php +++ b/ext/handle_ico/main.php @@ -5,65 +5,45 @@ * Description: Handle windows icons */ -class IcoFileHandler extends Extension +class IcoFileHandler extends DataHandlerExtension { - public function onDataUpload(DataUploadEvent $event) + const SUPPORTED_EXTENSIONS = ["ico", "ani", "cur"]; + + + protected function supported_ext(string $ext): bool { - if ($this->supported_ext($event->type) && $this->check_contents($event->tmpname)) { - $hash = $event->hash; - $ha = substr($hash, 0, 2); - move_upload_to_archive($event); - send_event(new ThumbnailGenerationEvent($event->hash, $event->type)); - $image = $this->create_image_from_data("images/$ha/$hash", $event->metadata); - if (is_null($image)) { - throw new UploadException("Icon handler failed to create image object from data"); - } - $iae = new ImageAdditionEvent($image); - send_event($iae); - $event->image_id = $iae->image->id; - } + return in_array(strtolower($ext), self::SUPPORTED_EXTENSIONS); } - public function onDisplayingImage(DisplayingImageEvent $event) - { - global $page; - if ($this->supported_ext($event->image->ext)) { - $this->theme->display_image($page, $event->image); - } - } - - private function supported_ext(string $ext): bool - { - $exts = ["ico", "ani", "cur"]; - return in_array(strtolower($ext), $exts); - } - - private function create_image_from_data(string $filename, array $metadata) + protected function create_image_from_data(string $filename, array $metadata) { $image = new Image(); - $fp = fopen($filename, "r"); - $header = unpack("Snull/Stype/Scount", fread($fp, 6)); - $subheader = unpack("Cwidth/Cheight/Ccolours/Cnull/Splanes/Sbpp/Lsize/loffset", fread($fp, 16)); - fclose($fp); + $fp = fopen($filename, "r"); + try { + unpack("Snull/Stype/Scount", fread($fp, 6)); + $subheader = unpack("Cwidth/Cheight/Ccolours/Cnull/Splanes/Sbpp/Lsize/loffset", fread($fp, 16)); + } finally { + fclose($fp); + } $width = $subheader['width']; $height = $subheader['height']; $image->width = $width == 0 ? 256 : $width; $image->height = $height == 0 ? 256 : $height; - $image->filesize = $metadata['size']; - $image->hash = $metadata['hash']; - $image->filename = $metadata['filename']; - $image->ext = $metadata['extension']; + $image->filesize = $metadata['size']; + $image->hash = $metadata['hash']; + $image->filename = $metadata['filename']; + $image->ext = $metadata['extension']; $image->tag_array = is_array($metadata['tags']) ? $metadata['tags'] : Tag::explode($metadata['tags']); - $image->source = $metadata['source']; + $image->source = $metadata['source']; return $image; } - private function check_contents(string $file): bool + protected function check_contents(string $file): bool { if (!file_exists($file)) { return false; @@ -74,27 +54,8 @@ class IcoFileHandler extends Extension return ($header['null'] == 0 && ($header['type'] == 0 || $header['type'] == 1)); } - private function create_thumb(string $hash): bool + protected function create_thumb(string $hash, string $type): bool { - global $config; - - $inname = warehouse_path("images", $hash); - $outname = warehouse_path("thumbs", $hash); - - $tsize = get_thumbnail_size_scaled($width, $height); - $w = $tsize[0]; - $h = $tsise[1]; - - $q = $config->get_int("thumb_quality"); - $mem = $config->get_int("thumb_mem_limit") / 1024 / 1024; // IM takes memory in MB - - if ($config->get_bool("ico_convert")) { - // "-limit memory $mem" broken? - exec("convert {$inname}[0] -geometry {$w}x{$h} -quality {$q} jpg:$outname"); - } else { - copy($inname, $outname); - } - - return true; + return create_thumbnail_convert($hash, $type); } } diff --git a/ext/handle_mp3/main.php b/ext/handle_mp3/main.php index a3e3dc9c..13e2bab4 100644 --- a/ext/handle_mp3/main.php +++ b/ext/handle_mp3/main.php @@ -7,7 +7,7 @@ class MP3FileHandler extends DataHandlerExtension { - protected function create_thumb(string $hash): bool + protected function create_thumb(string $hash, string $type): bool { copy("ext/handle_mp3/thumb.jpg", warehouse_path("thumbs", $hash)); return true; diff --git a/ext/handle_pixel/main.php b/ext/handle_pixel/main.php index daef5fe2..a3bc3bd2 100644 --- a/ext/handle_pixel/main.php +++ b/ext/handle_pixel/main.php @@ -8,11 +8,12 @@ class PixelFileHandler extends DataHandlerExtension { + const SUPPORTED_EXTENSIONS = ["jpg", "jpeg", "gif", "png", "webp"]; + protected function supported_ext(string $ext): bool { - $exts = ["jpg", "jpeg", "gif", "png", "webp"]; $ext = (($pos = strpos($ext, '?')) !== false) ? substr($ext, 0, $pos) : $ext; - return in_array(strtolower($ext), $exts); + return in_array(strtolower($ext), self::SUPPORTED_EXTENSIONS); } protected function create_image_from_data(string $filename, array $metadata) @@ -53,7 +54,7 @@ class PixelFileHandler extends DataHandlerExtension return false; } - protected function create_thumb(string $hash): bool + protected function create_thumb(string $hash, string $type): bool { global $config; diff --git a/ext/handle_svg/main.php b/ext/handle_svg/main.php index 5676d24f..f2151c06 100644 --- a/ext/handle_svg/main.php +++ b/ext/handle_svg/main.php @@ -32,7 +32,7 @@ class SVGFileHandler extends DataHandlerExtension } } - protected function create_thumb(string $hash): bool + protected function create_thumb(string $hash, string $type): bool { if (!create_thumbnail_convert($hash)) { copy("ext/handle_svg/thumb.jpg", warehouse_path("thumbs", $hash)); diff --git a/ext/handle_video/main.php b/ext/handle_video/main.php index 316139c8..f4f50320 100644 --- a/ext/handle_video/main.php +++ b/ext/handle_video/main.php @@ -53,7 +53,7 @@ class VideoFileHandler extends DataHandlerExtension /** * Generate the Thumbnail image for particular file. */ - protected function create_thumb(string $hash): bool + protected function create_thumb(string $hash, string $type): bool { return create_thumbnail_ffmpeg($hash); }