2020-01-26 13:19:35 +00:00
|
|
|
<?php declare(strict_types=1);
|
2008-04-08 21:58:17 +00:00
|
|
|
|
2019-06-14 17:34:53 +00:00
|
|
|
class IcoFileHandler extends DataHandlerExtension
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-06-14 17:34:53 +00:00
|
|
|
const SUPPORTED_EXTENSIONS = ["ico", "ani", "cur"];
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2019-06-25 20:17:13 +00:00
|
|
|
public function onMediaCheckProperties(MediaCheckPropertiesEvent $event)
|
|
|
|
{
|
2019-09-29 13:30:55 +00:00
|
|
|
if (in_array($event->ext, self::SUPPORTED_EXTENSIONS)) {
|
2020-01-29 20:22:50 +00:00
|
|
|
$event->image->lossless = true;
|
|
|
|
$event->image->video = false;
|
|
|
|
$event->image->audio = false;
|
|
|
|
$event->image->image = ($event->ext!="ani");
|
2019-06-25 20:17:13 +00:00
|
|
|
|
|
|
|
$fp = fopen($event->file_name, "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'];
|
2020-01-29 20:22:50 +00:00
|
|
|
$event->image->width = $width == 0 ? 256 : $width;
|
|
|
|
$event->image->height = $height == 0 ? 256 : $height;
|
2019-06-25 20:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 17:34:53 +00:00
|
|
|
protected function supported_ext(string $ext): bool
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-06-14 17:34:53 +00:00
|
|
|
return in_array(strtolower($ext), self::SUPPORTED_EXTENSIONS);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 17:34:53 +00:00
|
|
|
protected function check_contents(string $file): bool
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$fp = fopen($file, "r");
|
|
|
|
$header = unpack("Snull/Stype/Scount", fread($fp, 6));
|
|
|
|
fclose($fp);
|
|
|
|
return ($header['null'] == 0 && ($header['type'] == 0 || $header['type'] == 1));
|
|
|
|
}
|
|
|
|
|
2019-06-14 17:34:53 +00:00
|
|
|
protected function create_thumb(string $hash, string $type): bool
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-06-18 18:45:59 +00:00
|
|
|
try {
|
2019-06-24 15:05:16 +00:00
|
|
|
create_image_thumb($hash, $type, MediaEngine::IMAGICK);
|
2019-06-18 18:45:59 +00:00
|
|
|
return true;
|
2019-06-24 15:05:16 +00:00
|
|
|
} catch (MediaException $e) {
|
2019-06-18 18:45:59 +00:00
|
|
|
log_warning("handle_ico", "Could not generate thumbnail. " . $e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2008-04-08 21:58:17 +00:00
|
|
|
}
|