feat: better algorythm for image resize
This commit is contained in:
@@ -13,6 +13,12 @@ class ImageFactory
|
|||||||
private string $fullPath;
|
private string $fullPath;
|
||||||
private array $modifications = [];
|
private array $modifications = [];
|
||||||
|
|
||||||
|
private static array $modificatorMethods = [
|
||||||
|
'resize' => [self::class, 'resizeModification'],
|
||||||
|
'cover' => [self::class, 'coverModification'],
|
||||||
|
'contain' => [self::class, 'containModification'],
|
||||||
|
];
|
||||||
|
|
||||||
private string $imageDir;
|
private string $imageDir;
|
||||||
private string $siteUrl;
|
private string $siteUrl;
|
||||||
private ImageManager $manager;
|
private ImageManager $manager;
|
||||||
@@ -75,6 +81,17 @@ class ImageFactory
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function contain(?int $width = null, ?int $height = null, $background = 'ffffff'): self
|
||||||
|
{
|
||||||
|
$this->modifications['contain'] = [
|
||||||
|
'width' => $width,
|
||||||
|
'height' => $height,
|
||||||
|
'background' => $background,
|
||||||
|
];
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function url(?string $format = null, ?int $quality = null): string
|
public function url(?string $format = null, ?int $quality = null): string
|
||||||
{
|
{
|
||||||
$format = $format ?? $this->options['format'];
|
$format = $format ?? $this->options['format'];
|
||||||
@@ -100,6 +117,7 @@ class ImageFactory
|
|||||||
private function buildUrl(string $path): string
|
private function buildUrl(string $path): string
|
||||||
{
|
{
|
||||||
$relativePath = substr($path, strlen($this->imageDir));
|
$relativePath = substr($path, strlen($this->imageDir));
|
||||||
|
|
||||||
return $this->siteUrl . '/image/' . ltrim($relativePath, '/');
|
return $this->siteUrl . '/image/' . ltrim($relativePath, '/');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,9 +179,9 @@ class ImageFactory
|
|||||||
private function applyModifications(): void
|
private function applyModifications(): void
|
||||||
{
|
{
|
||||||
foreach ($this->modifications as $name => $value) {
|
foreach ($this->modifications as $name => $value) {
|
||||||
$method = "{$name}Modification";
|
$method = self::$modificatorMethods[$name][1] ?? null;
|
||||||
if (! method_exists($this, $method)) {
|
if (! $method) {
|
||||||
throw new RuntimeException('Modification method ' . $method . ' does not exist');
|
throw new RuntimeException('Modification for ' . $name . ' does not exist');
|
||||||
}
|
}
|
||||||
$this->{$method}(...array_values($value));
|
$this->{$method}(...array_values($value));
|
||||||
}
|
}
|
||||||
@@ -183,6 +201,14 @@ class ImageFactory
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function containModification(?int $width = null, ?int $height = null, $background = 'ffffff'): void
|
||||||
|
{
|
||||||
|
if ($width || $height) {
|
||||||
|
$this->image->resize($width, $height, static fn($constraint) => $constraint->aspectRatio());
|
||||||
|
$this->image->resizeCanvas($width, $height, 'center', false, $background);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private function ensureDestinationFolterExists(string $newImage): void
|
private function ensureDestinationFolterExists(string $newImage): void
|
||||||
{
|
{
|
||||||
$path = pathinfo($newImage, PATHINFO_DIRNAME);
|
$path = pathinfo($newImage, PATHINFO_DIRNAME);
|
||||||
|
|||||||
@@ -4,12 +4,12 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace App\Handlers;
|
namespace App\Handlers;
|
||||||
|
|
||||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
||||||
use Openguru\OpenCartFramework\Http\Request;
|
use Openguru\OpenCartFramework\Http\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
|
||||||
use Openguru\OpenCartFramework\TeleCartPulse\PulseIngestException;
|
use Openguru\OpenCartFramework\TeleCartPulse\PulseIngestException;
|
||||||
use Openguru\OpenCartFramework\TeleCartPulse\TeleCartPulseService;
|
use Openguru\OpenCartFramework\TeleCartPulse\TeleCartPulseService;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
class TelemetryHandler
|
class TelemetryHandler
|
||||||
|
|||||||
@@ -145,7 +145,7 @@ class ProductsService
|
|||||||
$productsImagesMap = [];
|
$productsImagesMap = [];
|
||||||
foreach ($productsImages as $item) {
|
foreach ($productsImages as $item) {
|
||||||
$productsImagesMap[$item['product_id']][] = [
|
$productsImagesMap[$item['product_id']][] = [
|
||||||
'url' => $this->image->make($item['image'])->resize($imageWidth, $imageHeight)->url(),
|
'url' => $this->image->make($item['image'])->contain($imageWidth, $imageHeight)->url(),
|
||||||
'alt' => 'Product Image',
|
'alt' => 'Product Image',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user