Updated handle_ico to use new common image thumbnailing and to inherit DataHandlerExtension

This commit is contained in:
Matthew Barbour 2019-06-14 12:34:53 -05:00 committed by matthew
parent 1b76366dd9
commit ed4b6bc4a0
8 changed files with 40 additions and 74 deletions

View file

@ -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;
}

View file

@ -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);

View file

@ -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;

View file

@ -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);
}
}

View file

@ -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;

View file

@ -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;

View file

@ -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));

View file

@ -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);
}