diff --git a/config/bootstrap.php b/config/bootstrap.php index 54ad6d2..726572c 100644 --- a/config/bootstrap.php +++ b/config/bootstrap.php @@ -8,6 +8,7 @@ function (Event $event) { $controller = $event->getSubject(); if ($controller->components()->has('RequestHandler')) { $controller->RequestHandler->setConfig('viewClassMap.jpg', 'HtmlToImageView.HtmlToImage'); + $controller->RequestHandler->setConfig('viewClassMap.png', 'HtmlToImageView.HtmlToImage'); } } ); diff --git a/src/View/HtmlToImageView.php b/src/View/HtmlToImageView.php index 756d177..2e7e64e 100644 --- a/src/View/HtmlToImageView.php +++ b/src/View/HtmlToImageView.php @@ -1,6 +1,7 @@ _passedVars[] = 'imageConfig'; + $this->_passedVars[] = 'imageOptions'; + $this->_windowsEnvironment = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'; parent::__construct($request, $response, $eventManager, $viewOptions); @@ -76,19 +83,7 @@ public function __construct( $this->subDir = null; $this->layoutPath = null; $this->response = $this->response->withType('html'); - - return; - } - - if ($binary = Hash::get($this->imageConfig, 'binary')) { - $this->_binary = $binary; } - - if (!Hash::get($this->imageConfig, 'options.format')) { - $this->imageConfig['options']['format'] = $this->request->getParam('_ext', 'jpg'); - } - - $this->response = $this->response->withType($this->imageConfig['options']['format']); } /** @@ -139,7 +134,7 @@ public function output($html) } /** - * Execute the WkHtmlToImage command to render image + * Execute the wkhtmltoimage command to render image * * @param string $cmd the command to execute * @param string $input Html to pass to wkhtmltoimage @@ -172,16 +167,23 @@ protected function _exec($cmd, $input) */ protected function _getCommand() { + if ($binary = Configure::read('HtmlToImageView.binary')) { + $this->_binary = $binary; + } + if (!is_executable($this->_binary)) { throw new Exception(sprintf('wkhtmltoimage binary is not found or not executable: %s', $this->_binary)); } - $options = (array)Hash::get($this->imageConfig, 'options', []); + if ($imageOptions = (array)Hash::get($this->viewOptions(), 'imageOptions', [])) { + $this->imageOptions = array_merge($this->imageOptions, $imageOptions); + } + + if (!Hash::get($this->imageOptions, 'format')) { + $this->imageOptions['format'] = $this->request->getParam('_ext', 'jpg'); + } - $options = array_intersect_key( - $options, - array_flip(['crop-w', 'crop-h', 'crop-x', 'crop-y', 'width', 'height', 'format', 'quality', 'zoom']) - ); + $options = array_intersect_key($this->imageOptions, array_flip($this->_allowedImageOptions)); $options['quiet'] = true; if ($this->_windowsEnvironment) { diff --git a/tests/TestCase/View/HtmlToImageViewTest.php b/tests/TestCase/View/HtmlToImageViewTest.php index d5bb72a..93d6f29 100644 --- a/tests/TestCase/View/HtmlToImageViewTest.php +++ b/tests/TestCase/View/HtmlToImageViewTest.php @@ -2,6 +2,7 @@ namespace ImageView\View; +use Cake\Core\Configure; use Cake\Core\Exception\Exception; use Cake\Http\Response; use Cake\Http\ServerRequest; @@ -27,6 +28,7 @@ public function setUp() { parent::setUp(); + Configure::write('HtmlToImageView.binary', '/bin/echo'); $request = new ServerRequest(); $response = new Response(); $this->View = new HtmlToImageView($request, $response); @@ -57,7 +59,7 @@ public function testConstructor() public function testRender() { $request = new ServerRequest(); - $response = new Response(); + $response = new Response(['type' => 'image/jpeg']); $this->View = $this->getMockBuilder('HtmlToImageView\View\HtmlToImageView') ->setConstructorArgs([$request, $response]) ->setMethods(['output']) @@ -82,16 +84,7 @@ public function testOutput() $request = new ServerRequest(); $response = new Response(); $this->View = $this->getMockBuilder('HtmlToImageView\View\HtmlToImageView') - ->setConstructorArgs([ - $request, - $response, - null, - [ - 'imageConfig' => [ - 'binary' => '/bin/echo' - ] - ] - ]) + ->setConstructorArgs([$request, $response]) ->setMethods(['_exec']) ->getMock(); @@ -116,16 +109,7 @@ public function testOutputNoData() $request = new ServerRequest(); $response = new Response(); $this->View = $this->getMockBuilder('HtmlToImageView\View\HtmlToImageView') - ->setConstructorArgs([ - $request, - $response, - null, - [ - 'imageConfig' => [ - 'binary' => '/bin/echo' - ] - ] - ]) + ->setConstructorArgs([$request, $response]) ->setMethods(['_exec']) ->getMock(); @@ -152,16 +136,7 @@ public function testOutputError() $request = new ServerRequest(); $response = new Response(); $this->View = $this->getMockBuilder('HtmlToImageView\View\HtmlToImageView') - ->setConstructorArgs([ - $request, - $response, - null, - [ - 'imageConfig' => [ - 'binary' => '/bin/echo' - ] - ] - ]) + ->setConstructorArgs([$request, $response]) ->setMethods(['_exec']) ->getMock(); @@ -193,40 +168,31 @@ public function testGetCommand() $method = $class->getMethod('_getCommand'); $method->setAccessible(true); - $this->View = new HtmlToImageView($request, $response, null, [ - 'imageConfig' => [ - 'binary' => '/bin/echo' - ] - ]); + $this->View = new HtmlToImageView($request, $response); $result = $method->invokeArgs($this->View, []); $expected = "/bin/echo --format 'jpg' --quiet - -"; $this->assertEquals($expected, $result); + Configure::write('HtmlToImageView.binary', '/bin/sh'); $this->View = new HtmlToImageView($request, $response, null, [ - 'imageConfig' => [ - 'binary' => '/bin/sh', - 'options' => [ - 'crop-w' => 100, - 'crop-h' => 200, - 'crop-x' => 300, - 'crop-y' => 400, - 'width' => 500, - 'height' => 600, - 'format' => 'png', - 'quality' => 50, - 'zoom' => 1.5, - ] + 'imageOptions' => [ + 'crop-w' => 100, + 'crop-h' => 200, + 'crop-x' => 300, + 'crop-y' => 400, + 'width' => 500, + 'height' => 600, + 'format' => 'png', + 'quality' => 50, + 'zoom' => 1.5, ] ]); $result = $method->invokeArgs($this->View, []); $expected = "/bin/sh --crop-w '100' --crop-h '200' --crop-x '300' --crop-y '400' --width '500' --height '600' --format 'png' --quality '50' --zoom '1.5' --quiet - -"; $this->assertEquals($expected, $result); - $this->View = new HtmlToImageView($request, $response, null, [ - 'imageConfig' => [ - 'binary' => '/bin/nonexisting', - ] - ]); + Configure::write('HtmlToImageView.binary', '/bin/nonexisting'); + $this->View = new HtmlToImageView($request, $response); $this->expectException(Exception::class); $this->expectExceptionMessage('wkhtmltoimage binary is not found or not executable: /bin/nonexisting'); $method->invokeArgs($this->View, []); @@ -246,11 +212,7 @@ public function testExec() $method = $class->getMethod('_exec'); $method->setAccessible(true); - $this->View = new HtmlToImageView($request, $response, null, [ - 'imageConfig' => [ - 'binary' => '/bin/echo' - ] - ]); + $this->View = new HtmlToImageView($request, $response); $result = $method->invokeArgs($this->View, ['/bin/echo test', 'test']); $expected = [