Skip to content

Commit 0a0bab7

Browse files
committed
Added view helper
1 parent 368b42f commit 0a0bab7

File tree

6 files changed

+236
-11
lines changed

6 files changed

+236
-11
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
build
2+
data
13
vendor
24
composer.lock
3-
phing*.phar
5+
phing*.phar
6+
*.bat

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ Example: Create a thumbnail and grayscale image
5959
* img/logo.jpg
6060
* `processed/`img/logo`.$thumb,160,120$grayscale`.jpg
6161

62+
#### View helper
63+
ZF2 tempalte:
64+
65+
```php
66+
<img alt="Example image" src="<?php echo $this->resize('img/logo.jpg')->thumb(200, 160)->grayscale(); ?>" />
67+
```
68+
69+
Rendered HTML:
70+
71+
```php
72+
<img alt="Example image" src="/processed/img/logo.$thumb,200,160$grayscale.jpg" />
73+
```
74+
6275

6376
#### Command list
6477

@@ -70,15 +83,15 @@ Example: Create a thumbnail and grayscale image
7083
* colorize(hexColor)
7184
* sharpen
7285
* blur(sigma = 1)
73-
* 404(text[, backgroundColor[, color[, width[, height]]]]) - 404 individual image [text: url-safe base64]
86+
* 404(text = 'Not found', backgroundColor = 'F8F8F8', color = '777777', width = null, height = null)
87+
- 404 individual image [text: url-safe base64] - \TckImageResizer\Util\UrlSafeBase64::encode($text)
7488

7589
Own commands possible - example place a watermark (Todo Documentation)
7690

7791

7892
### Goals / Todos
7993

80-
* View Helper
8194
* More commands
8295
* More command options
8396
* Administrative functions
84-
* ...
97+
* Create placeholder

