temporaryphoto.php
4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* ownCloud - TemporaryPhoto
*
* @author Thomas Tanghus
* @copyright 2014 Thomas Tanghus (thomas@tanghus.net)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Contacts\Utils;
use OCP\ICache,
OCP\Image;
/**
* This class is used for getting a temporary contact photo for cropping.
*
* Sub-classes must override __construct() and processImage()
*/
class TemporaryPhoto {
const MAX_SIZE = 400;
const PHOTO_CURRENT = 0;
const PHOTO_FILESYSTEM = 1;
const PHOTO_UPLOADED = 2;
const PHOTO_USER = 3;
/**
* @var \OCP\ICache
*/
protected $cache;
/**
* @var \OCP\Image
*/
protected $image;
/**
* Cache key for temporary storage.
*
* @var string
*/
protected $key;
/**
* Whether normalizePhoto() has already been called.
*
* @var bool
*/
protected $normalized;
/**
* Whether the photo is cached.
*
* @var bool
*/
protected $cached;
/**
* Photo loader classes.
*
* @var array
*/
static public $classMap = array(
'OCA\\Contacts\\Utils\\TemporaryPhoto\\Contact',
'OCA\\Contacts\\Utils\\TemporaryPhoto\\FileSystem',
'OCA\\Contacts\\Utils\\TemporaryPhoto\\Uploaded',
'OCA\\Contacts\\Utils\\TemporaryPhoto\\User',
);
/**
* Always call parents ctor in subclasses:
* parent::__construct($cache);
*/
public function __construct(ICache $cache, $key = null) {
$this->cache = $cache;
$this->key = $key;
if (!is_null($key)) {
$this->processImage();
}
}
/**
* Returns an instance of a subclass of this class
*
* @param \OCP\IServerContainer $server
* @param int|null $type One of the pre-defined types.
* @param mixed|null $data Whatever data is needed to load the photo.
*/
public static function create(ICache $cache, $type = null, $data = null) {
if (isset(self::$classMap[$type])) {
return new self::$classMap[$type]($cache, $data);
} else {
return new self($cache, $data);
}
}
/**
* Remove a cached image by key.
*
* @param string $key
*/
public function remove($key) {
return $this->cache->remove($key);
}
/**
* Do what's needed to get the image from storage
* depending on the type.
* After this method is called $this->image must hold an
* instance of \OCP\Image.
*/
protected function processImage() {
$this->image = new Image();
$this->image->loadFromData($this->cache->get($this->key));
}
/**
* Whether this image is valied
*
* @return bool
*/
public function isValid() {
return (($this->image instanceof Image) && $this->image->valid());
}
/**
* Get the key to the cache image.
*
* @return string
*/
public function getKey() {
$this->cachePhoto();
return $this->key;
}
/**
* Get normalized image.
*
* @return \OCP\Image
*/
public function getPhoto() {
$this->normalizePhoto();
return $this->image;
}
/**
* Save image data to cache and return the key
*
* @return string
*/
private function cachePhoto() {
if ($this->cached) {
return;
}
if (!$this->image instanceof Image) {
$this->processImage();
}
$this->normalizePhoto();
$data = $this->image->data();
$this->key = uniqid('photo-');
$this->cache->set($this->key, $data, 600);
}
/**
* Resize and rotate the image if needed.
*/
private function normalizePhoto() {
if($this->normalized) {
return;
}
$this->image->fixOrientation();
if ($this->image->height() > self::MAX_SIZE
|| $this->image->width() > self::MAX_SIZE
) {
$this->image->resize(self::MAX_SIZE);
}
$this->normalized = true;
}
public function getMimeType(){
return $this->image->mimeType();
}
public function getImage(){
return $this->image;
}
}