2020-04-27 20:32:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use Intervention\Image\Constraint;
|
|
|
|
use Intervention\Image\ImageManager;
|
|
|
|
|
|
|
|
class ImageWriter
|
|
|
|
{
|
2020-06-12 13:55:45 +00:00
|
|
|
private const DEFAULT_MAX_WIDTH = 500;
|
|
|
|
private const DEFAULT_QUALITY = 80;
|
2020-04-27 20:32:24 +00:00
|
|
|
|
|
|
|
private $imageManager;
|
|
|
|
|
|
|
|
public function __construct(ImageManager $imageManager)
|
|
|
|
{
|
|
|
|
$this->imageManager = $imageManager;
|
|
|
|
}
|
|
|
|
|
2020-06-12 13:55:45 +00:00
|
|
|
public function writeFromBinaryData(string $destination, string $data, array $config = []): void
|
2020-04-27 20:32:24 +00:00
|
|
|
{
|
2020-06-12 15:05:18 +00:00
|
|
|
$img = $this->imageManager
|
2020-04-27 20:32:24 +00:00
|
|
|
->make($data)
|
2020-06-12 13:55:45 +00:00
|
|
|
->resize(
|
|
|
|
$config['max_width'] ?? self::DEFAULT_MAX_WIDTH,
|
|
|
|
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);
|
2020-04-27 20:32:24 +00:00
|
|
|
}
|
|
|
|
}
|