Skip to content

Commit

Permalink
Merge pull request #8 from mapado/nr-update_with_new_server_features
Browse files Browse the repository at this point in the history
Server is now http2 + Allow resize based on height
  • Loading branch information
Nickinthebox authored May 31, 2024
2 parents 236da46 + 2497508 commit caa688a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 35 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
31 changes: 23 additions & 8 deletions Tests/Units/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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()
Expand All @@ -110,14 +114,25 @@ 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()
{
$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));
}

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);
}
}
40 changes: 13 additions & 27 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string|int> $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);
Expand All @@ -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 || !empty($options))) {
$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;
Expand All @@ -78,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) {
Expand All @@ -90,19 +91,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;
}
}

0 comments on commit caa688a

Please sign in to comment.