1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under AGPL or Commercial license. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/mangan |
7
|
|
|
* @licence AGPL or Commercial |
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> |
9
|
|
|
* @copyright Copyright (c) Maslosoft |
10
|
|
|
* @copyright Copyright (c) Others as mentioned in code |
11
|
|
|
* @link https://maslosoft.com/mangan/ |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Maslosoft\Mangan\Model; |
15
|
|
|
|
16
|
|
|
use Maslosoft\Mangan\Events\Event; |
17
|
|
|
use Maslosoft\Mangan\Events\ImageEvent; |
18
|
|
|
use Maslosoft\Mangan\Exceptions\FileNotFoundException; |
19
|
|
|
use Maslosoft\Mangan\Helpers\ImageThumb; |
20
|
|
|
use MongoGridFSFile; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class for storing embedded images |
24
|
|
|
* @since 2.0.1 |
25
|
|
|
* @author Piotr |
26
|
|
|
*/ |
27
|
|
|
class Image extends File |
28
|
|
|
{ |
29
|
|
|
const EventBeforeResize = 'beforeResize'; |
30
|
|
|
|
31
|
|
|
const EventAfterResize = 'afterResize'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Image width |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
public $width = 0; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Image height |
41
|
|
|
* @var int |
42
|
|
|
*/ |
43
|
|
|
public $height = 0; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get resized image |
47
|
|
|
* @param ImageParams $params |
48
|
|
|
* @return MongoGridFSFile |
49
|
|
|
*/ |
50
|
2 |
|
public function get(ImageParams $params = null) |
51
|
|
|
{ |
52
|
|
|
// Get original image or file if it is not image |
53
|
2 |
|
if (!$params || !$this->isImage($this->filename)) |
54
|
|
|
{ |
55
|
2 |
|
return $this->_get(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Get resized image |
59
|
2 |
|
$params->isTemp = true; |
60
|
2 |
|
$result = $this->_get($params->toArray()); |
61
|
|
|
|
62
|
|
|
// Resize and store if not found |
63
|
2 |
|
if (!$result) |
64
|
|
|
{ |
65
|
2 |
|
$result = $this->_get(); |
66
|
2 |
|
if (!$result) |
67
|
|
|
{ |
68
|
|
|
throw new FileNotFoundException('File not found'); |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
$originalFilename = $result->file['filename']; |
72
|
2 |
|
$fileName = tempnam('/tmp/', str_replace('\\', '.', __CLASS__)); |
73
|
2 |
|
$result->write($fileName); |
74
|
|
|
|
75
|
2 |
|
$ie = new ImageEvent; |
76
|
2 |
|
$ie->sender = $this; |
77
|
2 |
|
$ie->path = $fileName; |
78
|
|
|
|
79
|
2 |
|
Event::trigger($this, self::EventBeforeResize, $ie); |
80
|
|
|
|
81
|
2 |
|
$image = new ImageThumb($fileName); |
82
|
2 |
|
if ($params->adaptive) |
83
|
|
|
{ |
84
|
|
|
$image->adaptiveResize($params->width, $params->height)->save($fileName); |
85
|
|
|
} |
86
|
|
|
else |
87
|
|
|
{ |
88
|
2 |
|
$image->resize($params->width, $params->height)->save($fileName); |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
$ie = new ImageEvent; |
92
|
2 |
|
$ie->sender = $this; |
93
|
2 |
|
$ie->path = $fileName; |
94
|
|
|
|
95
|
2 |
|
Event::trigger($this, self::EventAfterResize, $ie); |
96
|
|
|
|
97
|
2 |
|
$this->_set($fileName, $originalFilename, $params->toArray()); |
98
|
2 |
|
unlink($fileName); |
99
|
2 |
|
return $this->_get($params->toArray()); |
100
|
|
|
} |
101
|
|
|
return $result; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Return true if it is really image |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
2 |
|
public function isImage($name) |
109
|
|
|
{ |
110
|
2 |
|
return in_array(strtolower(pathinfo($name, PATHINFO_EXTENSION)), ['jpg', 'jpeg', 'gif', 'png']); |
111
|
|
|
} |
112
|
|
|
|
113
|
2 |
|
protected function _set($tempName, $fileName, $params = []) |
114
|
|
|
{ |
115
|
2 |
|
if ($this->isImage($fileName)) |
116
|
|
|
{ |
117
|
2 |
|
$thumb = new ImageThumb($tempName); |
118
|
2 |
|
$dimensions = (object) $thumb->getCurrentDimensions(); |
119
|
2 |
|
$this->width = $dimensions->width; |
120
|
2 |
|
$this->height = $dimensions->height; |
121
|
|
|
} |
122
|
2 |
|
parent::_set($tempName, $fileName, $params); |
123
|
2 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Send file to browser |
127
|
|
|
*/ |
128
|
|
|
public function send(ImageParams $params = null) |
129
|
|
|
{ |
130
|
|
|
$this->_send($this->get($params)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Stream file to browser |
135
|
|
|
*/ |
136
|
|
|
public function stream(ImageParams $params = null) |
137
|
|
|
{ |
138
|
|
|
$this->_stream($this->get($params)); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|