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-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-04-18 05:47:16 +01:00
|
|
|
use Exception;
|
2021-04-16 16:57:25 +01:00
|
|
|
use Jcupitt\Vips;
|
2021-04-18 05:47:16 +01:00
|
|
|
use Symfony\Component\HttpFoundation\File\File as SymfonyFile;
|
2021-04-15 23:47:33 +01:00
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* Keeps only GIF (if animated) and WebP formats
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function preferredType(): int
|
2021-04-15 23:47:33 +01:00
|
|
|
{
|
2021-04-18 05:47:16 +01:00
|
|
|
if ($this->type == IMAGETYPE_GIF && $this->animated) {
|
|
|
|
return $this->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
return IMAGETYPE_WEBP;
|
2021-04-15 23:47:33 +01:00
|
|
|
}
|
2021-04-16 11:46:53 +01:00
|
|
|
|
|
|
|
/**
|
2021-04-18 05:47:16 +01:00
|
|
|
* Encodes the image to self::preferredType() format ensuring it's valid.
|
|
|
|
*
|
|
|
|
* @param SymfonyFile $sfile i/o
|
|
|
|
* @param null|string $mimetype out
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function onAttachmentValidation(SymfonyFile &$sfile, ?string &$mimetype = null): bool
|
|
|
|
{
|
|
|
|
$original_mimetype = $mimetype ?? $sfile->getMimeType();
|
|
|
|
// TODO: Encode in place
|
|
|
|
$mimetype = self::preferredType();
|
|
|
|
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-04-18 05:47:16 +01:00
|
|
|
public function onResizeImage(Attachment $attachment, AttachmentThumbnail $thumbnail, int $width, int $height, bool $crop): 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 {
|
|
|
|
// -1 means load all pages, 'sequential' access means decode pixels on demand
|
|
|
|
// $image = Vips\Image::newFromFile(self::getPath($attachment), ['n' => -1, 'access' => 'sequential']);
|
|
|
|
$image = Vips\Image::thumbnail($attachment->getPath(), $width, ['height' => $height]);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Log::error(__METHOD__ . ' encountered exception: ' . print_r($e, true));
|
|
|
|
// 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-04-16 16:57:25 +01:00
|
|
|
if ($attachment->getPath() === $thumbnail->getPath()) {
|
|
|
|
@unlink($thumbnail->getPath());
|
|
|
|
}
|
2021-04-16 11:46:53 +01:00
|
|
|
|
2021-04-16 16:57:25 +01:00
|
|
|
$image->writeToFile($thumbnail->getPath());
|
|
|
|
unset($image);
|
2021-04-16 11:46:53 +01:00
|
|
|
|
2021-04-16 16:57:25 +01:00
|
|
|
ini_set('memory_limit', $old_limit); // Restore the old memory limit
|
2021-04-16 11:46:53 +01:00
|
|
|
|
|
|
|
return Event::next;
|
|
|
|
}
|
2021-04-15 23:47:33 +01:00
|
|
|
}
|