1 | <?php |
||
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) |
|
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) |
||
116 | |||
117 | /** |
||
118 | * Stream file to browser |
||
119 | */ |
||
120 | public function stream(ImageParams $params = null) |
||
124 | |||
125 | } |
||
126 |