[ImageEncoder] Make plugin respect instance config and use the new core interface
This commit is contained in:
parent
f9079784c4
commit
2b7232891e
@ -24,7 +24,6 @@ use App\Core\GSFile;
|
|||||||
use function App\Core\I18n\_m;
|
use function App\Core\I18n\_m;
|
||||||
use App\Core\Log;
|
use App\Core\Log;
|
||||||
use App\Core\Modules\Plugin;
|
use App\Core\Modules\Plugin;
|
||||||
use App\Entity\Attachment;
|
|
||||||
use App\Util\Common;
|
use App\Util\Common;
|
||||||
use App\Util\Exception\ClientException;
|
use App\Util\Exception\ClientException;
|
||||||
use App\Util\Exception\ServerException;
|
use App\Util\Exception\ServerException;
|
||||||
@ -55,11 +54,33 @@ class ImageEncoder extends Plugin
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Several obscure file types should be normalized to WebP on resize.
|
* @param array $event_map
|
||||||
|
* @param string $mimetype
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function preferredType(): int
|
public function onFileSanitizerAvailable(array &$event_map, string $mimetype): bool
|
||||||
{
|
{
|
||||||
return IMAGETYPE_WEBP;
|
if (GSFile::mimetypeMajor($mimetype) !== 'image') {
|
||||||
|
return Event::next;
|
||||||
|
}
|
||||||
|
$event_map['image'][] = [$this, 'fileSanitize'];
|
||||||
|
return Event::next;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $event_map
|
||||||
|
* @param string $mimetype
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function onFileResizerAvailable(array &$event_map, string $mimetype): bool
|
||||||
|
{
|
||||||
|
if (GSFile::mimetypeMajor($mimetype) !== 'image') {
|
||||||
|
return Event::next;
|
||||||
|
}
|
||||||
|
$event_map['image'][] = [$this, 'resizeImagePath'];
|
||||||
|
return Event::next;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -71,29 +92,30 @@ class ImageEncoder extends Plugin
|
|||||||
* @param null|int $width out
|
* @param null|int $width out
|
||||||
* @param null|int $height out
|
* @param null|int $height out
|
||||||
*
|
*
|
||||||
* @throws ClientException When vips doesn't understand the given mimetype
|
|
||||||
* @throws ServerException
|
* @throws ServerException
|
||||||
* @throws TemporaryFileException
|
* @throws TemporaryFileException
|
||||||
* @throws Vips\Exception
|
* @throws Vips\Exception
|
||||||
|
* @throws ClientException When vips doesn't understand the given mimetype
|
||||||
|
*
|
||||||
|
* @return bool true if sanitized
|
||||||
*
|
*
|
||||||
* @return bool
|
|
||||||
*/
|
*/
|
||||||
public function onAttachmentSanitization(SplFileInfo &$file, ?string &$mimetype, ?int &$width, ?int &$height): bool
|
public function fileSanitize(SplFileInfo &$file, ?string &$mimetype, ?int &$width, ?int &$height): bool
|
||||||
{
|
{
|
||||||
$original_mimetype = $mimetype;
|
$original_mimetype = $mimetype;
|
||||||
if (GSFile::mimetypeMajor($original_mimetype) != 'image') {
|
if (GSFile::mimetypeMajor($original_mimetype) != 'image') {
|
||||||
// Nothing concerning us
|
// Nothing concerning us
|
||||||
return Event::next;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to maintain original mimetype extension, otherwise default to preferred.
|
// Try to maintain original mimetype extension, otherwise default to preferred.
|
||||||
$extension = image_type_to_extension($this->preferredType(), include_dot: true);
|
$extension = '.' . Common::config('thumbnail', 'extension');
|
||||||
$extension = GSFile::ensureFilenameWithProperExtension(
|
$extension = GSFile::ensureFilenameWithProperExtension(
|
||||||
title: $file->getFilename(),
|
title: $file->getFilename(),
|
||||||
mimetype: $original_mimetype,
|
mimetype: $original_mimetype,
|
||||||
ext: $extension,
|
ext: $extension,
|
||||||
force: false
|
force: false
|
||||||
) ?? $extension;
|
) ?? $extension;
|
||||||
|
|
||||||
// TemporaryFile handles deleting the file if some error occurs
|
// TemporaryFile handles deleting the file if some error occurs
|
||||||
// IMPORTANT: We have to specify the extension for the temporary file
|
// IMPORTANT: We have to specify the extension for the temporary file
|
||||||
@ -109,27 +131,16 @@ class ImageEncoder extends Plugin
|
|||||||
$width = $image->width;
|
$width = $image->width;
|
||||||
$height = $image->height;
|
$height = $image->height;
|
||||||
$image = $image->crop(left: 0,
|
$image = $image->crop(left: 0,
|
||||||
top: 0,
|
top: 0,
|
||||||
width: $width,
|
width: $width,
|
||||||
height: $height);
|
height: $height);
|
||||||
$image->writeToFile($temp->getRealPath());
|
$image->writeToFile($temp->getRealPath());
|
||||||
|
|
||||||
// Replace original file with the sanitized one
|
// Replace original file with the sanitized one
|
||||||
$temp->commit($file->getRealPath());
|
$temp->commit($file->getRealPath());
|
||||||
|
|
||||||
// Only one plugin can handle sanitization
|
// Only one plugin can handle sanitization
|
||||||
return Event::stop;
|
return true;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $event_map
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function onResizerAvailable(array &$event_map): bool
|
|
||||||
{
|
|
||||||
$event_map['image'] = 'ResizeImagePath';
|
|
||||||
return Event::next;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -140,8 +151,11 @@ class ImageEncoder extends Plugin
|
|||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function onViewAttachmentImage(array $vars, array &$res): bool
|
public function onViewAttachment(array $vars, array &$res): bool
|
||||||
{
|
{
|
||||||
|
if ($vars['attachment']->getMimetypeMajor() !== 'image') {
|
||||||
|
return Event::next;
|
||||||
|
}
|
||||||
$res[] = Formatting::twigRenderFile('imageEncoder/imageEncoderView.html.twig',
|
$res[] = Formatting::twigRenderFile('imageEncoder/imageEncoderView.html.twig',
|
||||||
[
|
[
|
||||||
'attachment' => $vars['attachment'],
|
'attachment' => $vars['attachment'],
|
||||||
@ -152,7 +166,7 @@ class ImageEncoder extends Plugin
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Resizes an image. It will encode the image in the
|
* Resizes an image. It will encode the image in the
|
||||||
* `self::preferredType()` format. This only applies henceforward,
|
* preferred thumbnail extension. This only applies henceforward,
|
||||||
* not retroactively
|
* not retroactively
|
||||||
*
|
*
|
||||||
* Increases the 'memory_limit' to the one in the 'attachments' section in the config, to
|
* Increases the 'memory_limit' to the one in the 'attachments' section in the config, to
|
||||||
@ -166,13 +180,14 @@ class ImageEncoder extends Plugin
|
|||||||
* @param bool $smart_crop
|
* @param bool $smart_crop
|
||||||
* @param null|string $mimetype
|
* @param null|string $mimetype
|
||||||
*
|
*
|
||||||
* @throws TemporaryFileException
|
|
||||||
* @throws Vips\Exception
|
* @throws Vips\Exception
|
||||||
|
* @throws TemporaryFileException
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*
|
*
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
public function onResizeImagePath(string $source, ?TemporaryFile &$destination, int &$width, int &$height, bool $smart_crop, ?string &$mimetype): bool
|
public function resizeImagePath(string $source, ?TemporaryFile &$destination, int &$width, int &$height, bool $smart_crop, ?string &$mimetype): bool
|
||||||
{
|
{
|
||||||
$old_limit = ini_set('memory_limit', Common::config('attachments', 'memory_limit'));
|
$old_limit = ini_set('memory_limit', Common::config('attachments', 'memory_limit'));
|
||||||
try {
|
try {
|
||||||
@ -192,14 +207,13 @@ class ImageEncoder extends Plugin
|
|||||||
if (is_null($destination)) {
|
if (is_null($destination)) {
|
||||||
// IMPORTANT: We have to specify the extension for the temporary file
|
// IMPORTANT: We have to specify the extension for the temporary file
|
||||||
// in order to have a format conversion
|
// in order to have a format conversion
|
||||||
$ext = image_type_to_extension($this->preferredType(), include_dot: true);
|
$ext = '.' . Common::config('thumbnail', 'extension');
|
||||||
$destination = new TemporaryFile(['prefix' => 'gs-thumbnail', 'suffix' => $ext]);
|
$destination = new TemporaryFile(['prefix' => 'gs-thumbnail', 'suffix' => $ext]);
|
||||||
} elseif ($source === $destination->getRealPath()) {
|
} elseif ($source === $destination->getRealPath()) {
|
||||||
@unlink($destination->getRealPath());
|
@unlink($destination->getRealPath());
|
||||||
}
|
}
|
||||||
|
|
||||||
$type = self::preferredType();
|
$mimetype = Common::config('thumbnail', 'mimetype');
|
||||||
$mimetype = image_type_to_mime_type($type);
|
|
||||||
|
|
||||||
$width = $image->width;
|
$width = $image->width;
|
||||||
$height = $image->height;
|
$height = $image->height;
|
||||||
@ -209,7 +223,7 @@ class ImageEncoder extends Plugin
|
|||||||
} finally {
|
} finally {
|
||||||
ini_set('memory_limit', $old_limit); // Restore the old memory limit
|
ini_set('memory_limit', $old_limit); // Restore the old memory limit
|
||||||
}
|
}
|
||||||
return Event::stop;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user