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

50 lines
1.6 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
class IcoFileHandler extends DataHandlerExtension
{
protected array $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;
$event->image->image = ($event->image->get_mime() != MimeType::ANI);
$fp = \Safe\fopen($event->image->get_image_filename(), "r");
2020-02-23 18:37:22 +00:00
try {
2024-01-15 13:53:54 +00:00
fseek($fp, 6); // skip header
$subheader = \Safe\unpack("Cwidth/Cheight/Ccolours/Cnull/Splanes/Sbpp/Lsize/loffset", \Safe\fread($fp, 16));
2023-01-11 11:59:56 +00:00
$width = $subheader['width'];
$height = $subheader['height'];
$event->image->width = $width == 0 ? 256 : $width;
$event->image->height = $height == 0 ? 256 : $height;
2020-02-23 18:37:22 +00:00
} finally {
fclose($fp);
}
}
protected function create_thumb(Image $image): bool
{
try {
create_image_thumb($image, 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 $tmpname): bool
2020-02-23 18:37:22 +00:00
{
$fp = \Safe\fopen($tmpname, "r");
$header = \Safe\unpack("Snull/Stype/Scount", \Safe\fread($fp, 6));
2020-02-23 18:37:22 +00:00
fclose($fp);
return ($header['null'] == 0 && ($header['type'] == 0 || $header['type'] == 1));
}
}