Skip to content

Commit

Permalink
Implement DPR option
Browse files Browse the repository at this point in the history
  • Loading branch information
arjanwestdorp committed May 17, 2018
1 parent 578cfae commit 759be61
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ public function width($width)
return $this;
}

/**
* Set the desired dpr of the image.
*
* @param mixed $dpr
* @return $this
*/
public function dpr($dpr = 'auto')
{
$this->manipulations['dpr'] = $dpr;

return $this;
}

/**
* Get the string representation of all manipulations that
* need to be executed on the given resource.
Expand Down
25 changes: 24 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class Client
* @var array
*/
private $config;
/**
* @var array
*/
protected $defaults = [];

/**
* Client constructor.
Expand Down Expand Up @@ -81,14 +85,33 @@ public function fetch($url)
{
$this->builder = new Builder(urlencode($url));

foreach ($this->defaults as $key => $value) {
if (method_exists($this->builder, $key)) {
call_user_func([$this->builder, $key], $value);
}
}

return $this;
}

/**
* Set the default dpr.
*
* @param string $key
* @param mixed $value
* @return $this
*/
public function default($key, $value)
{
$this->defaults[$key] = $value;

return $this;
}

/**
* Retrieve the imaginary url.
*
* @return string
* @throws InvalidConfigurationException
*/
public function url()
{
Expand Down
40 changes: 40 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ function it_can_generate_a_url_with_a_radius_manipulation()
$this->assertContains('r_50', $url);
}

/** @test */
function it_can_generate_a_url_with_a_dpr_modifier()
{
$url = $this->client->fetch('https://www.google.com/logo.jpg')
->dpr(3)
->url();

$this->assertContains('dpr_3', $url);
}

/** @test */
function it_can_generate_a_url_to_fetch_an_image_with_multiple_manipulations()
{
Expand Down Expand Up @@ -189,4 +199,34 @@ function it_will_be_possible_to_use_the_same_client_for_multiple_fetches_on_diff
$third = $this->client->fetch($path = 'https://www.google.com/logo.jpg')->width(200)->height(200);
$this->assertEquals('https://imaginary.com/imaginary/images/fetch/w_200,h_200/' . urlencode($path), $third->url());
}

/** @test */
function it_can_have_a_globally_defined_dpr_and_will_use_that_by_default()
{
$this->client->default('dpr', 2);

$url = $this->client->fetch('https://www.google.com/logo.jpg')
->url();

$this->assertContains('dpr_2', $url);

$url = $this->client->fetch('https://www.google.com/logo.jpg')
->dpr(4)
->url();

$this->assertContains('dpr_4', $url);
}

/** @test */
public function it_can_set_default_values_for_the_builder()
{
$this->client->default('dpr', 2);
$this->client->default('invalid', 'value');

$url = $this->client->fetch('https://www.google.com/logo.jpg')
->url();

$this->assertContains('dpr_2', $url);
$this->assertNotContains('invalid', $url);
}
}

0 comments on commit 759be61

Please sign in to comment.