|
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 http://maslosoft.com/mangan/ |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Maslosoft\Mangan\Model; |
|
15
|
|
|
|
|
16
|
|
|
use Exception; |
|
17
|
|
|
use MongoGridFSFile; |
|
18
|
|
|
use PHPThumb\GD; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class for storing embedded images |
|
22
|
|
|
* @since 2.0.1 |
|
23
|
|
|
* @author Piotr |
|
24
|
|
|
*/ |
|
25
|
|
|
class Image extends File |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Image width |
|
30
|
|
|
* @var int |
|
31
|
|
|
*/ |
|
32
|
|
|
public $width = 0; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Image height |
|
36
|
|
|
* @var int |
|
37
|
|
|
*/ |
|
38
|
|
|
public $height = 0; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get resized image |
|
42
|
|
|
* @param ImageParams $params |
|
43
|
|
|
* @return MongoGridFSFile |
|
44
|
|
|
*/ |
|
45
|
2 |
|
public function get(ImageParams $params = null) |
|
46
|
|
|
{ |
|
47
|
|
|
// Get original image or file if it is not image |
|
48
|
2 |
|
if (!$params || !$this->isImage($this->filename)) |
|
49
|
|
|
{ |
|
50
|
2 |
|
return $this->_get(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// Get resized image |
|
54
|
2 |
|
$params->isTemp = true; |
|
55
|
2 |
|
$result = $this->_get($params->toArray()); |
|
56
|
|
|
|
|
57
|
|
|
// Resize and store if not found |
|
58
|
2 |
|
if (!$result) |
|
59
|
|
|
{ |
|
60
|
2 |
|
$result = $this->_get(); |
|
61
|
2 |
|
if (!$result) |
|
62
|
|
|
{ |
|
63
|
|
|
throw new Exception('File not found'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
2 |
|
$originalFilename = $result->file['filename']; |
|
67
|
2 |
|
$fileName = tempnam('/tmp/', __CLASS__); |
|
68
|
2 |
|
$result->write($fileName); |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
2 |
|
$image = new GD($fileName); |
|
72
|
2 |
|
if ($params->adaptive) |
|
73
|
|
|
{ |
|
74
|
|
|
$image->adaptiveResize($params->width, $params->height)->save($fileName); |
|
75
|
|
|
} |
|
76
|
|
|
else |
|
77
|
|
|
{ |
|
78
|
2 |
|
$image->resize($params->width, $params->height)->save($fileName); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
$this->_set($fileName, $originalFilename, $params->toArray()); |
|
82
|
2 |
|
unlink($fileName); |
|
83
|
2 |
|
return $this->_get($params->toArray()); |
|
84
|
|
|
} |
|
85
|
|
|
return $result; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Return true if it is really image |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
2 |
|
public function isImage($name) |
|
93
|
|
|
{ |
|
94
|
2 |
|
return in_array(strtolower(pathinfo($name, PATHINFO_EXTENSION)), ['jpg', 'jpeg', 'gif', 'png']); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
2 |
|
protected function _set($tempName, $fileName, $params = []) |
|
98
|
|
|
{ |
|
99
|
2 |
|
if ($this->isImage($fileName)) |
|
100
|
|
|
{ |
|
101
|
2 |
|
$thumb = new GD($tempName); |
|
102
|
2 |
|
$dimensions = (object) $thumb->getCurrentDimensions(); |
|
103
|
2 |
|
$this->width = $dimensions->width; |
|
104
|
2 |
|
$this->height = $dimensions->height; |
|
105
|
|
|
} |
|
106
|
2 |
|
parent::_set($tempName, $fileName, $params); |
|
107
|
2 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Send file to browser |
|
111
|
|
|
*/ |
|
112
|
|
|
public function send(ImageParams $params = null) |
|
113
|
|
|
{ |
|
114
|
|
|
$this->_send($this->get($params)); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Stream file to browser |
|
119
|
|
|
*/ |
|
120
|
|
|
public function stream(ImageParams $params = null) |
|
121
|
|
|
{ |
|
122
|
|
|
$this->_stream($this->get($params)); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|