1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* defines the image sizes |
5
|
|
|
* and default upload folder. |
6
|
|
|
*/ |
7
|
|
|
class PerfectCMSImageDataExtension extends DataExtension |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* background image for padded images... |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private static $perfect_cms_images_background_padding_color = '#cccccc'; |
|
|
|
|
15
|
|
|
|
16
|
|
|
/*** |
17
|
|
|
* sizes of the images |
18
|
|
|
* width: 3200 |
19
|
|
|
* height: 3200 |
20
|
|
|
* folder: "myfolder" |
21
|
|
|
* filetype: "try jpg" |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
private static $perfect_cms_images_image_definitions = array(); |
|
|
|
|
27
|
|
|
|
28
|
|
|
/*** |
29
|
|
|
* Images Titles will be appended to the links only |
30
|
|
|
* if the ClassName of the Image is in this array |
31
|
|
|
* @var array |
32
|
|
|
* |
33
|
|
|
*/ |
34
|
|
|
private static $perfect_cms_images_append_title_to_image_links_classes = array(); |
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string $name name of Image Field template |
38
|
|
|
* @return string (link) |
39
|
|
|
*/ |
40
|
|
|
public function PerfectCMSImageLinkNonRetina($name) |
41
|
|
|
{ |
42
|
|
|
return $this->PerfectCMSImageLink($name, null, '', false); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string $name name of Image Field template |
47
|
|
|
* @return string (link) |
48
|
|
|
*/ |
49
|
|
|
public function PerfectCMSImageLinkRetina($name) |
50
|
|
|
{ |
51
|
|
|
return $this->PerfectCMSImageLink($name, null, '', true); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string $name name of Image Field template |
56
|
|
|
* @return string (link) |
57
|
|
|
*/ |
58
|
|
|
public function PerfectCMSAbsoluteImageLink($name) |
59
|
|
|
{ |
60
|
|
|
$base = Director::baseURL(); |
61
|
|
|
return $base . $this->PerfectCMSImageLink($name, null, '', true); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* |
66
|
|
|
* @param string $name |
67
|
|
|
* @return string (HTML) |
68
|
|
|
*/ |
69
|
|
|
public function PerfectCMSImageTag($name) |
70
|
|
|
{ |
71
|
|
|
$nonRetina = $this->PerfectCMSImageLinkNonRetina($name); |
72
|
|
|
$retina = $this->PerfectCMSImageLinkRetina($name); |
73
|
|
|
$width = self::get_width($name, true); |
74
|
|
|
$widthString = ''; |
75
|
|
|
if ($width) { |
76
|
|
|
$widthString = ' width="'.$width.'"'; |
77
|
|
|
} |
78
|
|
|
$heightString = ''; |
79
|
|
|
$height = self::get_height($name, true); |
80
|
|
|
if ($height) { |
81
|
|
|
$heightString = ' height="'.$height.'"'; |
82
|
|
|
} |
83
|
|
|
return |
84
|
|
|
'<img src="'.$nonRetina.'"'. |
85
|
|
|
' srcset="'.$nonRetina.' 1x, '.$retina.' 2x" '. |
86
|
|
|
' alt="'.Convert::raw2att($this->owner->Title).'"'. |
87
|
|
|
$widthString. |
88
|
|
|
$heightString. |
89
|
|
|
|
90
|
|
|
' />'; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $name |
95
|
|
|
* @param object (optional) $backupObject |
96
|
|
|
* @param string (optional) $backupField |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function PerfectCMSImageLink( |
|
|
|
|
101
|
|
|
$name, |
102
|
|
|
$backupObject = null, |
103
|
|
|
$backupField = '', |
104
|
|
|
$useRetina = null |
105
|
|
|
) { |
106
|
|
|
if (isset($_GET['flush'])) { |
107
|
|
|
if (! Config::inst()->get('Image', 'force_resample')) { |
108
|
|
|
Config::inst()->update('Image', 'force_resample', true); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
$image = $this->owner; |
112
|
|
|
if ($image && $image->exists()) { |
|
|
|
|
113
|
|
|
//we are all good ... |
114
|
|
|
} else { |
115
|
|
|
if (!$backupObject) { |
116
|
|
|
$backupObject = SiteConfig::current_site_config(); |
117
|
|
|
} |
118
|
|
|
if (!$backupField) { |
119
|
|
|
$backupField = $name; |
120
|
|
|
} |
121
|
|
|
if ($backupObject->hasMethod($backupField)) { |
122
|
|
|
$image = $backupObject->$backupField(); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$perfectWidth = self::get_width($name, true); |
127
|
|
|
$perfectHeight = self::get_height($name, true); |
128
|
|
|
|
129
|
|
|
if ($image) { |
130
|
|
|
if ($image instanceof Image) { |
131
|
|
|
if ($image->exists()) { |
132
|
|
|
//work out perfect with and height |
133
|
|
|
if (!$useRetina) { |
134
|
|
|
$useRetina = PerfectCMSImageDataExtension::use_retina($name); |
135
|
|
|
} else { |
136
|
|
|
$useRetina = $useRetina; |
|
|
|
|
137
|
|
|
} |
138
|
|
|
$crop = PerfectCMSImageDataExtension::crop($name); |
139
|
|
|
$multiplier = 1; |
140
|
|
|
if ($useRetina) { |
141
|
|
|
$multiplier = 2; |
142
|
|
|
} |
143
|
|
|
$perfectWidth = $perfectWidth * $multiplier; |
144
|
|
|
$perfectHeight = $perfectHeight * $multiplier; |
145
|
|
|
|
146
|
|
|
//get current width and height |
147
|
|
|
$myWidth = $image->getWidth(); |
148
|
|
|
$myHeight = $image->getHeight(); |
149
|
|
|
// $backEndString = Image::get_backend(); |
|
|
|
|
150
|
|
|
// $backend = Injector::inst()->get($backEndString); |
|
|
|
|
151
|
|
|
if ($perfectWidth && $perfectHeight) { |
152
|
|
|
|
153
|
|
|
//if the height or the width are already perfect then we can not do anything about it. |
154
|
|
|
if ($myWidth == $perfectWidth && $myHeight == $perfectHeight) { |
155
|
|
|
$link = $image->Link(); |
156
|
|
|
} elseif ($crop) { |
157
|
|
|
$link = $image->Fill($perfectWidth, $perfectHeight)->Link(); |
158
|
|
|
} elseif ($myWidth < $perfectWidth || $myHeight < $perfectHeight) { |
159
|
|
|
$link = $image->Pad( |
160
|
|
|
$perfectWidth, |
161
|
|
|
$perfectHeight, |
162
|
|
|
PerfectCMSImageDataExtension::get_padding_bg_colour($name) |
|
|
|
|
163
|
|
|
)->Link(); |
164
|
|
|
} else { |
165
|
|
|
$link = $image->FitMax($perfectWidth, $perfectHeight)->Link(); |
166
|
|
|
} |
167
|
|
|
} elseif ($perfectWidth) { |
168
|
|
|
if ($myWidth == $perfectWidth) { |
169
|
|
|
$link = $image->Link(); |
170
|
|
|
} elseif ($crop) { |
171
|
|
|
$link = $image->Fill($perfectHeight, $myHeight)->Link(); |
172
|
|
|
} else { |
173
|
|
|
$link = $image->ScaleWidth($perfectWidth)->Link(); |
174
|
|
|
} |
175
|
|
|
} elseif ($perfectHeight) { |
176
|
|
|
if ($myHeight == $perfectHeight) { |
177
|
|
|
$link = $image->Link(); |
178
|
|
|
} elseif ($crop) { |
179
|
|
|
$link = $image->Fill($myWidth, $perfectHeight)->Link(); |
180
|
|
|
} else { |
181
|
|
|
$link = $image->ScaleHeight($perfectHeight)->Link(); |
182
|
|
|
} |
183
|
|
|
} else { |
184
|
|
|
$link = $image->ScaleWidth($myWidth)->Link(); |
185
|
|
|
} |
186
|
|
|
$path_parts = pathinfo($link); |
187
|
|
|
|
188
|
|
|
if (class_exists('HashPathExtension')) { |
189
|
|
|
if ($curr = Controller::curr()) { |
190
|
|
|
if ($curr->hasMethod('HashPath')) { |
191
|
|
|
$link = $curr->HashPath($link, false); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
$imageClasses = Config::inst()->get('PerfectCMSImageDataExtension', 'perfect_cms_images_append_title_to_image_links_classes'); |
196
|
|
|
if (in_array($image->ClassName, $imageClasses) && $image->Title) { |
197
|
|
|
$link = $this->replaceLastInstance( |
198
|
|
|
'.'.$path_parts['extension'], |
199
|
|
|
'.pci/'.$image->Title.'.'.$path_parts['extension'], |
200
|
|
|
$link |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $link; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
// no image -> provide placeholder if in DEV MODE only!!! |
209
|
|
|
if (Director::isDev()) { |
210
|
|
|
if ($perfectWidth || $perfectHeight) { |
211
|
|
|
if (!$perfectWidth) { |
212
|
|
|
$perfectWidth = $perfectHeight; |
213
|
|
|
} |
214
|
|
|
if (!$perfectHeight) { |
215
|
|
|
$perfectHeight = $perfectWidth; |
216
|
|
|
} |
217
|
|
|
$text = "$perfectWidth x $perfectHeight /2 = ".round($perfectWidth/2)." x ".round($perfectHeight/2).""; |
218
|
|
|
|
219
|
|
|
return 'https://placehold.it/'.($perfectWidth).'x'.($perfectHeight).'?text='.urlencode($text); |
220
|
|
|
} else { |
221
|
|
|
return 'https://placehold.it/500x500?text='.urlencode('no size set'); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param string $name |
228
|
|
|
* |
229
|
|
|
* @return boolean |
230
|
|
|
*/ |
231
|
|
|
public static function image_info_available($name) |
232
|
|
|
{ |
233
|
|
|
$sizes = self::get_all_values_for_images(); |
234
|
|
|
//print_r($sizes);die(); |
|
|
|
|
235
|
|
|
return isset($sizes[$name]) ? true : false; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @param string $name |
241
|
|
|
* |
242
|
|
|
* @return boolean |
243
|
|
|
*/ |
244
|
|
|
public static function use_retina($name) |
245
|
|
|
{ |
246
|
|
|
return self::get_one_value_for_image($name, "use_retina", true); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param string $name |
252
|
|
|
* |
253
|
|
|
* @return boolean |
254
|
|
|
*/ |
255
|
|
|
public static function crop($name) |
256
|
|
|
{ |
257
|
|
|
return self::get_one_value_for_image($name, "crop", false); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @param string $name |
262
|
|
|
* @param bool $forceInteger |
263
|
|
|
* |
264
|
|
|
* @return int |
265
|
|
|
*/ |
266
|
|
View Code Duplication |
public static function get_width($name, $forceInteger = false) |
|
|
|
|
267
|
|
|
{ |
268
|
|
|
$v = self::get_one_value_for_image($name, "width", 0); |
269
|
|
|
if ($forceInteger) { |
270
|
|
|
$v = intval($v) - 0; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return $v; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @param string $name |
278
|
|
|
* @param bool $forceInteger |
279
|
|
|
* |
280
|
|
|
* @return int |
281
|
|
|
*/ |
282
|
|
View Code Duplication |
public static function get_height($name, $forceInteger) |
|
|
|
|
283
|
|
|
{ |
284
|
|
|
$v = self::get_one_value_for_image($name, "height", 0); |
285
|
|
|
if ($forceInteger) { |
286
|
|
|
$v = intval($v) - 0; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
return $v; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @param string $name |
294
|
|
|
* |
295
|
|
|
* @return string |
296
|
|
|
*/ |
297
|
|
|
public static function get_folder($name) |
298
|
|
|
{ |
299
|
|
|
return self::get_one_value_for_image($name, "folder", 'other-images'); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @param string $name |
304
|
|
|
* |
305
|
|
|
* @return int |
306
|
|
|
*/ |
307
|
|
|
public static function max_size_in_kilobytes($name) |
308
|
|
|
{ |
309
|
|
|
return self::get_one_value_for_image($name, "max_size_in_kilobytes", 0); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @param string $name |
314
|
|
|
* |
315
|
|
|
* @return string |
316
|
|
|
*/ |
317
|
|
|
public static function get_file_type($name) |
318
|
|
|
{ |
319
|
|
|
return self::get_one_value_for_image($name, "filetype", 'jpg'); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @param string $name |
324
|
|
|
* |
325
|
|
|
* @return boolean |
326
|
|
|
*/ |
327
|
|
|
public static function get_enforce_size($name) |
328
|
|
|
{ |
329
|
|
|
return self::get_one_value_for_image($name, "enforce_size", false); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @param string $name |
334
|
|
|
* |
335
|
|
|
* @return boolean |
336
|
|
|
*/ |
337
|
|
|
public static function get_padding_bg_colour($name) |
338
|
|
|
{ |
339
|
|
|
return self::get_one_value_for_image( |
340
|
|
|
$name, |
341
|
|
|
"padding_bg_colour", |
342
|
|
|
Config::inst()->get('PerfectCMSImageDataExtension', 'perfect_cms_images_background_padding_color') |
343
|
|
|
); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* @param string $name |
348
|
|
|
* @param int $key |
349
|
|
|
* @param mixed $default |
350
|
|
|
* |
351
|
|
|
* @return mixed |
352
|
|
|
*/ |
353
|
|
|
private static function get_one_value_for_image($name, $key, $default = '') |
354
|
|
|
{ |
355
|
|
|
$sizes = self::get_all_values_for_images(); |
356
|
|
|
//print_r($sizes);die(); |
|
|
|
|
357
|
|
|
if (isset($sizes[$name])) { |
358
|
|
|
if (isset($sizes[$name][$key])) { |
359
|
|
|
return $sizes[$name][$key]; |
360
|
|
|
} |
361
|
|
|
} else { |
362
|
|
|
user_error('no information for image with name: '.$name); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
return $default; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* @return array |
|
|
|
|
370
|
|
|
*/ |
371
|
|
|
private static function get_all_values_for_images() |
372
|
|
|
{ |
373
|
|
|
return Config::inst()->get('PerfectCMSImageDataExtension', 'perfect_cms_images_image_definitions'); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* replace the last instance of a string occurence. |
378
|
|
|
* |
379
|
|
|
* @param string $search needle |
380
|
|
|
* @param string $replace new needle |
381
|
|
|
* @param string $subject haystack |
382
|
|
|
* |
383
|
|
|
* @return string |
384
|
|
|
*/ |
385
|
|
|
private function replaceLastInstance($search, $replace, $subject) |
386
|
|
|
{ |
387
|
|
|
$pos = strrpos($subject, $search); |
388
|
|
|
|
389
|
|
|
if ($pos !== false) { |
390
|
|
|
$subject = substr_replace($subject, $replace, $pos, strlen($search)); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
return $subject; |
394
|
|
|
} |
395
|
|
|
} |
396
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.