2021-04-15 23:47:33 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// {{{ License
|
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
// }}}
|
|
|
|
|
2021-04-18 05:47:16 +01:00
|
|
|
namespace Plugin\ImageEncoder;
|
2021-04-15 23:47:33 +01:00
|
|
|
|
|
|
|
use App\Core\Event;
|
2021-04-29 21:31:24 +01:00
|
|
|
use App\Core\GSFile;
|
2021-04-16 11:46:53 +01:00
|
|
|
use function App\Core\I18n\_m;
|
2021-04-18 05:47:16 +01:00
|
|
|
use App\Core\Log;
|
|
|
|
use App\Core\Modules\Plugin;
|
2021-04-16 11:46:53 +01:00
|
|
|
use App\Entity\Attachment;
|
2021-04-16 16:57:25 +01:00
|
|
|
use App\Entity\AttachmentThumbnail;
|
2021-04-16 11:46:53 +01:00
|
|
|
use App\Util\Common;
|
2021-07-22 20:49:12 +01:00
|
|
|
use App\Util\Exception\TemporaryFileException;
|
|
|
|
use App\Util\Formatting;
|
2021-04-29 21:31:24 +01:00
|
|
|
use App\Util\TemporaryFile;
|
2021-04-18 05:47:16 +01:00
|
|
|
use Exception;
|
2021-04-16 16:57:25 +01:00
|
|
|
use Jcupitt\Vips;
|
2021-07-22 20:49:12 +01:00
|
|
|
use SplFileInfo;
|
2021-04-15 23:47:33 +01:00
|
|
|
|
2021-04-30 14:14:16 +01:00
|
|
|
/**
|
|
|
|
* Create thumbnails and validate image attachments
|
|
|
|
*
|
|
|
|
* @package GNUsocial
|
|
|
|
* @ccategory Attachment
|
|
|
|
*
|
|
|
|
* @author Diogo Peralta Cordeiro <mail@diogo.site>
|
|
|
|
* @authir Hugo Sales <hugo@hsal.es>
|
|
|
|
*
|
|
|
|
* @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
|
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
*/
|
2021-04-18 05:47:16 +01:00
|
|
|
class ImageEncoder extends Plugin
|
2021-04-15 23:47:33 +01:00
|
|
|
{
|
2021-04-18 05:47:16 +01:00
|
|
|
/**
|
|
|
|
* Several obscure file types should be normalized to WebP on resize.
|
|
|
|
*/
|
|
|
|
public function preferredType(): int
|
2021-04-15 23:47:33 +01:00
|
|
|
{
|
2021-04-18 05:47:16 +01:00
|
|
|
return IMAGETYPE_WEBP;
|
2021-04-15 23:47:33 +01:00
|
|
|
}
|
2021-04-16 11:46:53 +01:00
|
|
|
|
|
|
|
/**
|
2021-07-22 20:49:12 +01:00
|
|
|
* Re-encodes the image ensuring it's valid.
|
|
|
|
* Also ensures that the image is not greater than the max width and height configured.
|
2021-04-18 05:47:16 +01:00
|
|
|
*
|
2021-07-20 21:17:53 +01:00
|
|
|
* @param SplFileInfo $file
|
2021-07-22 20:49:12 +01:00
|
|
|
* @param null|string $mimetype in/out
|
|
|
|
* @param null|string $title in/out
|
|
|
|
* @param null|int $width out
|
|
|
|
* @param null|int $height out
|
2021-05-01 22:48:44 +01:00
|
|
|
*
|
|
|
|
* @throws Vips\Exception
|
2021-07-20 21:17:53 +01:00
|
|
|
* @throws TemporaryFileException
|
2021-04-18 05:47:16 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-08-14 16:47:45 +01:00
|
|
|
public function onAttachmentSanitization(SplFileInfo &$file, ?string &$mimetype, ?int &$width, ?int &$height): bool
|
2021-04-18 05:47:16 +01:00
|
|
|
{
|
2021-04-25 22:24:43 +01:00
|
|
|
$original_mimetype = $mimetype;
|
2021-04-29 21:31:24 +01:00
|
|
|
if (GSFile::mimetypeMajor($original_mimetype) != 'image') {
|
|
|
|
// Nothing concerning us
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2021-07-22 20:49:12 +01:00
|
|
|
// Try to maintain original mimetype extension, otherwise default to preferred.
|
|
|
|
$extension = image_type_to_extension($this->preferredType(), include_dot: true);
|
2021-08-14 16:47:45 +01:00
|
|
|
GSFile::ensureFilenameWithProperExtension(
|
|
|
|
title: $file->getFilename(),
|
2021-07-22 20:49:12 +01:00
|
|
|
mimetype: $original_mimetype,
|
|
|
|
ext: $extension,
|
|
|
|
force: false
|
|
|
|
);
|
|
|
|
|
2021-07-20 21:17:53 +01:00
|
|
|
// TemporaryFile handles deleting the file if some error occurs
|
2021-07-22 20:49:12 +01:00
|
|
|
// IMPORTANT: We have to specify the extension for the temporary file
|
|
|
|
// in order to have a format conversion
|
2021-07-20 21:17:53 +01:00
|
|
|
$temp = new TemporaryFile(['prefix' => 'image', 'suffix' => $extension]);
|
2021-04-29 21:31:24 +01:00
|
|
|
|
|
|
|
$image = Vips\Image::newFromFile($file->getRealPath(), ['access' => 'sequential']);
|
|
|
|
$width = Common::clamp($image->width, 0, Common::config('attachments', 'max_width'));
|
|
|
|
$height = Common::clamp($image->height, 0, Common::config('attachments', 'max_height'));
|
|
|
|
$image = $image->crop(0, 0, $width, $height);
|
|
|
|
$image->writeToFile($temp->getRealPath());
|
|
|
|
|
2021-07-22 20:49:12 +01:00
|
|
|
// Replace original file with the sanitized one
|
|
|
|
$temp->commit($file->getRealPath());
|
2021-04-29 21:31:24 +01:00
|
|
|
|
2021-07-22 20:49:12 +01:00
|
|
|
return Event::stop;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-22 21:17:23 +01:00
|
|
|
* @param array $event_map
|
2021-07-22 20:49:12 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-07-22 21:17:23 +01:00
|
|
|
public function onResizerAvailable(array &$event_map): bool
|
2021-07-22 20:49:12 +01:00
|
|
|
{
|
|
|
|
$event_map['image'] = 'ResizeImagePath';
|
|
|
|
return Event::next;
|
|
|
|
}
|
2021-04-29 21:31:24 +01:00
|
|
|
|
2021-07-22 20:49:12 +01:00
|
|
|
/**
|
|
|
|
* Generates the view for attachments of type Image
|
|
|
|
*
|
|
|
|
* @param array $vars
|
|
|
|
* @param array $res
|
2021-07-22 21:17:23 +01:00
|
|
|
*
|
2021-07-22 20:49:12 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function onViewAttachmentImage(array $vars, array &$res): bool
|
|
|
|
{
|
|
|
|
$res[] = Formatting::twigRenderFile('imageEncoder/imageEncoderView.html.twig', ['attachment' => $vars['attachment'], 'thumbnail_parameters' => $vars['thumbnail_parameters']]);
|
2021-04-18 05:47:16 +01:00
|
|
|
return Event::stop;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resizes an image. It will encode the image in the
|
|
|
|
* `self::preferredType()` format. This only applies henceforward,
|
2021-04-16 11:46:53 +01:00
|
|
|
* not retroactively
|
|
|
|
*
|
|
|
|
* Increases the 'memory_limit' to the one in the 'attachments' section in the config, to
|
|
|
|
* enable the handling of bigger images, which can cause a peak of memory consumption, while
|
|
|
|
* encoding
|
|
|
|
*
|
2021-04-18 05:47:16 +01:00
|
|
|
* @param Attachment $attachment
|
|
|
|
* @param AttachmentThumbnail $thumbnail
|
|
|
|
* @param int $width
|
|
|
|
* @param int $height
|
|
|
|
* @param bool $crop
|
|
|
|
*
|
2021-04-16 11:46:53 +01:00
|
|
|
* @throws Exception
|
2021-04-18 05:47:16 +01:00
|
|
|
* @throws Vips\Exception
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*
|
2021-04-16 11:46:53 +01:00
|
|
|
*/
|
2021-07-22 20:49:12 +01:00
|
|
|
public function onResizeImagePath(string $source, ?TemporaryFile &$destination, int &$width, int &$height, bool $smart_crop, ?string &$mimetype): bool
|
2021-04-16 11:46:53 +01:00
|
|
|
{
|
|
|
|
$old_limit = ini_set('memory_limit', Common::config('attachments', 'memory_limit'));
|
2021-04-16 16:57:25 +01:00
|
|
|
try {
|
2021-04-19 08:28:53 +01:00
|
|
|
try {
|
2021-04-28 16:03:17 +01:00
|
|
|
$image = Vips\Image::thumbnail($source, $width, ['height' => $height]);
|
2021-04-19 08:28:53 +01:00
|
|
|
} catch (Exception $e) {
|
2021-08-03 18:46:39 +01:00
|
|
|
Log::error(__METHOD__ . ' encountered exception: ' . get_class($e));
|
2021-04-19 08:28:53 +01:00
|
|
|
// TRANS: Exception thrown when trying to resize an unknown file type.
|
|
|
|
throw new Exception(_m('Unknown file type'));
|
|
|
|
}
|
2021-04-16 11:46:53 +01:00
|
|
|
|
2021-07-22 20:49:12 +01:00
|
|
|
if (is_null($destination)) {
|
|
|
|
// IMPORTANT: We have to specify the extension for the temporary file
|
|
|
|
// in order to have a format conversion
|
|
|
|
$ext = image_type_to_extension($this->preferredType(), include_dot: true);
|
|
|
|
$destination = new TemporaryFile(['prefix' => 'gs-thumbnail', 'suffix' => $ext]);
|
|
|
|
} elseif ($source === $destination->getRealPath()) {
|
|
|
|
@unlink($destination->getRealPath());
|
2021-04-19 08:28:53 +01:00
|
|
|
}
|
2021-04-16 11:46:53 +01:00
|
|
|
|
2021-04-28 16:03:17 +01:00
|
|
|
$type = self::preferredType();
|
|
|
|
$mimetype = image_type_to_mime_type($type);
|
|
|
|
|
|
|
|
if ($smart_crop) {
|
2021-04-19 08:28:53 +01:00
|
|
|
$image = $image->smartcrop($width, $height);
|
|
|
|
}
|
2021-05-01 22:48:44 +01:00
|
|
|
|
|
|
|
$width = $image->width;
|
|
|
|
$height = $image->height;
|
|
|
|
|
2021-07-22 20:49:12 +01:00
|
|
|
$image->writeToFile($destination->getRealPath());
|
2021-04-19 08:28:53 +01:00
|
|
|
unset($image);
|
|
|
|
} finally {
|
|
|
|
ini_set('memory_limit', $old_limit); // Restore the old memory limit
|
|
|
|
}
|
2021-07-20 21:17:53 +01:00
|
|
|
return Event::stop;
|
2021-04-16 11:46:53 +01:00
|
|
|
}
|
2021-04-15 23:47:33 +01:00
|
|
|
}
|