Skip to content

Commit

Permalink
Merge branch 'feature/rewrite' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
spescina committed Apr 28, 2014
2 parents 9097d58 + 035bff3 commit 0353ebd
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 8 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Add the service provider in the `app/config/app.php` file

Publish the package assets running `php artisan asset:publish spescina/imgproxy`

Publish the package config running `php artisan config:publish spescina/imgproxy`

Use the package facade to generate the resource url
```
ImgProxy::link("path/to/image.jpg", 100, 80)
Expand Down Expand Up @@ -50,7 +52,16 @@ These are the supported values

## Config

After publishing the assets it's possible to edit package config in the `public/packages/spescina/imgproxy/timthumb-config.php` file.
### Package config

After publishing the package config file it's possible to change the package behaviour in the `app/config/packages/spescina/imgproxy/config.php` file.

These are the current options:
* __rewrite__ - Default: `true` - If `false` querystring uri are generated instead of pretty ones

### Timthumb config

It's possible to edit timthumb config in the `public/packages/spescina/imgproxy/timthumb-config.php` file.

These, at the moment, are the default values
```
Expand All @@ -73,4 +84,4 @@ define ("ERROR_IMAGE", "./nophoto.gif");
define ("PNG_IS_TRANSPARENT", FALSE);
define ("DEFAULT_Q", 90);
```
```
36 changes: 33 additions & 3 deletions src/Spescina/Imgproxy/Imgproxy.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php namespace Spescina\Imgproxy;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\URL;

class Imgproxy {

const PREFIX = 'packages/spescina/imgproxy';
const FILE = 'timthumb.php';

const Q = 90;
const ZC = 1;
Expand All @@ -15,9 +17,37 @@ public function link($path, $width, $height, $quality = null, $zoomCrop = null)

$zc = is_null($zoomCrop) ? self::ZC : $zoomCrop;

$url = array(self::PREFIX, $width, $height, $zc, $q, $path);

return URL::to(implode('/', $url));
$url = array(
'base' => self::PREFIX,
'w' => $width,
'h' => $height,
'zc' => $zc,
'q' => $q,
'src' => $path
);

return $this->getUrl($url);

}

private function getUrl($url)
{
return Config::get('imgproxy::rewrite') ? $this->build_rewrited($url) : $this->build_urlencoded($url);
}

private function build_urlencoded($url)
{
$base = array_shift($url);

return URL::to($base . '/' . self::FILE . '?' . urldecode(http_build_query($url)));

}

private function build_rewrited($url)
{
$values = array_values($url);

return URL::to(implode('/', $values));
}

}
5 changes: 5 additions & 0 deletions src/config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return array(
'rewrite' => true
);
45 changes: 42 additions & 3 deletions tests/ImgproxyTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\URL;
use Spescina\Imgproxy\Imgproxy;

Expand All @@ -12,8 +13,14 @@ public function tearDown()
m::close();
}

public function test_image_link_building()
/** @test */
public function it_generates_rewrited_url()
{
Config::shouldReceive('get')
->once()
->with("imgproxy::rewrite")
->andReturn(true);

URL::shouldReceive('to')
->once()
->with("packages/spescina/imgproxy/100/70/1/90/image/path/url.jpg")
Expand All @@ -26,8 +33,14 @@ public function test_image_link_building()
$this->assertEquals("http://www.example.com/packages/spescina/imgproxy/100/70/1/90/image/path/url.jpg", $url);
}

public function test_image_link_building_with_quality()
/** @test */
public function it_generates_rewrited_url_with_quality()
{
Config::shouldReceive('get')
->once()
->with("imgproxy::rewrite")
->andReturn(true);

URL::shouldReceive('to')
->once()
->with("packages/spescina/imgproxy/100/70/1/70/image/path/url.jpg")
Expand All @@ -40,8 +53,14 @@ public function test_image_link_building_with_quality()
$this->assertEquals("http://www.example.com/packages/spescina/imgproxy/100/70/1/70/image/path/url.jpg", $url);
}

public function test_image_link_building_with_quality_and_zoomcrop()
/** @test */
public function it_generates_rewrited_url_with_quality_and_zoomcrop()
{
Config::shouldReceive('get')
->once()
->with("imgproxy::rewrite")
->andReturn(true);

URL::shouldReceive('to')
->once()
->with("packages/spescina/imgproxy/100/70/2/70/image/path/url.jpg")
Expand All @@ -53,5 +72,25 @@ public function test_image_link_building_with_quality_and_zoomcrop()

$this->assertEquals("http://www.example.com/packages/spescina/imgproxy/100/70/2/70/image/path/url.jpg", $url);
}

/** @test */
public function it_generates_not_rewrite_url_if_config_value_is_false()
{
Config::shouldReceive('get')
->once()
->with("imgproxy::rewrite")
->andReturn(false);

URL::shouldReceive('to')
->once()
->with("packages/spescina/imgproxy/timthumb.php?w=100&h=70&zc=1&q=90&src=image/path/url.jpg")
->andReturn("http://www.example.com/packages/spescina/imgproxy/timthumb.php?w=100&h=70&zc=1&q=90&src=image/path/url.jpg");

$imgProxy = new Imgproxy;

$url = $imgProxy->link("image/path/url.jpg", 100, 70);

$this->assertEquals("http://www.example.com/packages/spescina/imgproxy/timthumb.php?w=100&h=70&zc=1&q=90&src=image/path/url.jpg", $url);
}

}

0 comments on commit 0353ebd

Please sign in to comment.