Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
226 lines (202 loc) · 5.58 KB

File metadata and controls

226 lines (202 loc) · 5.58 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
App::uses('FileStorage', 'FileStorage.Model');
App::uses('Folder', 'Utility');
/**
* Image
*
* @author Florian Kr�mer
* @copyright 2012 Florian Kr�mer
* @license MIT
*/
class ImageStorage extends FileStorage {
/**
* Table to use
*
* @var mixed
*/
public $useTable = 'file_storage';
/**
* Behaviours
*
* @var array
*/
public $actsAs = array(
'Imagine.Imagine',
'FileStorage.UploadValidator' => array(
'localFile' => true,
'validate' => false,
'allowedExtensions' => array('jpg', 'jpeg', 'png', 'gif')
),
);
/**
* Getter
*
* @param string $name
* @return void
*/
public function __get($name) {
if ($name === 'createVersions') {
throw new \RuntimeException(__d('file_storage', 'createVersions was removed, see the change log'));
}
}
/**
* beforeSave callback
*
* @param array $options
* @return boolean true on success
*/
public function beforeSave($options = array()) {
if (!parent::beforeSave($options)) {
return false;
}
$Event = new CakeEvent('ImageStorage.beforeSave', $this, array(
'record' => $this->data));
CakeEventManager::instance()->dispatch($Event);
if ($Event->isStopped()) {
return false;
}
return true;
}
/**
* afterSave callback
*
* Does not call the parent to avoid that the regular file storage event listener saves the image already
*
* @param boolean
* @return void
*/
public function afterSave($created) {
if ($created) {
$this->data[$this->alias][$this->primaryKey] = $this->getLastInsertId();
$Event = new CakeEvent('ImageStorage.afterSave', $this, array(
'created' => $created,
'storage' => StorageManager::adapter($this->data[$this->alias]['adapter']),
'record' => $this->data));
CakeEventManager::instance()->dispatch($Event);
}
}
/**
* Get a copy of the actual record before we delete it to have it present in afterDelete
*
* @param boolean $cascade
* @return boolean
*/
public function beforeDelete($cascade = true) {
if (!parent::beforeDelete($cascade)) {
return false;
}
$Event = new CakeEvent('ImageStorage.beforeDelete', $this, array(
'record' => $this->record,
'storage' => StorageManager::adapter($this->record[$this->alias]['adapter'])));
CakeEventManager::instance()->dispatch($Event);
if ($Event->isStopped()) {
return false;
}
return true;
}
/**
* After the main file was deleted remove the the thumbnails
*
* Note that we do not call the parent::afterDelete(), we just want to trigger the ImageStorage.afterDelete event but not the FileStorage.afterDelete at the same time!
*
* @return void
*/
public function afterDelete() {
$Event = new CakeEvent('ImageStorage.afterDelete', $this, array(
'record' => $this->record,
'storage' => StorageManager::adapter($this->record[$this->alias]['adapter'])));
CakeEventManager::instance()->dispatch($Event);
}
/**
* Serializes and then hashes an array of operations that are applied to an image
*
* @param array $operations
* @return array
*/
public function hashOperations($operations) {
$this->ksortRecursive($operations);
return substr(md5(serialize($operations)), 0, 8);
}
/**
* Generate hashes
*
* @param string
* @return void
*/
public function generateHashes($configPath = 'Media') {
$imageSizes = Configure::read($configPath . '.imageSizes');
$this->ksortRecursive($imageSizes);
foreach ($imageSizes as $model => $version) {
foreach ($version as $name => $operations) {
Configure::write($configPath . '.imageHashes.' . $model . '.' . $name, $this->hashOperations($operations));
}
}
}
/**
* @deperacted This has been replaced by Events
* @throws InternalErrorException
*/
public function createVersions($data = array(), $format = 'jpg') {
throw new InternalErrorException(__('file_storage', 'ImageStorage::createVersions is deprecated use the event system.'));
}
/**
* Recursive ksort() implementation
*
* @param array $array
* @param integer
* @return void
* @link https://gist.github.com/601849
*/
public function ksortRecursive(&$array, $sortFlags = SORT_REGULAR) {
if (!is_array($array)) return false;
ksort($array, $sortFlags);
foreach ($array as &$arr) {
$this->ksortRecursive($arr, $sortFlags);
}
return true;
}
/**
* Image size validation method
*
* @param mixed $check
* @param array $options is an array with key width or height and a value of array
* with two options, operator and value. For example:
* array('height' => array('==', 100)) will only be true if the image has a
* height of exactly 100px. See the CakePHP core class and method
* Validation::comparison for all operators.
* @return boolean true
* @see Validation::comparison()
* @throws \InvalidArgumentException
*/
public function validateImageSize($check, $options) {
if (!isset($options['height']) && !isset($options['width'])) {
throw new \InvalidArgumentException(__d('file_storage', 'Invalid image size validation parameters'));
}
if (is_string($check)) {
$imageFile = $check;
} else {
$check = array_values($check);
$check = $check[0];
if (is_array($check) && isset($check['tmp_name'])) {
$imageFile = $check['tmp_name'];
} else {
$imageFile = $check;
}
}
$imageSizes = $this->getImageSize($imageFile);
if (isset($options['height'])) {
$height = Validation::comparison($imageSizes[1], $options['height'][0], $options['height'][1]);
} else {
$height = true;
}
if (isset($options['width'])) {
$width = Validation::comparison($imageSizes[0], $options['width'][0], $options['width'][1]);
} else {
$width = true;
}
if ($height === false || $width === false) {
return false;
}
return true;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.