Skip to content

add dpr compatibility #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Optimole.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class Optimole
/**
* The Optimole SDK version.
*/
public const VERSION = '1.2.1';
public const VERSION = '1.2.2';

/**
* The Optimole dashboard API URL.
Expand Down
10 changes: 10 additions & 0 deletions src/Resource/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@

class Image extends AbstractResource
{
/**
* Set the dpr of the optimized image.
*/
public function dpr($dpr = 1): self
{
$this->addProperty(new ImageProperty\DprProperty($dpr));

return $this;
}

/**
* Convert the optimized image to the given format.
*/
Expand Down
46 changes: 46 additions & 0 deletions src/Resource/ImageProperty/DprProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

/*
* This file is part of Optimole PHP SDK.
*
* (c) Optimole Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Optimole\Sdk\Resource\ImageProperty;

use Optimole\Sdk\Exception\InvalidArgumentException;
use Optimole\Sdk\Resource\PropertyInterface;

class DprProperty implements PropertyInterface
{
/**
* The dpr of the image.
*/
private $dpr;

/**
* Constructor.
*/
public function __construct($dpr = 1)
{
if (!is_int($dpr) || $dpr < 1) {
throw new InvalidArgumentException('Image dpr must be an integer greater or equal than 1.');
}
$dpr = max(1, min(5, $dpr));

$this->dpr = $dpr;
}

/**
* {@inheritdoc}
*/
public function __toString(): string
{
return sprintf('dpr:%s', $this->dpr);
}
}
21 changes: 21 additions & 0 deletions tests/Unit/Resource/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@

class ImageTest extends TestCase
{
public function testDprAddsDprPropriety(): void
{
$image = (new Image('domain', 'source'))->dpr(2);

$this->assertSame('https://domain/dpr:2/source', (string) $image);
}

public function testDprAddsDprProprietyDefault(): void
{
$image = (new Image('domain', 'source'))->dpr(1);

$this->assertSame('https://domain/dpr:1/source', (string) $image);
}

public function testDprAddsDprProprietyHigh(): void
{
$image = (new Image('domain', 'source'))->dpr(10);

$this->assertSame('https://domain/dpr:5/source', (string) $image);
}

public function testFormatAddsFormatProperty(): void
{
$image = (new Image('domain', 'source'))->format('webp');
Expand Down