From 32aaab5a8eec55a0097eb3be19b18f7f30e3526e Mon Sep 17 00:00:00 2001 From: Nicolas Rosset Date: Wed, 29 May 2024 13:47:06 +0000 Subject: [PATCH 1/5] Server is now http2 (useless to use img/img1) + allow resize based on height --- Tests/Units/BuilderTest.php | 20 ++++++++++++-------- src/Builder.php | 24 +++--------------------- 2 files changed, 15 insertions(+), 29 deletions(-) diff --git a/Tests/Units/BuilderTest.php b/Tests/Units/BuilderTest.php index 48ed488..ddc5dc3 100644 --- a/Tests/Units/BuilderTest.php +++ b/Tests/Units/BuilderTest.php @@ -25,11 +25,11 @@ public function testNonChangingUrl(): void $url = $this->manager->buildUrl($baseUrl); $this->assertSame($baseUrl, $url); - $baseUrl = '//img1.mapado.net/2014/1/1/my-image.png'; + $baseUrl = '//img.mapado.net/2014/1/1/my-image.png'; $url = $this->manager->buildUrl($baseUrl); $this->assertSame($baseUrl, $url); - $baseUrl = '//img2.mapado.net/2014/1/1/my-image.png'; + $baseUrl = '//img.mapado.net/2014/1/1/my-image.png'; $url = $this->manager->buildUrl($baseUrl); $this->assertSame($baseUrl, $url); } @@ -39,11 +39,15 @@ public function testThumbs() { $baseUrl = '//img.mapado.net/2014/1/1/my-image'; $url = $this->manager->buildUrl($baseUrl, 200); - $this->assertSame($baseUrl . '_thumbs/200', $url); + $this->assertSame($baseUrl . '_thumbs/200-0', $url); + + $baseUrl = '//img.mapado.net/2014/1/1/my-image'; + $url = $this->manager->buildUrl($baseUrl, 0, 200); + $this->assertSame($baseUrl . '_thumbs/0-200', $url); $baseUrl = '//img.mapado.net/2014/1/1/my-image.png'; $url = $this->manager->buildUrl($baseUrl, 200); - $this->assertSame($baseUrl . '_thumbs/200.png', $url); + $this->assertSame($baseUrl . '_thumbs/200-0.png', $url); $baseUrl = '//img.mapado.net/2014/1/1/my-image.png'; $url = $this->manager->buildUrl($baseUrl, 200, 150); @@ -86,7 +90,7 @@ public function testNoDomain() { $baseUrl = '2014/1/1/my-image.png'; $url = $this->manager->buildUrl($baseUrl); - $this->assertSame('//img1.mapado.net/' . $baseUrl, $url); + $this->assertSame('//img.mapado.net/' . $baseUrl, $url); $baseUrl = '2014/1/2/my-image.png'; $url = $this->manager->buildUrl($baseUrl); @@ -100,7 +104,7 @@ public function testBuildHttpUrl() ->withHttpPrefix() ->buildUrl($baseUrl); - $this->assertSame('http://img1.mapado.net/' . $baseUrl, $url); + $this->assertSame('http://img.mapado.net/' . $baseUrl, $url); } public function testBuildHttpsUrl() @@ -110,7 +114,7 @@ public function testBuildHttpsUrl() ->withHttpsPrefix() ->buildUrl($baseUrl); - $this->assertSame('https://img1.mapado.net/' . $baseUrl, $url); + $this->assertSame('https://img.mapado.net/' . $baseUrl, $url); } public function testBuildHttpDoesNotInterferWithInstance() @@ -118,6 +122,6 @@ public function testBuildHttpDoesNotInterferWithInstance() $baseUrl = '2014/1/1/my-image.png'; $this->manager->withHttpPrefix(); - $this->assertSame('//img1.mapado.net/' . $baseUrl, $this->manager->buildUrl($baseUrl)); + $this->assertSame('//img.mapado.net/' . $baseUrl, $this->manager->buildUrl($baseUrl)); } } diff --git a/src/Builder.php b/src/Builder.php index bbfbd2a..1c8cf80 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -49,21 +49,18 @@ public function buildUrl( } if (null !== $image && !preg_match('#^//img([1-3])?.mapado.net/#', $image)) { - $host = $this->getHost($image); + $host = '//img.mapado.net/'; $image = $host . $image; } - if (null !== $image && $width > 0) { + if (null !== $image && (null !== $width || null !== $height)) { $extension = pathinfo($image, PATHINFO_EXTENSION); $extLen = strlen($extension); if ($extLen > 4) { $extension = null; } - $image .= '_thumbs/' . $width; - if ($height > 0) { - $image .= '-' . $height; - } + $image .= '_thumbs/' . ($width ?? 0) . '-' . ($height ?? 0); if (!empty($options['cropWidth']) || !empty($options['cropHeight'])) { $cropWidth = $options['cropWidth'] ?? 0; @@ -90,19 +87,4 @@ public function buildUrl( return $this->prefix . $image; } - - private function getHost(string $image): string - { - $matches = []; - preg_match('#^[0-9]{4}/[0-9]{1,2}/([0-9]{1,2})#', $image, $matches); - if (!empty($matches)) { - $shard = (int) $matches[1] % 2; - $shard = $shard ?: ''; // remove "0" - } else { - $shard = ''; - } - $host = '//img' . $shard . '.mapado.net/'; - - return $host; - } } From be5e7edc6fc6004acbbe29d4dc92ab41513b0895 Mon Sep 17 00:00:00 2001 From: Nicolas Rosset Date: Wed, 29 May 2024 14:03:19 +0000 Subject: [PATCH 2/5] Make width or height null by default --- src/Builder.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Builder.php b/src/Builder.php index 1c8cf80..93bb17c 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -33,14 +33,14 @@ public function withHttpsPrefix(): self * generate an image url from its slug * * @param string $imageSlug the image slug (ie. `/2018/01/foo.jpg`) - * @param int $width the output width - * @param int $height the output height + * @param ?int $width the output width + * @param ?int $height the output height * @param array $options accept cropWidth, cropHeight or url options parameters (like `rcr`, etc.) */ public function buildUrl( string $imageSlug, - int $width = 0, - int $height = 0, + ?int $width = null, + ?int $height = null, array $options = [] ): string { $image = trim($imageSlug); From 1f48ca3bca699f05acdf3ce94ac9afc87521f429 Mon Sep 17 00:00:00 2001 From: Nicolas Rosset Date: Thu, 30 May 2024 12:47:55 +0000 Subject: [PATCH 3/5] create an "allowwebp" option to automatically set width and height to 0 + allow options without resizing --- src/Builder.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Builder.php b/src/Builder.php index 93bb17c..d8ac9ab 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -53,7 +53,7 @@ public function buildUrl( $image = $host . $image; } - if (null !== $image && (null !== $width || null !== $height)) { + if (null !== $image && (null !== $width || null !== $height || !empty($options))) { $extension = pathinfo($image, PATHINFO_EXTENSION); $extLen = strlen($extension); if ($extLen > 4) { @@ -75,9 +75,13 @@ public function buildUrl( $optionValues = []; ksort($options); foreach ($options as $key => $value) { - $optionValues[] = $key . '=' . $value; + if ($key !== 'allowwebp') { + $optionValues[] = $key . '=' . $value; + } + } + if (!empty($optionValues)) { + $image .= '.' . implode(';', $optionValues); } - $image .= '.' . implode(';', $optionValues); } if ($extension) { From 722ad19c446d03d573388229a95aa08080e8476b Mon Sep 17 00:00:00 2001 From: Nicolas Rosset Date: Fri, 31 May 2024 08:03:39 +0000 Subject: [PATCH 4/5] Test allowwebp --- Tests/Units/BuilderTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Tests/Units/BuilderTest.php b/Tests/Units/BuilderTest.php index ddc5dc3..61d6c46 100644 --- a/Tests/Units/BuilderTest.php +++ b/Tests/Units/BuilderTest.php @@ -124,4 +124,15 @@ public function testBuildHttpDoesNotInterferWithInstance() $this->assertSame('//img.mapado.net/' . $baseUrl, $this->manager->buildUrl($baseUrl)); } + + public function testAllowwebp(): void + { + $baseUrl = '//img.mapado.net/2014/1/1/my-image.png'; + $url = $this->manager->buildUrl($baseUrl, null, null); + $this->assertSame($baseUrl, $url); + + $baseUrl = '//img.mapado.net/2014/1/1/my-image.png'; + $url = $this->manager->buildUrl($baseUrl, null, null, ['allowwebp' => 1]); + $this->assertSame($baseUrl . '_thumbs/0-0.png', $url); + } } From 249750881b2afec100f365f13ed9ca3921c58111 Mon Sep 17 00:00:00 2001 From: Nicolas Rosset Date: Fri, 31 May 2024 10:30:26 +0000 Subject: [PATCH 5/5] Update changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61b182e..06cb817 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 2.2.0 + +Remove use of img/img1 as we are now multiplexing with http2 +Add option allowwebp to enable webp optimization even for not resized images +Allow =0 width, with >= height to resize based on set height + ## 2.1.1 Fix malformed imageSlug parameter in buildUrl