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
|
|
|
* @param string $name |
56
|
|
|
* @param object (optional) $backupObject |
57
|
|
|
* @param string (optional) $backupField |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function PerfectCMSImageLink( |
62
|
|
|
$name, |
63
|
|
|
$backupObject = null, |
64
|
|
|
$backupField = '', |
65
|
|
|
$isRetina = true |
66
|
|
|
) { |
67
|
|
|
if (! Config::inst()->get('Image', 'force_resample')) { |
68
|
|
|
Config::inst()->update('Image', 'force_resample', true); |
69
|
|
|
} |
70
|
|
|
$image = $this->owner; |
71
|
|
|
if ($image && $image->exists()) { |
|
|
|
|
72
|
|
|
//we are all good ... |
73
|
|
|
} else { |
74
|
|
|
if (!$backupObject) { |
75
|
|
|
$backupObject = SiteConfig::current_site_config(); |
76
|
|
|
} |
77
|
|
|
if (!$backupField) { |
78
|
|
|
$backupField = $name; |
79
|
|
|
} |
80
|
|
|
if ($backupObject->hasMethod($backupField)) { |
81
|
|
|
$image = $backupObject->$backupField(); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$perfectWidth = (intval(self::get_width($name)) - 0); |
86
|
|
|
$perfectHeight = (intval(self::get_height($name)) - 0); |
87
|
|
|
if ($isRetina) { |
88
|
|
|
$perfectWidth = $perfectWidth * 2; |
89
|
|
|
$perfectHeight = $perfectHeight * 2; |
90
|
|
|
} |
91
|
|
|
if ($image) { |
92
|
|
|
if ($image instanceof Image) { |
93
|
|
|
if ($image->exists()) { |
94
|
|
|
//get preferred width and height |
95
|
|
|
$myWidth = $image->getWidth(); |
96
|
|
|
$myHeight = $image->getHeight(); |
97
|
|
|
$backEndString = Image::get_backend(); |
98
|
|
|
$backend = Injector::inst()->get($backEndString); |
|
|
|
|
99
|
|
|
if ($perfectWidth && $perfectHeight) { |
100
|
|
|
if ($myWidth == $perfectWidth || $myHeight == $perfectHeight) { |
101
|
|
|
$link = $image->ScaleWidth($myWidth)->Link(); |
102
|
|
|
} elseif ($myWidth < $perfectWidth || $myHeight < $perfectHeight) { |
103
|
|
|
$link = $image->Pad( |
104
|
|
|
$perfectWidth, |
105
|
|
|
$perfectHeight, |
106
|
|
|
Config::inst()->get('PerfectCMSImageDataExtension', 'perfect_cms_images_background_padding_color') |
107
|
|
|
)->Link(); |
108
|
|
|
} elseif ($myWidth > $perfectWidth || $myHeight > $perfectHeight) { |
109
|
|
|
$link = $image->FitMax($perfectWidth, $perfectHeight)->Link(); |
110
|
|
|
} |
111
|
|
|
} elseif ($perfectWidth) { |
112
|
|
|
$link = $image->ScaleWidth($perfectWidth)->Link(); |
113
|
|
|
} elseif ($perfectHeight) { |
114
|
|
|
$link = $image->ScaleHeight($perfectHeight)->Link(); |
115
|
|
|
} else { |
116
|
|
|
$link = $image->ScaleWidth($myWidth)->Link(); |
117
|
|
|
} |
118
|
|
|
$path_parts = pathinfo($link); |
|
|
|
|
119
|
|
|
|
120
|
|
|
if (class_exists('HashPathExtension')) { |
121
|
|
|
if ($curr = Controller::curr()) { |
122
|
|
|
if ($curr->hasMethod('HashPath')) { |
123
|
|
|
$link = $curr->HashPath($link, false); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
$imageClasses = Config::inst()->get('PerfectCMSImageDataExtension', 'perfect_cms_images_append_title_to_image_links_classes'); |
128
|
|
|
if(in_array($image->ClassName, $imageClasses) && $image->Title){ |
129
|
|
|
$link = $this->replaceLastInstance( |
130
|
|
|
'.'.$path_parts['extension'], |
131
|
|
|
'.pci/'.$image->Title.'.'.$path_parts['extension'], |
132
|
|
|
$link |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
return $link; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
// no image -> provide placeholder |
140
|
|
|
if ($perfectWidth || $perfectHeight) { |
141
|
|
|
if (!$perfectWidth) { |
142
|
|
|
$perfectWidth = $perfectHeight; |
143
|
|
|
} |
144
|
|
|
if (!$perfectHeight) { |
145
|
|
|
$perfectHeight = $perfectWidth; |
146
|
|
|
} |
147
|
|
|
$text = "$perfectWidth x $perfectHeight /2 = ".round($perfectWidth/2)." x ".round($perfectHeight/2).""; |
148
|
|
|
|
149
|
|
|
return 'https://placehold.it/'.($perfectWidth).'x'.($perfectHeight).'?text='.urlencode($text); |
150
|
|
|
} else { |
151
|
|
|
return 'https://placehold.it/500x500?text='.urlencode('no size set'); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $name |
157
|
|
|
* @param Image (optional) $image |
158
|
|
|
* |
159
|
|
|
* @return int |
160
|
|
|
*/ |
161
|
|
|
public static function get_width($name) |
162
|
|
|
{ |
163
|
|
|
return self::get_one_value_for_image($name, "width", 0); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string $name |
168
|
|
|
* @param Image (optional) $image |
169
|
|
|
* |
170
|
|
|
* @return int |
171
|
|
|
*/ |
172
|
|
|
public static function get_height($name) |
173
|
|
|
{ |
174
|
|
|
return self::get_one_value_for_image($name, "height", 0); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param string $name |
179
|
|
|
* @param Image (optional) $image |
180
|
|
|
* |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
|
|
public static function get_folder($name) |
184
|
|
|
{ |
185
|
|
|
return self::get_one_value_for_image($name, "folder", 'other-images'); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param string $name |
190
|
|
|
* @param Image (optional) $image |
191
|
|
|
* |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
|
|
public static function get_file_type($name) |
195
|
|
|
{ |
196
|
|
|
return self::get_one_value_for_image($name, "filetype", 'jpg'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param string $name |
201
|
|
|
* @param Image (optional) $image |
202
|
|
|
* |
203
|
|
|
* @return boolean |
204
|
|
|
*/ |
205
|
|
|
public static function get_enforce_size($name) |
206
|
|
|
{ |
207
|
|
|
return self::get_one_value_for_image($name, "enforce_size", true); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param string $name |
212
|
|
|
* @param int $key |
213
|
|
|
* @param mixed $default |
214
|
|
|
* |
215
|
|
|
* @return mixed |
216
|
|
|
*/ |
217
|
|
|
private static function get_one_value_for_image($name, $key, $default = '') |
218
|
|
|
{ |
219
|
|
|
$sizes = self::get_all_values_for_images(); |
220
|
|
|
//print_r($sizes);die(); |
|
|
|
|
221
|
|
|
if (isset($sizes[$name])) { |
222
|
|
|
if (isset($sizes[$name][$key])) { |
223
|
|
|
return $sizes[$name][$key]; |
224
|
|
|
} |
225
|
|
|
} else { |
226
|
|
|
user_error('no information for image with name: '.$name); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $default; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @return array |
|
|
|
|
234
|
|
|
*/ |
235
|
|
|
private static function get_all_values_for_images() |
236
|
|
|
{ |
237
|
|
|
return Config::inst()->get('PerfectCMSImageDataExtension', 'perfect_cms_images_image_definitions'); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* replace the last instance of a string occurence. |
242
|
|
|
* |
243
|
|
|
* @param string $search needle |
244
|
|
|
* @param string $replace new needle |
245
|
|
|
* @param string $subject haystack |
246
|
|
|
* |
247
|
|
|
* @return string |
248
|
|
|
*/ |
249
|
|
|
private function replaceLastInstance($search, $replace, $subject) |
250
|
|
|
{ |
251
|
|
|
$pos = strrpos($subject, $search); |
252
|
|
|
|
253
|
|
|
if($pos !== false) |
254
|
|
|
{ |
255
|
|
|
$subject = substr_replace($subject, $replace, $pos, strlen($search)); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return $subject; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
} |
262
|
|
|
|
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.