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_pixel/main.php

82 lines
2.8 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
class PixelFileHandler extends DataHandlerExtension
{
protected array $SUPPORTED_MIME = [MimeType::JPEG, MimeType::GIF, MimeType::PNG, MimeType::WEBP];
2020-02-23 18:37:22 +00:00
protected function media_check_properties(MediaCheckPropertiesEvent $event): void
{
$filename = $event->image->get_image_filename();
$mime = $event->image->get_mime();
$event->image->lossless = Media::is_lossless($filename, $mime);
2020-02-23 18:37:22 +00:00
$event->image->audio = false;
switch ($mime) {
2020-06-14 16:05:55 +00:00
case MimeType::GIF:
$event->image->video = MimeType::is_animated_gif($filename);
2020-02-23 18:37:22 +00:00
break;
2020-06-14 16:05:55 +00:00
case MimeType::WEBP:
$event->image->video = MimeType::is_animated_webp($filename);
2020-02-23 18:37:22 +00:00
break;
default:
$event->image->video = false;
break;
}
$event->image->image = !$event->image->video;
$info = getimagesize($event->image->get_image_filename());
2020-02-23 18:37:22 +00:00
if ($info) {
2020-01-29 20:22:50 +00:00
$event->image->width = $info[0];
$event->image->height = $info[1];
}
}
protected function check_contents(string $tmpname): bool
{
$valid = [IMAGETYPE_PNG, IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_WEBP];
$info = getimagesize($tmpname);
2020-02-23 18:14:15 +00:00
return $info && in_array($info[2], $valid);
}
protected function create_thumb(Image $image): bool
{
try {
create_image_thumb($image);
return true;
} catch (InsufficientMemoryException $e) {
log_warning("handle_pixel", "Insufficient memory while creating thumbnail: ".$e->getMessage());
$tsize = get_thumbnail_max_size_scaled();
$thumb = imagecreatetruecolor($tsize[0], min($tsize[1], 64));
$white = imagecolorallocate($thumb, 255, 255, 255);
$black = imagecolorallocate($thumb, 0, 0, 0);
imagefill($thumb, 0, 0, $white);
imagestring($thumb, 5, 10, 24, "Image Too Large :(", $black);
// FIXME: write the image to disk??
return true;
2023-01-11 11:15:26 +00:00
} catch (\Exception $e) {
log_error("handle_pixel", "Error while creating thumbnail: ".$e->getMessage());
return false;
}
}
public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event)
{
if ($event->context == "view") {
$event->add_part("
<form>
<select class='shm-zoomer'>
<option value='full'>Full Size</option>
<option value='width'>Fit Width</option>
<option value='height'>Fit Height</option>
<option value='both'>Fit Both</option>
</select>
</form>
", 20);
}
}
}