autoload_classmap.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<?php
22
// Generated by ZF2's ./bin/classmap_generator.php
33
return array(
4-
'TckImageResizer\Module' => __DIR__ . '/Module.php',
5-
'TckImageResizer\Controller\IndexController' => __DIR__ . '/src/TckImageResizer/Controller/IndexController.php',
6-
'TckImageResizer\Controller\IndexControllerFactory' => __DIR__ . '/src/TckImageResizer/Controller/IndexControllerFactory.php',
7-
'TckImageResizer\Exception\BadMethodCallException' => __DIR__ . '/src/TckImageResizer/Exception/BadMethodCallException.php',
8-
'TckImageResizer\Service\CommandRegistry' => __DIR__ . '/src/TckImageResizer/Service/CommandRegistry.php',
9-
'TckImageResizer\Service\ImageProcessing' => __DIR__ . '/src/TckImageResizer/Service/ImageProcessing.php',
10-
'TckImageResizer\Service\ImageProcessingFactory' => __DIR__ . '/src/TckImageResizer/Service/ImageProcessingFactory.php',
4+
'TckImageResizer\Module' => __DIR__ . '/Module.php',
5+
'TckImageResizer\Controller\IndexController' => __DIR__ . '/src/TckImageResizer/Controller/IndexController.php',
6+
'TckImageResizer\Controller\IndexControllerFactory' => __DIR__ . '/src/TckImageResizer/Controller/IndexControllerFactory.php',
7+
'TckImageResizer\Exception\BadMethodCallException' => __DIR__ . '/src/TckImageResizer/Exception/BadMethodCallException.php',
8+
'TckImageResizer\Service\CommandRegistry' => __DIR__ . '/src/TckImageResizer/Service/CommandRegistry.php',
9+
'TckImageResizer\Service\ImageProcessing' => __DIR__ . '/src/TckImageResizer/Service/ImageProcessing.php',
10+
'TckImageResizer\Service\ImageProcessingFactory' => __DIR__ . '/src/TckImageResizer/Service/ImageProcessingFactory.php',
11+
'TckImageResizer\Util\UrlSafeBase64' => __DIR__ . '/src/TckImageResizer/Util/UrlSafeBase64.php',
12+
'TckImageResizer\View\Helper\Resize' => __DIR__ . '/src/TckImageResizer/View/Helper/Resize.php',
1113
);
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
namespace TckImageResizer\View\Helper;
4+
5+
use Zend\View\Helper\AbstractHelper;
6+
use TckImageResizer\Util\UrlSafeBase64;
7+
8+
class Resize extends AbstractHelper
9+
{
10+
protected $imgParts;
11+
protected $commands;
12+
13+
public function __invoke($imgPath)
14+
{
15+
$this->imgParts = pathinfo($imgPath);
16+
$this->commands = '';
17+
18+
return $this;
19+
}
20+
21+
public function thumb($width, $height)
22+
{
23+
$this->commands .= '$thumb,' . $width . ',' . $height;
24+
25+
return $this;
26+
}
27+
28+
public function resize($width, $height)
29+
{
30+
$this->commands .= '$resize,' . $width . ',' . $height;
31+
32+
return $this;
33+
}
34+
35+
public function grayscale()
36+
{
37+
$this->commands .= '$grayscale';
38+
39+
return $this;
40+
}
41+
42+
public function negative()
43+
{
44+
$this->commands .= '$negative';
45+
46+
return $this;
47+
}
48+
49+
public function gamma($correction)
50+
{
51+
$this->commands .= '$gamma,' . $correction;
52+
53+
return $this;
54+
}
55+
56+
public function colorize($hexColor)
57+
{
58+
$this->commands .= '$colorize,' . $hexColor;
59+
60+
return $this;
61+
}
62+
63+
public function sharpen()
64+
{
65+
$this->commands .= '$sharpen';
66+
67+
return $this;
68+
}
69+
70+
public function blur($sigma = null)
71+
{
72+
$this->commands .= '$blur' . ($sigma !== null ? ',' . $sigma : '');
73+
74+
return $this;
75+
}
76+
77+
public function _404($text = null, $backgroundColor = null, $color = null, $width = null, $height = null)
78+
{
79+
$this->commands .= '$404'
80+
. ($text !== null ? ',' . UrlSafeBase64::encode($text) : '')
81+
. ($backgroundColor !== null ? ',' . $backgroundColor : '')
82+
. ($color !== null ? ',' . $color : '')
83+
. ($width !== null ? ',' . $width : '')
84+
. ($height !== null ? ',' . $height : '');
85+
86+
return $this;
87+
}
88+
89+
public function __toString()
90+
{
91+
return $this->getView()->basePath('processed/'
92+
. ($this->imgParts['dirname'] && $this->imgParts['dirname'] !== '.' ? $this->imgParts['dirname'] . '/' : '')
93+
. $this->imgParts['filename'] . '.'
94+
. $this->commands . '.' . $this->imgParts['extension']);
95+
}
96+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
/**
3+
* Smart image resizing (and manipulation) by url module for Zend Framework 2
4+
*
5+
* @link http://github.com/tck/zf2-imageresizer for the canonical source repository
6+
* @copyright Copyright (c) 2015 Tobias Knab
7+
*
8+
* For the full copyright and license information, please view the LICENSE.txt
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace TckImageResizerTest\View\Helper;
13+
14+
use PHPUnit_Framework_TestCase;
15+
use TckImageResizer\View\Helper\Resize;
16+
use Zend\View\Renderer\PhpRenderer;
17+
18+
class ResizeTest extends PHPUnit_Framework_TestCase
19+
{
20+
protected $view;
21+
protected $helper;
22+
23+
protected function setUp()
24+
{
25+
$this->view = new PhpRenderer();
26+
$this->view->getHelperPluginManager()->get('basePath')->setBasePath('/');
27+
28+
$this->helper = new Resize();
29+
$this->helper->setView($this->view);
30+
}
31+
32+
public function testInvoke()
33+
{
34+
$resizeObject = $this->helper->__invoke('test.jpg');
35+
36+
$this->assertInstanceOf('TckImageResizer\View\Helper\Resize', $resizeObject);
37+
}
38+
39+
public function testFileInFolder()
40+
{
41+
$actual = $this->helper->__invoke('folder/test.jpg')->__toString();
42+
43+
$this->assertEquals('/processed/folder/test..jpg', $actual);
44+
}
45+
46+
public function testThumb()
47+
{
48+
$actual = $this->helper->__invoke('test.jpg')->thumb(300, 200)->__toString();
49+
50+
$this->assertEquals('/processed/test.$thumb,300,200.jpg', $actual);
51+
}
52+
53+
public function testResize()
54+
{
55+
$actual = $this->helper->__invoke('test.jpg')->resize(300, 200)->__toString();
56+
57+
$this->assertEquals('/processed/test.$resize,300,200.jpg', $actual);
58+
}
59+
60+
public function testGrayscale()
61+
{
62+
$actual = $this->helper->__invoke('test.jpg')->grayscale()->__toString();
63+
64+
$this->assertEquals('/processed/test.$grayscale.jpg', $actual);
65+
}
66+
67+
public function testNegative()
68+
{
69+
$actual = $this->helper->__invoke('test.jpg')->negative()->__toString();
70+
71+
$this->assertEquals('/processed/test.$negative.jpg', $actual);
72+
}
73+
74+
public function testGamma()
75+
{
76+
$actual = $this->helper->__invoke('test.jpg')->gamma(1)->__toString();
77+
78+
$this->assertEquals('/processed/test.$gamma,1.jpg', $actual);
79+
}
80+
81+
public function testColorize()
82+
{
83+
$actual = $this->helper->__invoke('test.jpg')->colorize('ff00ff')->__toString();
84+
85+
$this->assertEquals('/processed/test.$colorize,ff00ff.jpg', $actual);
86+
}
87+
88+
public function testSharpen()
89+
{
90+
$actual = $this->helper->__invoke('test.jpg')->sharpen()->__toString();
91+
92+
$this->assertEquals('/processed/test.$sharpen.jpg', $actual);
93+
}
94+
95+
public function testBlur()
96+
{
97+
$actual = $this->helper->__invoke('test.jpg')->blur(1)->__toString();
98+
99+
$this->assertEquals('/processed/test.$blur,1.jpg', $actual);
100+
}
101+
102+
public function test404()
103+
{
104+
$actual = $this->helper->__invoke('test.jpg')->_404('File not found', 'ffffff', 'ff00ff', 400, 200)->__toString();
105+
106+
$this->assertEquals('/processed/test.$404,RmlsZSBub3QgZm91bmQ,ffffff,ff00ff,400,200.jpg', $actual);
107+
}
108+
}

test/phpunit.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
<testsuite name="TckImageResizer util test suite">
1111
<directory>./TckImageResizerTest/Util</directory>
1212
</testsuite>
13+
<testsuite name="TckImageResizer view test suite">
14+
<directory>./TckImageResizerTest/View</directory>
15+
</testsuite>
1316
</testsuites>
1417
<filter>
1518
<blacklist>

0 commit comments

Comments
 (0)