1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Smart image resizing (and manipulation) by url module for Zend Framework 3 |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/tck/zf2-imageresizer for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2017 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 TckImageResizer\Controller; |
13
|
|
|
|
14
|
|
|
use Zend\Mvc\Controller\AbstractActionController; |
15
|
|
|
use TckImageResizer\Service\ImageProcessing; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Index controller |
19
|
|
|
* |
20
|
|
|
* @package TckImageResizer |
21
|
|
|
*/ |
22
|
|
|
class IndexController extends AbstractActionController |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var ImageProcessing |
26
|
|
|
*/ |
27
|
|
|
protected $imageProcessing; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $publicDirectory; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* constructor |
36
|
|
|
* |
37
|
|
|
* @param ImageProcessing $imageProcessing |
38
|
|
|
* @param string $publicDirectory |
39
|
|
|
*/ |
40
|
|
|
public function __construct(ImageProcessing $imageProcessing, $publicDirectory = 'public') |
41
|
15 |
|
{ |
42
|
|
|
$this->setImageProcessing($imageProcessing); |
43
|
15 |
|
$this->publicDirectory = $publicDirectory; |
44
|
15 |
|
} |
45
|
15 |
|
|
46
|
|
|
/** |
47
|
|
|
* set the image processing service |
48
|
|
|
* |
49
|
|
|
* @param ImageProcessing $imageProcessing |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
|
public function setImageProcessing(ImageProcessing $imageProcessing) |
53
|
15 |
|
{ |
54
|
|
|
$this->imageProcessing = $imageProcessing; |
55
|
15 |
|
|
56
|
|
|
return $this; |
57
|
15 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get the image processing service |
61
|
|
|
* |
62
|
|
|
* @return ImageProcessing |
63
|
|
|
*/ |
64
|
|
|
public function getImageProcessing() |
65
|
12 |
|
{ |
66
|
|
|
return $this->imageProcessing; |
67
|
12 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return \Zend\Http\Response |
71
|
|
|
*/ |
72
|
|
|
public function resizeAction() |
73
|
12 |
|
{ |
74
|
|
|
$source = $this->publicDirectory . '/' |
75
|
12 |
|
. $this->params('file') |
76
|
12 |
|
. '.' . $this->params('extension'); |
77
|
12 |
|
|
78
|
|
|
$targetExtension = $this->params('extension'); |
79
|
12 |
|
if (!file_exists($source)) { |
80
|
12 |
|
$source = null; |
81
|
3 |
|
$targetExtension = '404.' . $targetExtension . '.png'; |
82
|
3 |
|
} |
83
|
3 |
|
|
84
|
|
|
$target = $this->publicDirectory . '/processed/' |
85
|
12 |
|
. $this->params('file') |
86
|
12 |
|
. '.$' . $this->params('command') |
87
|
12 |
|
. '.' . $targetExtension; |
88
|
12 |
|
|
89
|
|
|
if ($source) { |
|
|
|
|
90
|
12 |
|
$this->getImageProcessing()->process($source, $target, $this->params('command')); |
91
|
9 |
|
|
92
|
|
|
} else { |
93
|
9 |
|
$this->getImageProcessing()->process404($target, $this->params('command')); |
94
|
3 |
|
} |
95
|
|
|
|
96
|
|
|
$imageInfo = getimagesize($target); |
97
|
12 |
|
$mimeType = $imageInfo['mime']; |
98
|
12 |
|
|
99
|
|
|
/** @var \Zend\Http\Response $response */ |
100
|
|
|
$response = $this->getResponse(); |
101
|
12 |
|
$response->setContent(file_get_contents($target)); |
102
|
12 |
|
$response->setStatusCode($source ? 200 : 404); |
103
|
12 |
|
$response |
104
|
|
|
->getHeaders() |
105
|
12 |
|
->addHeaderLine('Content-Transfer-Encoding', 'binary') |
106
|
12 |
|
->addHeaderLine('Content-Type', $mimeType); |
107
|
12 |
|
|
108
|
|
|
return $response; |
109
|
12 |
|
} |
110
|
|
|
} |
111
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: