This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/handle_ico/main.php

47 lines
1.5 KiB
PHP
Raw Normal View History

2020-01-26 13:19:35 +00:00
<?php declare(strict_types=1);
class IcoFileHandler extends DataHandlerExtension
{
protected $SUPPORTED_MIME = [MimeType::ICO, MimeType::ANI, MimeType::WIN_BITMAP, MimeType::ICO_OSX];
2020-02-23 18:37:22 +00:00
protected function media_check_properties(MediaCheckPropertiesEvent $event): void
{
2020-02-23 18:37:22 +00:00
$event->image->lossless = true;
$event->image->video = false;
$event->image->audio = false;
2020-06-14 16:05:55 +00:00
$event->image->image = ($event->mime!= MimeType::ANI);
2020-02-23 18:37:22 +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);
}
2020-02-23 18:37:22 +00:00
$width = $subheader['width'];
$height = $subheader['height'];
$event->image->width = $width == 0 ? 256 : $width;
$event->image->height = $height == 0 ? 256 : $height;
}
2020-06-14 16:05:55 +00:00
protected function create_thumb(string $hash, string $mime): bool
{
try {
2020-06-14 16:05:55 +00:00
create_image_thumb($hash, $mime, MediaEngine::IMAGICK);
return true;
} catch (MediaException $e) {
log_warning("handle_ico", "Could not generate thumbnail. " . $e->getMessage());
return false;
}
}
2020-02-23 18:37:22 +00:00
protected function check_contents(string $file): bool
{
$fp = fopen($file, "r");
$header = unpack("Snull/Stype/Scount", fread($fp, 6));
fclose($fp);
return ($header['null'] == 0 && ($header['type'] == 0 || $header['type'] == 1));
}
}