koel/app/Services/ImageWriter.php
2021-06-05 12:47:56 +02:00

39 lines
1,012 B
PHP

<?php
namespace App\Services;
use Intervention\Image\Constraint;
use Intervention\Image\ImageManager;
class ImageWriter
{
private const DEFAULT_MAX_WIDTH = 500;
private const DEFAULT_QUALITY = 80;
private ImageManager $imageManager;
public function __construct(ImageManager $imageManager)
{
$this->imageManager = $imageManager;
}
public function writeFromBinaryData(string $destination, string $data, array $config = []): void
{
$img = $this->imageManager
->make($data)
->resize(
$config['max_width'] ?? self::DEFAULT_MAX_WIDTH,
null,
static function (Constraint $constraint): void {
$constraint->upsize();
$constraint->aspectRatio();
}
);
if (isset($config['blur'])) {
$img->blur($config['blur']);
}
$img->save($destination, $config['quality'] ?? self::DEFAULT_QUALITY);
}
}