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