Skip to content
Open
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: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.gitattributes export-ignore
.gitignore export-ignore
.travis.yml export-ignore
examples export-ignore
tests export-ignore
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor
/composer.lock
24 changes: 24 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
language: php
php:
- 5.3.3
- 5.4
- 5.5
- 5.6
- hhvm

matrix:
allow_failures:
- php: hhvm

script:
- vendor/bin/tester tests -s -c tests/php-unix.ini
- php code-checker/src/code-checker.php -d src

after_failure:
# Print *.actual content
- for i in $(find tests -name \*.actual); do echo "--- $i"; cat $i; echo; echo; done

before_script:
# Install Nette Tester & Code Checker
- composer install --no-interaction --dev --prefer-source
- composer create-project nette/code-checker code-checker ~2.2 --no-interaction --prefer-source
15 changes: 9 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
"source": "https://github.com/dotblue/nette-webimages"
},
"require": {
"php": ">=5.4",
"nette/application": ">=2.2.0,<2.3.0",
"nette/di": ">=2.2.0,<2.3.0",
"nette/http": ">=2.2.0,<2.3.0",
"latte/latte": ">=2.2.0,<2.3.0",
"nette/utils": ">=2.2.0,<2.3.0"
"php": ">=5.3.3",
"nette/application": "~2.2.0",
"nette/di": "~2.2.0",
"nette/http": "~2.2.0",
"latte/latte": "~2.2.0",
"nette/utils": "~2.2.0"
},
"require-dev": {
"nette/tester": "@dev"
},
"autoload": {
"classmap": ["src/"]
Expand Down
6 changes: 2 additions & 4 deletions examples/DefaultImageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,19 @@ class DefaultImageProvider implements IProvider
private $wwwDir;



public function __construct($wwwDir)
{
$this->wwwDir = $wwwDir;
}



public function getImage($id, $width, $height, $algorithm = NULL)
public function getImage($id, $width, $height, $flags = NULL)
{
$path = $this->wwwDir . '/originals/' . $id . '.jpg';

if (is_file($path)) {
$image = Image::fromFile($path);
$image->resize($width, $height, $algorithm);
$image->resize($width, $height, $flags);
return $image;
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/FakeImageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class FakeImageProvider implements IProvider
{

public function getImage($id, $width, $height, $algorithm = NULL)
public function getImage($id, $width, $height, $flags = NULL)
{
$source = "http://fakeimg.pl/{$width}x{$height}";
return Image::fromString(file_get_contents($source));
Expand Down
1 change: 1 addition & 0 deletions examples/config.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extensions:
webimages: DotBlue\WebImages\Extension


webimages:
providers:
- DefaultImageProvider(%wwwDir%)
Expand Down
14 changes: 8 additions & 6 deletions examples/route-with-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

$router = $container->getService('router');

$router[] = new DotBlue\WebImages\Route('images/<id>-<width>x<height>.jpg', [
'id' => [
DotBlue\WebImages\Route::FILTER_IN => function ($slug) {
$route = new DotBlue\WebImages\Application\Route('images/<id>-<width>x<height>.jpg', array(
'id' => array(
DotBlue\WebImages\Application\Route::FILTER_IN => function($slug) {
// ...
},
DotBlue\WebImages\Route::FILTER_OUT => function ($id) {
DotBlue\WebImages\Application\Route::FILTER_OUT => function($id) {
// ...
},
],
], $container->getByType('DotBlue\WebImages\Generator'));
),
), $container->getByType('DotBlue\WebImages\Generator'));

DotBlue\WebImages\Helpers::prependRoute($router, $route);
12 changes: 6 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#### Requirements

- PHP 5.4+
- PHP 5.3.3+
- [nette/application](https://github.com/nette/application) >= 2.2
- [nette/di](https://github.com/nette/di) >= 2.2
- [nette/http](https://github.com/nette/http) >= 2.2
Expand All @@ -15,9 +15,9 @@ $ composer require dotblue/nette-webimages@~1.0
```

2) Register as Configurator's extension:
```
```yml
extensions:
webimages: DotBlue\WebImages\Extension
webimages: DotBlue\WebImages\DI\Extension
```

## Concept
Expand All @@ -39,15 +39,15 @@ First, you have to define your `DotBlue\WebImages\IProvider` implementation. Its

When you have it, register it in configuration:

```
```yml
webimages:
providers:
- <name of your class>
```

Secondly you have to specify route where your images will be available. Central point of the route is `id` parameter, which should uniquely identify your image. Lets setup simple route:

```
```yml
webimages:
routes:
- images/<id>-<width>x<height>.jpg
Expand All @@ -65,4 +65,4 @@ This will result in following HTML:
<img src="/images/foo-200x150.jpg">
```

Creation of this file will handle your implementation of `DotBlue\WebImages\IProvider`.
Creation of this file will be handled by your implementation of `DotBlue\WebImages\IProvider`.
47 changes: 47 additions & 0 deletions src/Application/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace DotBlue\WebImages\Application;

use Nette;
use Nette\Application\IResponse;
use Nette\Utils\Image;
use Nette\Http\IRequest;
use Nette\Http\IResponse as HttpResponse;


/**
* Image response.
*/
class Response extends Nette\Object implements IResponse
{

/** @var \Nette\Utils\Image|NULL */
protected $image;


/** @var int */
protected $format;


/**
* @param \Nette\Utils\Image|NULL
* @param int
*/
public function __construct(Image $image = NULL, $format = Image::JPEG)
{
$this->image = $image;
$this->format = $format;
}


public function send(IRequest $httpRequest, HttpResponse $httpResponse)
{
if (!$this->image) {
$httpResponse->setHeader('Content-Type', image_type_to_mime_type($this->format));
$httpResponse->setCode(HttpResponse::S404_NOT_FOUND);
return;
}
$this->image->send($this->format);
}

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

/**
* Copyright (c) dotBlue (http://dotblue.net)
*/

namespace DotBlue\WebImages\Application;

use Nette\Application\Routers\Route as BaseRoute;
use Nette\Utils\Image;
use DotBlue\WebImages\Generator;


class Route extends BaseRoute
{

/** @var array */
protected $defaults = array(
'flags' => Image::JPEG
);


/**
* @param \DotBlue\WebImages\Generator
* @param string
* @param array
* @param int
*/
public function __construct(Generator $generator, $mask, array $metadata = array(), $flags = 0)
{
$this->defaults = array_replace($this->defaults, $metadata);
$metadata['presenter'] = 'Nette:Micro';
$me = $this;

$metadata[NULL][self::FILTER_OUT] = function($params) use ($me, $generator) {
$width = $me->acquireArgument('width', $params);
$height = $me->acquireArgument('height', $params);
$flags = $me->acquireArgument('flags', $params);

if (!$generator->getValidator()->validate($width, $height, $flags)) {
throw new \Nette\Application\ForbiddenRequestException("Image with params ({$width}x{$height}, {$flags}) is not allowed - check your 'webimages.rules' please.");
}

return $params;
};

$metadata['callback'] = function($presenter) use ($me, $generator) {
$params = $presenter->getRequest()->getParameters();

$image = NULL;
try {
$id = $me->acquireArgument('id', $params);
$width = $me->acquireArgument('width', $params);
$height = $me->acquireArgument('height', $params);
$flags = $me->acquireArgument('flags', $params);

$image = $generator->generateImage($id, $width, $height, $flags);
} catch (\Exception $e) {}

return new Response($image);
};

parent::__construct($mask, $metadata, $flags);
}


/**
* @param string
* @param array
* @return mixed
* @throws \Nette\InvalidStateException
*/
public function acquireArgument($name, array $data)
{
if (isset($data[$name])) {
return $data[$name];
} elseif (isset($this->defaults[$name])) {
return $this->defaults[$name];
} else {
throw new \Nette\InvalidArgumentException("Missing parameter $name.");
}
}

}
Loading