vips conversion

pull/6650/head
Daniels Šatcs 4 months ago
parent c8bed78bee
commit ba3f450061
No known key found for this signature in database
GPG Key ID: 4B974CC26375BB06

@ -5,11 +5,9 @@ namespace App\Util\Media;
use App\Media;
use App\Services\StatusService;
use Cache;
use Intervention\Image\Encoders\JpegEncoder;
use Intervention\Image\Encoders\PngEncoder;
use Intervention\Image\Encoders\WebpEncoder;
use Log;
use Storage;
use Jcupitt\Vips;
class Image
{
@ -117,6 +115,7 @@ class Image
public function handleImageTransform(Media $media, $thumbnail = false)
{
$path = $media->media_path;
$targetFormat = StorageFormat::getConfigured();
$localFs = config('filesystems.default') === 'local';
if (! in_array($media->mime, $this->acceptedMimes)) {
@ -135,7 +134,7 @@ class Image
}
$fileInfo = pathinfo($path);
$extension = strtolower($fileInfo['extension'] ?? 'jpg');
$extension = $targetFormat->extension();
$outputExtension = $extension;
$metadata = null;
@ -202,69 +201,45 @@ class Image
}
}
$img = $this->imageManager->read($fileContents);
$img = Vips\Image::newFromBuffer($fileContents);
$ratio = $this->getAspect($img->width(), $img->height(), $thumbnail);
$ratio = $this->getAspect($img->width, $img->height, $thumbnail);
$aspect = $ratio['dimensions'];
$orientation = $ratio['orientation'];
if ($thumbnail) {
$img = $img->coverDown(
$aspect['width'],
$aspect['height']
);
$img = Vips\Image::thumbnail_buffer($fileContents, $aspect['width'], [
'height' => $aspect['height'],
'crop' => 'centre',
'size' => 'down',
]);
} else {
if (
($ratio['width_original'] > $aspect['width'])
|| ($ratio['height_original'] > $aspect['height'])
) {
$img = $img->scaleDown(
$aspect['width'],
$aspect['height']
);
$img = Vips\Image::thumbnail_buffer($fileContents, $aspect['width'], [
'height' => $aspect['height'],
'size' => 'down',
]);
} else {
$img = Vips\Image::newFromBuffer($fileContents);
}
}
$quality = config_cache('pixelfed.image_quality');
$encoder = null;
switch ($extension) {
case 'jpeg':
case 'jpg':
$encoder = new JpegEncoder($quality);
$outputExtension = 'jpg';
break;
case 'png':
$encoder = new PngEncoder;
$outputExtension = 'png';
break;
case 'webp':
$encoder = new WebpEncoder($quality);
$outputExtension = 'webp';
break;
case 'avif':
$encoder = new JpegEncoder($quality);
$outputExtension = 'jpg';
break;
case 'heic':
$encoder = new JpegEncoder($quality);
$outputExtension = 'jpg';
break;
default:
$encoder = new JpegEncoder($quality);
$outputExtension = 'jpg';
}
$converted = $this->setBaseName($path, $thumbnail, $outputExtension);
$encoded = $encoder->encode($img);
$encoded = $targetFormat->saveBuffer($img);
if ($localFs) {
$newPath = storage_path('app/'.$converted['path']);
file_put_contents($newPath, $encoded->toString());
file_put_contents($newPath, $encoded);
} else {
Storage::disk($this->defaultDisk)->put(
$converted['path'],
$encoded->toString()
$encoded
);
}
@ -272,11 +247,11 @@ class Image
$media->thumbnail_path = $converted['path'];
$media->thumbnail_url = url(Storage::url($converted['path']));
} else {
$media->width = $img->width();
$media->height = $img->height();
$media->width = $img->width;
$media->height = $img->height;
$media->orientation = $orientation;
$media->media_path = $converted['path'];
$media->mime = 'image/'.$outputExtension;
$media->mime = $targetFormat->mimeType();
}
$media->save();
@ -305,7 +280,7 @@ class Image
$filename = $pathInfo['filename'];
$name = ($thumbnail == true) ? $filename . '_thumb' : $filename;
$basePath = $dir . $name . '.' . $extension;
return ['path' => $basePath, 'png' => false];
}

@ -0,0 +1,43 @@
<?php
namespace App\Util\Media;
enum StorageFormat: string
{
case JPEG = 'jpeg';
case JXL = 'jxl';
public function extension(): string
{
return match($this) {
self::JPEG => 'jpg',
self::JXL => 'jxl',
};
}
/**
* Get the appropriate MIME type for the format.
*/
public function mimeType(): string
{
return match($this) {
self::JPEG => 'image/jpeg',
self::JXL => 'image/jxl',
};
}
public function saveBuffer(\Jcupitt\Vips\Image $media): string
{
return match($this) {
self::JPEG => $media->uhdrsave_buffer(),
self::JXL => $media->jxlsave_buffer(),
};
}
static function getConfigured(): StorageFormat
{
$key = config_cache("pixelfed.image_storage_format", 'jpeg');
return StorageFormat::from($key);
}
}

@ -18,8 +18,9 @@
"buzz/laravel-h-captcha": "^1.0.4",
"endroid/qr-code": "^6.0",
"guzzlehttp/guzzle": "^7.10",
"intervention/image-laravel": "^1.5",
"intervention/image-driver-vips": "^1.0",
"intervention/image-laravel": "^1.5",
"jcupitt/vips": "^2.6",
"jenssegers/agent": "^2.6",
"laravel-notification-channels/expo": "^2.0.0",
"laravel-notification-channels/webpush": "^10.2",

@ -159,6 +159,16 @@ return [
*/
'image_quality' => (int) env('IMAGE_QUALITY', 80),
/*
|--------------------------------------------------------------------------
| Image Storage Format
|--------------------------------------------------------------------------
|
| Set the image storage format. All uploads will be converted to this format.
|
*/
'image_storage_format' => (string) env('IMAGE_STORAGE_FORMAT', \App\Util\Media\StorageFormat::JPEG->name),
/*
|--------------------------------------------------------------------------
| Account deletion

Loading…
Cancel
Save