2021-04-16 07:47:33 +09: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 13:47:16 +09:00
|
|
|
namespace Plugin\ImageEncoder;
|
2021-04-16 07:47:33 +09:00
|
|
|
|
|
|
|
use App\Core\Event;
|
2021-04-30 05:31:24 +09:00
|
|
|
use App\Core\GSFile;
|
2021-04-16 19:46:53 +09:00
|
|
|
use function App\Core\I18n\_m;
|
2021-04-18 13:47:16 +09:00
|
|
|
use App\Core\Log;
|
|
|
|
use App\Core\Modules\Plugin;
|
2021-04-16 19:46:53 +09:00
|
|
|
use App\Util\Common;
|
2021-08-14 23:00:40 +09:00
|
|
|
use App\Util\Exception\ClientException;
|
|
|
|
use App\Util\Exception\ServerException;
|
2021-07-23 04:49:12 +09:00
|
|
|
use App\Util\Exception\TemporaryFileException;
|
|
|
|
use App\Util\Formatting;
|
2021-04-30 05:31:24 +09:00
|
|
|
use App\Util\TemporaryFile;
|
2021-04-18 13:47:16 +09:00
|
|
|
use Exception;
|
2021-04-17 00:57:25 +09:00
|
|
|
use Jcupitt\Vips;
|
2021-08-14 23:00:40 +09:00
|
|
|
use Plugin\ImageEncoder\Exception\UnsupportedFileTypeException;
|
2021-07-23 04:49:12 +09:00
|
|
|
use SplFileInfo;
|
2021-04-16 07:47:33 +09:00
|
|
|
|
2021-04-30 22:14:16 +09:00
|
|
|
/**
|
|
|
|
* Create thumbnails and validate image attachments
|
|
|
|
*
|
|
|
|
* @package GNUsocial
|
2021-08-14 23:00:40 +09:00
|
|
|
* @category Attachment
|
2021-04-30 22:14:16 +09:00
|
|
|
*
|
|
|
|
* @author Diogo Peralta Cordeiro <mail@diogo.site>
|
2021-08-14 23:00:40 +09:00
|
|
|
* @author Hugo Sales <hugo@hsal.es>
|
2021-04-30 22:14:16 +09:00
|
|
|
* @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 13:47:16 +09:00
|
|
|
class ImageEncoder extends Plugin
|
2021-04-16 07:47:33 +09:00
|
|
|
{
|
2021-08-14 23:00:40 +09:00
|
|
|
public function version(): string
|
|
|
|
{
|
|
|
|
return '3.0.0';
|
|
|
|
}
|
|
|
|
|
2021-09-22 23:04:45 +09:00
|
|
|
/**
|
|
|
|
* @param array $event_map
|
|
|
|
* @param string $mimetype
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function onFileMetaAvailable(array &$event_map, string $mimetype): bool
|
|
|
|
{
|
|
|
|
if (GSFile::mimetypeMajor($mimetype) !== 'image') {
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
$event_map['image'][] = [$this, 'fileMeta'];
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:47:16 +09:00
|
|
|
/**
|
2021-08-19 06:14:15 +09:00
|
|
|
* @param array $event_map
|
|
|
|
* @param string $mimetype
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function onFileSanitizerAvailable(array &$event_map, string $mimetype): bool
|
|
|
|
{
|
|
|
|
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
|
2021-04-18 13:47:16 +09:00
|
|
|
*/
|
2021-08-19 06:14:15 +09:00
|
|
|
public function onFileResizerAvailable(array &$event_map, string $mimetype): bool
|
2021-04-16 07:47:33 +09:00
|
|
|
{
|
2021-08-19 06:14:15 +09:00
|
|
|
if (GSFile::mimetypeMajor($mimetype) !== 'image') {
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
$event_map['image'][] = [$this, 'resizeImagePath'];
|
|
|
|
return Event::next;
|
2021-04-16 07:47:33 +09:00
|
|
|
}
|
2021-04-16 19:46:53 +09:00
|
|
|
|
2021-09-22 23:04:45 +09:00
|
|
|
public function fileMeta(SplFileInfo &$file, ?string &$mimetype, ?int &$width, ?int &$height): bool
|
|
|
|
{
|
2021-09-23 22:50:53 +09:00
|
|
|
$old_limit = ini_set('memory_limit', Common::config('attachments', 'memory_limit'));
|
2021-09-22 23:04:45 +09:00
|
|
|
try {
|
2021-09-23 22:50:53 +09:00
|
|
|
$original_mimetype = $mimetype;
|
|
|
|
if (GSFile::mimetypeMajor($original_mimetype) !== 'image') {
|
|
|
|
// Nothing concerning us
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$image = Vips\Image::newFromFile($file->getRealPath(), ['access' => 'sequential']);
|
|
|
|
} catch (Vips\Exception $e) {
|
|
|
|
Log::debug("ImageEncoder's Vips couldn't handle the image file, failed with {$e}.");
|
|
|
|
throw new UnsupportedFileTypeException(_m("Unsupported image file with {$mimetype}.", previous: $e));
|
|
|
|
}
|
|
|
|
$width = $image->width;
|
|
|
|
$height = $image->height;
|
|
|
|
} finally {
|
|
|
|
ini_set('memory_limit', $old_limit); // Restore the old memory limit
|
2021-09-22 23:04:45 +09:00
|
|
|
}
|
|
|
|
// Only one plugin can handle meta
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-16 19:46:53 +09:00
|
|
|
/**
|
2021-09-22 23:04:45 +09:00
|
|
|
* Re-encodes the image ensuring it is valid.
|
2021-07-23 04:49:12 +09:00
|
|
|
* Also ensures that the image is not greater than the max width and height configured.
|
2021-04-18 13:47:16 +09:00
|
|
|
*
|
2021-07-21 05:17:53 +09:00
|
|
|
* @param SplFileInfo $file
|
2021-07-23 04:49:12 +09:00
|
|
|
* @param null|string $mimetype in/out
|
|
|
|
* @param null|int $width out
|
|
|
|
* @param null|int $height out
|
2021-05-02 06:48:44 +09:00
|
|
|
*
|
2021-08-14 23:00:40 +09:00
|
|
|
* @throws ServerException
|
2021-07-21 05:17:53 +09:00
|
|
|
* @throws TemporaryFileException
|
2021-08-14 23:00:40 +09:00
|
|
|
* @throws Vips\Exception
|
2021-08-19 06:14:15 +09:00
|
|
|
* @throws ClientException When vips doesn't understand the given mimetype
|
|
|
|
*
|
|
|
|
* @return bool true if sanitized
|
2021-04-18 13:47:16 +09:00
|
|
|
*
|
|
|
|
*/
|
2021-08-19 06:14:15 +09:00
|
|
|
public function fileSanitize(SplFileInfo &$file, ?string &$mimetype, ?int &$width, ?int &$height): bool
|
2021-04-18 13:47:16 +09:00
|
|
|
{
|
2021-09-23 22:50:53 +09:00
|
|
|
$old_limit = ini_set('memory_limit', Common::config('attachments', 'memory_limit'));
|
|
|
|
try {
|
|
|
|
$original_mimetype = $mimetype;
|
|
|
|
if (GSFile::mimetypeMajor($original_mimetype) !== 'image') {
|
|
|
|
// Nothing concerning us
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to maintain original mimetype extension, otherwise default to preferred.
|
|
|
|
$extension = '.' . Common::config('thumbnail', 'extension');
|
|
|
|
$extension = GSFile::ensureFilenameWithProperExtension(
|
2021-08-19 06:14:15 +09:00
|
|
|
title: $file->getFilename(),
|
|
|
|
mimetype: $original_mimetype,
|
|
|
|
ext: $extension,
|
|
|
|
force: false
|
|
|
|
) ?? $extension;
|
2021-07-23 04:49:12 +09:00
|
|
|
|
2021-09-23 22:50:53 +09:00
|
|
|
// TemporaryFile handles deleting the file if some error occurs
|
|
|
|
// IMPORTANT: We have to specify the extension for the temporary file
|
|
|
|
// in order to have a format conversion
|
|
|
|
$temp = new TemporaryFile(['prefix' => 'image', 'suffix' => $extension]);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$image = Vips\Image::newFromFile($file->getRealPath(), ['access' => 'sequential']);
|
|
|
|
} catch (Vips\Exception $e) {
|
|
|
|
Log::debug("ImageEncoder's Vips couldn't handle the image file, failed with {$e}.");
|
|
|
|
throw new UnsupportedFileTypeException(_m("Unsupported image file with {$mimetype}.", previous: $e));
|
|
|
|
}
|
|
|
|
$width = $image->width;
|
|
|
|
$height = $image->height;
|
|
|
|
$image = $image->crop(left: 0,
|
|
|
|
top: 0,
|
|
|
|
width: $width,
|
|
|
|
height: $height);
|
|
|
|
$image->writeToFile($temp->getRealPath());
|
|
|
|
|
|
|
|
// Replace original file with the sanitized one
|
|
|
|
$temp->commit($file->getRealPath());
|
|
|
|
} finally {
|
|
|
|
ini_set('memory_limit', $old_limit); // Restore the old memory limit
|
2021-08-14 23:00:40 +09:00
|
|
|
}
|
2021-04-30 05:31:24 +09:00
|
|
|
|
2021-08-14 23:00:40 +09:00
|
|
|
// Only one plugin can handle sanitization
|
2021-08-19 06:14:15 +09:00
|
|
|
return true;
|
2021-07-23 04:49:12 +09:00
|
|
|
}
|
2021-04-30 05:31:24 +09:00
|
|
|
|
2021-07-23 04:49:12 +09:00
|
|
|
/**
|
|
|
|
* Generates the view for attachments of type Image
|
|
|
|
*
|
|
|
|
* @param array $vars
|
|
|
|
* @param array $res
|
2021-07-23 05:17:23 +09:00
|
|
|
*
|
2021-07-23 04:49:12 +09:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-08-19 06:14:15 +09:00
|
|
|
public function onViewAttachment(array $vars, array &$res): bool
|
2021-07-23 04:49:12 +09:00
|
|
|
{
|
2021-08-19 06:14:15 +09:00
|
|
|
if ($vars['attachment']->getMimetypeMajor() !== 'image') {
|
|
|
|
return Event::next;
|
|
|
|
}
|
2021-08-12 11:43:11 +09:00
|
|
|
$res[] = Formatting::twigRenderFile('imageEncoder/imageEncoderView.html.twig',
|
2021-08-18 22:04:17 +09:00
|
|
|
[
|
|
|
|
'attachment' => $vars['attachment'],
|
|
|
|
'note' => $vars['note'],
|
2021-08-12 11:43:11 +09:00
|
|
|
]);
|
2021-04-18 13:47:16 +09:00
|
|
|
return Event::stop;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resizes an image. It will encode the image in the
|
2021-08-19 06:14:15 +09:00
|
|
|
* preferred thumbnail extension. This only applies henceforward,
|
2021-04-16 19:46:53 +09: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-08-14 23:00:40 +09:00
|
|
|
* @param string $source
|
|
|
|
* @param null|TemporaryFile $destination
|
|
|
|
* @param int $width
|
|
|
|
* @param int $height
|
|
|
|
* @param bool $smart_crop
|
|
|
|
* @param null|string $mimetype
|
2021-04-18 13:47:16 +09:00
|
|
|
*
|
|
|
|
* @throws Vips\Exception
|
2021-08-19 06:14:15 +09:00
|
|
|
* @throws TemporaryFileException
|
2021-04-18 13:47:16 +09:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*
|
2021-08-19 06:14:15 +09:00
|
|
|
*
|
2021-04-16 19:46:53 +09:00
|
|
|
*/
|
2021-08-19 06:14:15 +09:00
|
|
|
public function resizeImagePath(string $source, ?TemporaryFile &$destination, int &$width, int &$height, bool $smart_crop, ?string &$mimetype): bool
|
2021-04-16 19:46:53 +09:00
|
|
|
{
|
|
|
|
$old_limit = ini_set('memory_limit', Common::config('attachments', 'memory_limit'));
|
2021-04-17 00:57:25 +09:00
|
|
|
try {
|
2021-04-19 16:28:53 +09:00
|
|
|
try {
|
2021-08-18 21:08:15 +09:00
|
|
|
if (!$smart_crop) {
|
|
|
|
$image = Vips\Image::thumbnail($source, $width, ['height' => $height]);
|
|
|
|
} else {
|
|
|
|
$image = Vips\Image::newFromFile($source, ['access' => 'sequential']);
|
|
|
|
$image = $image->smartcrop($width, $height, [Vips\Interesting::ATTENTION]);
|
|
|
|
}
|
2021-04-19 16:28:53 +09:00
|
|
|
} catch (Exception $e) {
|
2021-08-04 02:46:39 +09:00
|
|
|
Log::error(__METHOD__ . ' encountered exception: ' . get_class($e));
|
2021-04-19 16:28:53 +09:00
|
|
|
// TRANS: Exception thrown when trying to resize an unknown file type.
|
|
|
|
throw new Exception(_m('Unknown file type'));
|
|
|
|
}
|
2021-04-16 19:46:53 +09:00
|
|
|
|
2021-07-23 04:49:12 +09:00
|
|
|
if (is_null($destination)) {
|
|
|
|
// IMPORTANT: We have to specify the extension for the temporary file
|
|
|
|
// in order to have a format conversion
|
2021-08-19 06:14:15 +09:00
|
|
|
$ext = '.' . Common::config('thumbnail', 'extension');
|
2021-07-23 04:49:12 +09:00
|
|
|
$destination = new TemporaryFile(['prefix' => 'gs-thumbnail', 'suffix' => $ext]);
|
|
|
|
} elseif ($source === $destination->getRealPath()) {
|
|
|
|
@unlink($destination->getRealPath());
|
2021-04-19 16:28:53 +09:00
|
|
|
}
|
2021-04-16 19:46:53 +09:00
|
|
|
|
2021-08-19 06:14:15 +09:00
|
|
|
$mimetype = Common::config('thumbnail', 'mimetype');
|
2021-04-29 00:03:17 +09:00
|
|
|
|
2021-05-02 06:48:44 +09:00
|
|
|
$width = $image->width;
|
|
|
|
$height = $image->height;
|
|
|
|
|
2021-07-23 04:49:12 +09:00
|
|
|
$image->writeToFile($destination->getRealPath());
|
2021-04-19 16:28:53 +09:00
|
|
|
unset($image);
|
|
|
|
} finally {
|
|
|
|
ini_set('memory_limit', $old_limit); // Restore the old memory limit
|
|
|
|
}
|
2021-08-19 06:14:15 +09:00
|
|
|
return true;
|
2021-04-16 19:46:53 +09:00
|
|
|
}
|
2021-08-14 23:00:40 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Event raised when GNU social polls the plugin for information about it.
|
|
|
|
* Adds this plugin's version information to $versions array
|
|
|
|
*
|
2021-09-07 07:47:28 +09:00
|
|
|
* @param array $versions inherited from parent
|
2021-08-14 23:00:40 +09:00
|
|
|
*
|
|
|
|
* @return bool true hook value
|
|
|
|
*/
|
|
|
|
public function onPluginVersion(array &$versions): bool
|
|
|
|
{
|
|
|
|
$versions[] = [
|
|
|
|
'name' => 'ImageEncoder',
|
|
|
|
'version' => $this->version(),
|
|
|
|
'author' => 'Hugo Sales, Diogo Peralta Cordeiro',
|
|
|
|
'homepage' => GNUSOCIAL_PROJECT_URL,
|
|
|
|
'description' => // TRANS: Plugin description.
|
|
|
|
_m('Use VIPS for some additional image support.'),
|
|
|
|
];
|
|
|
|
return Event::next;
|
|
|
|
}
|
2021-04-16 07:47:33 +09:00
|
|
|
}
|