koel/app/Services/ImageWriter.php

40 lines
1,012 B
PHP
Raw Normal View History

<?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;
2021-06-05 10:47:56 +00:00
private ImageManager $imageManager;
public function __construct(ImageManager $imageManager)
{
$this->imageManager = $imageManager;
}
public function writeFromBinaryData(string $destination, string $data, array $config = []): void
{
2020-06-12 15:05:18 +00:00
$img = $this->imageManager
->make($data)
->resize(
$config['max_width'] ?? self::DEFAULT_MAX_WIDTH,
2020-12-22 20:11:22 +00:00
null,
static function (Constraint $constraint): void {
$constraint->upsize();
$constraint->aspectRatio();
}
2020-06-12 15:05:18 +00:00
);
if (isset($config['blur'])) {
$img->blur($config['blur']);
}
$img->save($destination, $config['quality'] ?? self::DEFAULT_QUALITY);
}
}