Skip to content

Commit

Permalink
add resize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OxCom committed Jul 12, 2023
1 parent bed5fcc commit 12a66ea
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 41 deletions.
38 changes: 1 addition & 37 deletions src/Providers/ImgProxy/Builder/Processing/Resize.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,12 @@

class Resize implements ProcessOptionInterface
{
// private const RESIZE_TPL = 'rs:%resizing_type:%width:%height:%enlarge:%extend';
private const RESIZE_TPL = 'rs:%resizing_type:%width:%height';

protected string $type;
protected string $algo;

public function __construct(
protected int $width = 0,
protected int $height = 0,
string $type = ImgProxy::RESIZE_TYPE_FIT,
string $algo = ImgProxy::RESIZE_ALGO_LANCZOS3
protected string $type = ImgProxy::RESIZE_TYPE_FIT
) {
$this
->type($type)
->algo($algo);
}

public function type(string $type): self
{
$allowed = [
ImgProxy::RESIZE_TYPE_FIT,
ImgProxy::RESIZE_TYPE_FILL,
Expand All @@ -37,29 +24,6 @@ public function type(string $type): self
if (!\in_array($type, $allowed, true)) {
throw new \InvalidArgumentException(\sprintf("Resize type '%s' is not allowed", $type));
}

$this->type = $type;

return $this;
}

public function algo(string $algo): self
{
$allowed = [
ImgProxy::RESIZE_ALGO_LANCZOS3,
ImgProxy::RESIZE_ALGO_LANCZOS2,
ImgProxy::RESIZE_ALGO_NEAREST,
ImgProxy::RESIZE_ALGO_LINEAR,
ImgProxy::RESIZE_ALGO_CUBIC,
];

if (!\in_array($algo, $allowed, true)) {
throw new \InvalidArgumentException(\sprintf("Resize algorithm '%s' is not allowed", $algo));
}

$this->algo = $algo;

return $this;
}

public function compile(): string
Expand Down
34 changes: 34 additions & 0 deletions src/Providers/ImgProxy/Builder/Processing/ResizeAlgo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace SymfonyImageProxyBundle\Providers\ImgProxy\Builder\Processing;

use SymfonyImageProxyBundle\Providers\ImgProxy\Builder\ProcessOptionInterface;
use SymfonyImageProxyBundle\Providers\ImgProxy\ImgProxy;

class ResizeAlgo implements ProcessOptionInterface
{
private const RESIZE_TPL = 'ra:%algorithm';

public function __construct(
protected string $algo = ImgProxy::RESIZE_ALGO_LANCZOS3,
) {
$allowed = [
ImgProxy::RESIZE_ALGO_LANCZOS3,
ImgProxy::RESIZE_ALGO_LANCZOS2,
ImgProxy::RESIZE_ALGO_NEAREST,
ImgProxy::RESIZE_ALGO_LINEAR,
ImgProxy::RESIZE_ALGO_CUBIC,
];

if (!\in_array($algo, $allowed, true)) {
throw new \InvalidArgumentException(\sprintf("Resize algorithm '%s' is not allowed", $algo));
}
}

public function compile(): string
{
return \strtr(self::RESIZE_TPL, [
'%algorithm' => $this->algo,
]);
}
}
11 changes: 9 additions & 2 deletions src/Providers/ImgProxy/Builder/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use SymfonyImageProxyBundle\Providers\ImgProxy\Builder\Processing\ExtendAspectRatio;
use SymfonyImageProxyBundle\Providers\ImgProxy\Builder\Processing\Gravity;
use SymfonyImageProxyBundle\Providers\ImgProxy\Builder\Processing\Resize;
use SymfonyImageProxyBundle\Providers\ImgProxy\Builder\Processing\ResizeAlgo;
use SymfonyImageProxyBundle\Providers\ImgProxy\Builder\Processing\Zoom;
use SymfonyImageProxyBundle\Providers\ImgProxy\ImgProxy;
use SymfonyImageProxyBundle\Providers\ImgProxy\Security;
Expand All @@ -30,9 +31,15 @@ public function resize(
int $width = 0,
int $height = 0,
string $type = ImgProxy::RESIZE_TYPE_FIT,
string $algo = ImgProxy::RESIZE_ALGO_LANCZOS3
): self {
$this->process[Resize::class] = new Resize($width, $height, $type, $algo);
$this->process[Resize::class] = new Resize($width, $height, $type);

return $this;
}

public function resizeAlgo(string $algo): self
{
$this->process[ResizeAlgo::class] = new ResizeAlgo($algo);

return $this;
}
Expand Down
46 changes: 44 additions & 2 deletions tests/Providers/ImgProxy/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,50 @@ public function testResize()
);

self::assertEquals(
'https://conv.awesome.com/unsafe/rs:fit:33:42/plain/https://awesome.com/awesome/image.jpg@webp',
$url->toWebP()
'https://conv.awesome.com/unsafe/rs:fill:33:0/plain/https://awesome.com/awesome/image.jpg@webp',
$url->resize(33, 0, ImgProxy::RESIZE_TYPE_FILL)->toWebP()
);
}

public function testResizeAlgo()
{
$img = 'https://awesome.com/awesome/image.jpg';
$security = new Security('617765736F6D65', '6F78636F6D');
$builder = new Builder($security, 'conv.awesome.com');

$url = $builder
->url($img)
->resize(33, 42)
->resizeAlgo(ImgProxy::RESIZE_ALGO_NEAREST)
->toWebP();

self::assertEquals(
'https://conv.awesome.com/unsafe/rs:fit:33:42/ra:nearest/plain/https://awesome.com/awesome/image.jpg@webp',
$url
);
}

public function testZoom()
{
$img = 'https://awesome.com/awesome/image.jpg';
$security = new Security('617765736F6D65', '6F78636F6D');
$builder = new Builder($security, 'conv.awesome.com');

$url = $builder->url($img);

self::assertEquals(
'https://conv.awesome.com/unsafe/z:0.5/plain/https://awesome.com/awesome/image.jpg@webp',
$url->zoom(0.5)->toWebP()
);

self::assertEquals(
'https://conv.awesome.com/unsafe/z:0.5:0.7/plain/https://awesome.com/awesome/image.jpg@webp',
$url->zoom(0.5,0.7)->toWebP()
);

self::assertEquals(
'https://conv.awesome.com/unsafe/z:0.33/plain/https://awesome.com/awesome/image.jpg@webp',
$url->zoom(0.33, 0.33)->toWebP()
);
}
}

0 comments on commit 12a66ea

Please sign in to comment.