soo_image user guide Page 7 of 16 « »
soo_image Source code
1 : require_plugin('soo_txp_obj');
2 : @require_plugin('soo_plugin_pref'); // optional
3 :
4 : global $soo_image;
5 :
6 : $soo_image = array(
7 : 'page_param' => 'soo_img_pg',
8 : 'jump_param' => 'soo_img_jp',
9 : );
10 : $soo_image_prefs = function_exists('soo_plugin_pref_vals') ?
11 : soo_plugin_pref_vals('soo_image') : soo_image_defaults();
12 : foreach ( $soo_image_prefs as $name => $val )
13 : $soo_image[$name] = is_array($val) ? $val['val'] : $val;
14 :
15 : add_privs('plugin_prefs.soo_image','1,2');
16 : add_privs('plugin_lifecycle.soo_image','1,2');
17 : register_callback('soo_image_prefs', 'plugin_prefs.soo_image');
18 : register_callback('soo_image_prefs', 'plugin_lifecycle.soo_image');
19 :
20 : function soo_image_prefs( $event, $step ) {
21 : if ( function_exists('soo_plugin_pref') )
22 : return soo_plugin_pref($event, $step, soo_image_defaults());
23 : if ( substr($event, 0, 12) == 'plugin_prefs' ) {
24 : $plugin = substr($event, 12);
25 : $message = '<p><br /><strong>' . gTxt('edit') . " $plugin " .
26 : gTxt('edit_preferences') . ':</strong><br />' . gTxt('install_plugin')
27 : . ' <a href="http://ipsedixit.net/txp/92/soo_plugin_pref">soo_plugin_pref</a></p>';
28 : pagetop(gTxt('edit_preferences') . " › $plugin", $message);
29 : }
30 : }
31 :
32 : function soo_image_defaults( ) {
33 : return array(
34 : 'default_form' => array(
35 : 'val' => 'soo_image',
36 : 'html' => 'text_input',
37 : 'text' => 'Default image form',
38 : ),
39 : 'default_listform' => array(
40 : 'val' => '',
41 : 'html' => 'text_input',
42 : 'text' => 'Default image list form',
43 : ),
44 : 'default_dimensions' => array(
45 : 'val' => 1,
46 : 'html' => 'yesnoradio',
47 : 'text' => 'Add height and width in pixels to img tag by default?',
48 : ),
49 : 'persistent_context' => array(
50 : 'val' => 1,
51 : 'html' => 'yesnoradio',
52 : 'text' => 'Use persistent context for tags outside an image form?',
53 : ),
54 : );
55 : }
56 :
57 : //---------------------------------------------------------------------//
58 : // Tags //
59 : //---------------------------------------------------------------------//
60 :
61 : function soo_image_select( $atts, $thing = null ) {
62 :
63 : // Controller for the soo_image plugin.
64 : //
65 : // Determine what images the user is requesting, fetch image data objects
66 : // (or raw URLs, as appropriate) for those images, and pass this along
67 : // to an image form (i.e., a form called by this tag, containing one or
68 : // more of soo_image's output tags) for display.
69 :
70 : global $soo_image, $thisarticle, $context, $c;
71 :
72 : extract(lAtts(array(
73 : 'aspect_ratio' => '',
74 : 'author' => '',
75 : 'break' => '',
76 : 'category' => '',
77 : 'class' => '',
78 : 'ext' => '',
79 : 'form' => $soo_image['default_form'], // required
80 : 'html_id' => '',
81 : 'id' => '',
82 : 'limit' => 0,
83 : 'listform' => $soo_image['default_listform'],
84 : 'name' => '',
85 : 'pagination' => true,
86 : 'persistent_context' => $soo_image['persistent_context'],
87 : 'sort' => 'name asc', // accepts lists, e.g. "a asc,b,c desc"
88 : 'wraptag' => '',
89 : ), $atts));
90 :
91 : $page = intval(gps($soo_image['page_param']));
92 : if ( $page < 1 ) $page = 1;
93 : $offset = $pagination ? ($page - 1) * $limit : 0;
94 :
95 : $jump_to = intval(gps($soo_image['jump_param']));
96 :
97 : $query = new soo_txp_select('txp_image');
98 :
99 : // Selection priority: name; id; (category|author|ext|aspect_ratio); article image.
100 : // Comma-separated lists allowed for all criteria; preserve list order for name|id.
101 :
102 : if ( $name or $id ) {
103 : $sort = '';
104 : if ( $name )
105 : $query->in('name', $name)->order_by_field('name', $name);
106 : else
107 : $ids = _soo_range_to_list($id);
108 : }
109 :
110 : elseif ( $category or $author or $ext or $aspect_ratio ) {
111 : if ( $category ) $query->in('category', $category);
112 : if ( $author ) $query->in('author', $author);
113 : if ( $ext ) $query->in('ext', _soo_valid_ext($ext));
114 : if ( $aspect_ratio ) _soo_image_aspect_ratio($query, trim($aspect_ratio));
115 : }
116 :
117 : elseif ( ! empty($thisarticle) ) {
118 : $image_array = _soo_article_image_list();
119 : if ( ! $image_array )
120 : return false;
121 : foreach ( $image_array as $img ) {
122 : if ( is_numeric($img) )
123 : $ids[] = $img;
124 : else
125 : $urls[] = $img;
126 : }
127 : }
128 :
129 : elseif ( $context == gTxt('image_context') and $c ) {
130 : $query->in('category', $c);
131 : }
132 :
133 : else return;
134 :
135 : if ( isset($ids) ) {
136 : $sort = '';
137 : $query->in('id', $ids)->order_by_field('id', $ids);
138 : }
139 :
140 : $full_count = $query->count();
141 : if ( $pagination && $full_count <= $offset ) { // in case of direct URI manipulation and invalid #
142 : $page = $limit ? ceil($full_count / $limit) : 1; // go to last page
143 : $offset = ($page - 1) * $limit;
144 : }
145 :
146 : $soo_image['this_page'] = $page;
147 : if ( $pagination ) $soo_image['total_pages'] = $limit ? ceil($full_count / $limit) : 1;
148 :
149 : if ( $jump_to and is_numeric($jump_to) ) {
150 : $uri = new soo_uri;
151 : $uri->set_query_param($soo_image['jump_param']);
152 : $all_ids = new soo_txp_rowset($query->order_by($sort));
153 : $all_ids = array_flip($all_ids->field_vals('id'));
154 : if ( isset($all_ids[$jump_to]) ) {
155 : $jump_index = $all_ids[$jump_to];
156 : if ( $limit ) {
157 : $page = ceil(($jump_index + 1) / $limit);
158 : $offset = ($page - 1) * $limit;
159 : $_POST[$soo_image['page_param']] = $page;
160 : }
161 : }
162 : }
163 :
164 : $query->order_by($sort)->limit($limit)->offset($offset);
165 : $rs = new soo_txp_rowset($query);
166 : unset($query);
167 :
168 : if ( isset($urls) )
169 : foreach ( $image_array as $i => $img ) {
170 : if ( isset($rs->rows[$img]) )
171 : $image_array[$i] = $rs->rows[$img];
172 : elseif ( ! in_array($img, $urls) )
173 : unset($image_array[$i]);
174 : }
175 : else
176 : $image_array = $rs->rows;
177 :
178 : if ( empty($image_array) )
179 : return false;
180 :
181 : $soo_image['count'] = count($image_array);
182 :
183 : if ( $pagination && $limit && ( $offset + $limit < $full_count ) )
184 : $soo_image['next'] = $page + 1;
185 :
186 : // $image_array now contains data objects and/or presumed URLs
187 :
188 : if ( ( $soo_image['count'] > 1 or $page > 1) and $listform )
189 : $form = $listform;
190 :
191 : $soo_image['first_selection'] = isset($jump_index) ? new soo_txp_img($jump_to) : current($image_array);
192 :
193 : foreach ( $image_array as $img ) {
194 : if ( $img instanceof soo_txp_img )
195 : $soo_image['data_obj'] = $img;
196 : else
197 : $soo_image['url'] = $img;
198 : $parsed = is_null($thing) ? parse_form($form) : parse($thing);
199 : if ( $parsed ) $out[] = $parsed;
200 : unset($soo_image['data_obj']);
201 : unset($soo_image['url']);
202 : }
203 :
204 : if ( ! $persistent_context )
205 : unset($soo_image['first_selection']);
206 :
207 : unset($soo_image['count']);
208 :
209 : return isset($out) ? doWrap($out, $wraptag, $break, $class, '', '', '', $html_id) : false;
210 :
211 : }
212 : ////////////////////// end of soo_image_select /////////////////////
213 :
214 : function soo_image( $atts ) {
215 : // Display an image. Selection by, in descending order of priority:
216 : // the tag's 'name' attribute;
217 : // the tag's 'id' attribute;
218 : // the image context:
219 : // id or url sent by $soo_image to an image form
220 : // article image
221 : // first selection from prior soo_image_select (subject to pref setting)
222 :
223 : extract(lAtts(array(
224 : 'id' => '',
225 : 'name' => '',
226 : 'thumbnail' => false,
227 : 'height' => '',
228 : 'width' => '',
229 : 'html_id' => '',
230 : 'class' => '',
231 : 'title' => '{caption}',
232 : 'link' => false,
233 : 'link_class' => '',
234 : 'link_id' => '',
235 : 'link_rel' => '',
236 : 'link_to' => '',
237 : 'escape' => true,
238 : 'onclick' => '', // only for linked thumbnails
239 : ), $atts));
240 :
241 : global $soo_image;
242 :
243 : if ( $link_class or $link_id or $link_rel or $onclick or $link_to )
244 : $link = true;
245 :
246 : if ( $link )
247 : $thumbnail = true;
248 :
249 : if ( $name or $id )
250 : $image = new soo_txp_img($name ? $name : $id);
251 : else
252 : $image = _soo_image_by_context();
253 : // false if image context is empty
254 : // otherwise a soo_txp_img or soo_html_img object
255 : // if soo_html_img it will be URL only, no other properties
256 :
257 : if ( $image instanceof soo_html_img and $thumbnail )
258 : return false;
259 :
260 : if ( $image instanceof soo_txp_img ) {
261 : if ( $thumbnail and !$image->thumbnail )
262 : return false;
263 : $image_data = $image;
264 : $image = new soo_html_img($image_data, $thumbnail, $escape);
265 : $title = str_replace(array('{author}', '{alt}', '{caption}'),
266 : array(get_author_name($image_data->author), $image->alt, $image->title), $title);
267 : }
268 :
269 : if ( ! ( $image instanceof soo_html_img ) )
270 : return false;
271 :
272 : // Standard height and width attributes (i.e., actual size in pixels)
273 : // will be added to the img tag if the default_dimensions preference is true
274 : // and has not been overriden (by specifying height or width other than 1),
275 : // or if either attribute has been set to 1. Otherwise, height and width
276 : // attributes will be passed through as is (in which case a value of 0
277 : // or empty will result in no attribute in the img tag).
278 :
279 : if ( $width === '0' or $height === '0' or $width > 1 or $height > 1 )
280 : $soo_image['default_dimensions'] = false;
281 :
282 : if ( ! $soo_image['default_dimensions'] and $height != 1 and $width != 1 )
283 : $image->height($height)->width($width);
284 :
285 : $image->class($class)
286 : ->id($html_id)
287 : ->title($title);
288 :
289 : // Shadowbox-specific block to create galleries
290 : if ( strtolower($link_rel) == "shadowbox"
291 : and isset($soo_image['first_selection'])
292 : and $soo_image['first_selection'] instanceof soo_txp_img ) {
293 : $img_obj = $soo_image['first_selection'];
294 : $link_rel .= '[' . $img_obj->id . ']';
295 : }
296 :
297 : if ( $thumbnail and $link ) {
298 : if ( $link_to )
299 : $url = hu . $link_to . '?' . $soo_image['jump_param']
300 : . '=' . $image_data->id;
301 : else
302 : $url = $image_data->full_url;
303 : $anchor = new soo_html_anchor($url, $image);
304 : $anchor->title($title)
305 : ->onclick($onclick)
306 : ->rel($link_rel)
307 : ->class($link_class)
308 : ->id($link_id);
309 : }
310 :
311 : return isset($anchor) ? $anchor->tag() : $image->tag();
312 : }
313 : ///////////////////////// end of soo_image //////////////////////////
314 :
315 : function soo_image_alt($atts) { return _soo_image_data('alt', $atts); }
316 : function soo_image_author($atts) { return _soo_image_data('author', $atts); }
317 : function soo_image_caption($atts) { return _soo_image_data('caption', $atts); }
318 : function soo_image_category($atts) { return _soo_image_data('category', $atts); }
319 : function soo_image_date($atts) { return _soo_image_data('date', $atts); }
320 : function soo_image_height($atts) { return _soo_image_data('h', $atts); }
321 : function soo_image_id($atts) { return _soo_image_data('id', $atts); }
322 : function soo_image_name($atts) { return _soo_image_data('name', $atts); }
323 : function soo_image_url($atts) { return _soo_image_data('full_url', $atts); }
324 : function soo_image_width($atts) { return _soo_image_data('w', $atts); }
325 : function soo_thumbnail_height($atts) { return _soo_image_data('thumb_h', $atts); }
326 : function soo_thumbnail_url($atts) { return _soo_image_data('thumb_url', $atts); }
327 : function soo_thumbnail_width($atts) { return _soo_image_data('thumb_w', $atts); }
328 :
329 : function _soo_image_data($field, $atts = array()) {
330 : extract(lAtts(array(
331 : 'format' => '',
332 : 'escape' => 1,
333 : 'no_widow' => 1,
334 : 'wraptag' => '',
335 : 'class' => '',
336 : 'html_id' => '',
337 : ), $atts));
338 : $image = _soo_image_by_context();
339 : if ( ! $image instanceof soo_txp_img )
340 : $out = $field == 'url' ? $image : '';
341 : else
342 : switch ( $field ) {
343 : case 'alt':
344 : $out = $escape ? htmlspecialchars($image->alt) : $image->alt;
345 : break;
346 : case 'author':
347 : $out = get_author_name($image->author);
348 : break;
349 : case 'caption':
350 : $out = $escape ? htmlspecialchars($image->caption) : $image->caption;
351 : $out = $no_widow ? preg_replace('/\s+(\S+)$/', ' \1', $out) : $out;
352 : break;
353 : case 'date':
354 : global $dateformat;
355 : $f = $format ? $format : $dateformat;
356 : $utime = safe_strtotime($image->date);
357 : $out = $image->date ? safe_strftime($f, $utime) : '';
358 : break;
359 : default:
360 : $out = $image->$field;
361 : }
362 : return doWrap(array($out), $wraptag, '', $class, '', '', '', $html_id);
363 : }
364 :
365 : function soo_if_txp_image($atts, $thing) {
366 :
367 : $image = _soo_image_by_context();
368 : return parse(EvalElse($thing, $image instanceof soo_txp_img ));
369 : }
370 :
371 : function soo_if_image_author($atts, $thing) {
372 :
373 : extract(lAtts(array('name' => ''), $atts));
374 : $image = _soo_image_by_context();
375 : return parse(EvalElse($thing, $image instanceof soo_txp_img and in_list($image->author, $name) ));
376 : }
377 :
378 : function soo_if_image_category($atts, $thing) {
379 :
380 : extract(lAtts(array('name' => ''), $atts));
381 : $image = _soo_image_by_context();
382 : return parse(EvalElse($thing, $image instanceof soo_txp_img and in_list($image->category, $name) ));
383 : }
384 :
385 : function soo_if_image_thumbnail($atts, $thing) {
386 :
387 : $image = _soo_image_by_context();
388 : return parse(EvalElse($thing, $image instanceof soo_txp_img and $image->thumbnail));
389 : }
390 :
391 : function soo_if_image_count($atts, $thing) {
392 : // Expected context:
393 : // - Inside a soo_image_select tag/form, else
394 : // - Inside an article/article_custom tag/form, else
395 : // - On an image category page
396 : // Otherwise the tag returns empty.
397 : extract(lAtts(array(
398 : 'exact' => null,
399 : 'min' => null,
400 : 'max' => null,
401 : ), $atts));
402 : global $soo_image, $thisarticle, $context, $c;
403 :
404 : if ( isset($soo_image['count']) ) // inside a soo_image_select
405 : $count = $soo_image['count'];
406 : elseif ( ! empty($thisarticle) )
407 : $count = $thisarticle['article_image'] ? count(do_list($thisarticle['article_image'])) : 0;
408 : elseif ( $context == gTxt('image_context') and ! empty($c) )
409 : $count = safe_count('txp_image', 'category = "' . $c . '"');
410 : else return;
411 :
412 : if ( ! is_null($exact) )
413 : $condition = $count == $exact;
414 : elseif ( ! is_null($min) )
415 : $condition = $count >= $min;
416 : elseif ( ! is_null($max) )
417 : $condition = $count <= $max;
418 : else return;
419 :
420 : return parse(EvalElse($thing, $condition));
421 : }
422 :
423 : function soo_image_next($atts, $thing = null) {
424 :
425 : extract(lAtts(array(
426 : 'link_text' => '→',
427 : 'class' => '',
428 : 'html_id' => '',
429 : ), $atts));
430 : global $soo_image;
431 :
432 : if ( ! isset($soo_image['next']) )
433 : return soo_util::secondpass(__FUNCTION__, $atts, $thing);
434 : $uri = new soo_uri;
435 : $uri->set_query_param($soo_image['jump_param']);
436 : $uri->set_query_param($soo_image['page_param'], $soo_image['next']);
437 : $out = new soo_html_anchor($uri->full, $thing ? $thing : $link_text);
438 : return $out->class($class)->id($html_id)->tag();
439 : }
440 :
441 : function soo_image_prev($atts, $thing = null) {
442 :
443 : extract(lAtts(array(
444 : 'link_text' => '←',
445 : 'class' => '',
446 : 'html_id' => '',
447 : ), $atts));
448 : global $soo_image;
449 :
450 : $prev = intval(gps($soo_image['page_param'])) - 1;
451 : if ( $prev < 1 )
452 : return soo_util::secondpass(__FUNCTION__, $atts, $thing);
453 : $uri = new soo_uri;
454 : $uri->set_query_param($soo_image['jump_param']);
455 : $uri->set_query_param($soo_image['page_param'], $prev > 1 ? $prev : null);
456 : $out = new soo_html_anchor($uri->full, $thing ? $thing : $link_text);
457 : return $out->class($class)->id($html_id)->tag();
458 : }
459 :
460 : function soo_image_page_count($atts) {
461 :
462 : extract(lAtts(array(
463 : 'format' => 'Page {current} of {total}',
464 : 'showalways' => false,
465 : ), $atts));
466 : global $soo_image;
467 :
468 : if ( ! isset($soo_image['this_page']) )
469 : return soo_util::secondpass(__FUNCTION__, $atts);
470 :
471 : if ( ! $showalways and $soo_image['total_pages'] <= 1 ) return;
472 :
473 : return str_replace(array('{current}', '{total}'),
474 : array($soo_image['this_page'], $soo_image['total_pages']), $format);
475 : }
476 :
477 : function soo_exif($atts, $thing) {
478 :
479 : global $dateformat, $soo_exif_field, $soo_exif_value;
480 : extract(lAtts(array(
481 : 'field' => '',
482 : 'format' => '{field}: {value}',
483 : 'wraptag' => '',
484 : 'break' => '',
485 : ), $atts));
486 :
487 : $display = array(
488 : 'Model' => 'Camera',
489 : 'ExposureTime' => 'Shutter speed',
490 : 'FNumber' => 'F-stop',
491 : 'ISOSpeedRatings' => 'ISO speed',
492 : 'DateTimeOriginal' => 'Exposure date',
493 : 'FocalLength' => 'Focal length',
494 : 'CropFactor' => 'Crop factor',
495 : 'FOV' => 'Computed FOV',
496 : 'ImageHistory' => 'History',
497 : );
498 :
499 : $shortcuts = array(
500 : 'model' => 'Model',
501 : 'exp-time' => 'ExposureTime',
502 : 'f-number' => 'FNumber',
503 : 'iso-speed' => 'ISOSpeedRatings',
504 : 'date-taken' => 'DateTimeOriginal',
505 : 'focal-len' => 'FocalLength',
506 : 'crop' => 'CropFactor',
507 : 'history' => 'ImageHistory',
508 : );
509 :
510 : if ( empty($field) )
511 : $fields = array_keys($display);
512 :
513 : else {
514 : $fields = do_list($field);
515 : foreach ( $fields as $i => $f )
516 : if ( array_key_exists(strtolower($f), $shortcuts) )
517 : $fields[$i] = $shortcuts[$f];
518 : }
519 :
520 : $exif = exif_read_data(_soo_image_path());
521 :
522 : extract($exif);
523 :
524 : if ( isset($FNumber) )
525 : $exif['FNumber'] = 'ƒ/' . round(_soo_image_convert_ratio($FNumber), 1);
526 :
527 : if ( isset($FocalLength) )
528 : $exif['FocalLength'] = _soo_image_convert_ratio($FocalLength) . 'mm';
529 :
530 : if ( isset($FocalLengthIn35mmFilm) ) {
531 : $fl35 = _soo_image_convert_ratio($FocalLengthIn35mmFilm);
532 : if ( isset($FocalLength) )
533 : $exif['CropFactor'] = round($fl35 / _soo_image_convert_ratio($FocalLength), 1);
534 : $exif['FocalLengthIn35mmFilm'] = $fl35 . 'mm';
535 : // 43.27 is the diagonal length (mm) of a full-frame sensor
536 : $exif['FOV'] = round(rad2deg(2 * atan(43.27 / ( 2 * $fl35 ))), 1) . '°';
537 : }
538 :
539 : if ( isset($DateTimeOriginal) )
540 : $exif['DateTimeOriginal'] =
541 : safe_strftime($dateformat, strtotime($DateTimeOriginal));
542 :
543 : if ( strtolower($fields[0]) == 'dump' )
544 : $fields = array_keys($exif);
545 :
546 : foreach ( $fields as $k ) {
547 : if ( isset($exif[$k]) ) {
548 : $v = $exif[$k];
549 : $soo_exif_field = isset($display[$k]) ? $display[$k] : $k;
550 : $soo_exif_value = is_array($v) ? implode(' ', $v) : trim($v);
551 : $r_format = str_replace('{field}', $soo_exif_field, $format);
552 : $r_format = str_replace('{value}', $soo_exif_value, $r_format);
553 : $out[] = $thing ? parse($thing) : $r_format;
554 : }
555 : }
556 : return isset($out) ? doWrap($out, $wraptag, $break) : '';
557 : }
558 :
559 : function soo_exif_field($atts) {
560 :
561 : global $soo_exif_field;
562 : extract(lAtts(array(
563 : 'wraptag' => '',
564 : ), $atts));
565 :
566 : if ( $soo_exif_field )
567 : return doWrap(array($soo_exif_field), $wraptag, '');
568 : }
569 :
570 : function soo_exif_value($atts) {
571 :
572 : global $soo_exif_value;
573 : extract(lAtts(array(
574 : 'wraptag' => '',
575 : ), $atts));
576 :
577 : if ( $soo_exif_value ) {
578 : if ( ! is_array($soo_exif_value) )
579 : $soo_exif_value = array($soo_exif_value);
580 : return doWrap($soo_exif_value, $wraptag, '');
581 : }
582 : }
583 :
584 : //---------------------------------------------------------------------//
585 : // Support Functions //
586 : //---------------------------------------------------------------------//
587 :
588 : function _soo_image_path() {
589 :
590 : global $img_dir;
591 :
592 : $img_dir_path = $_SERVER['DOCUMENT_ROOT'] . "/$img_dir/";
593 : $image = _soo_image_by_context();
594 :
595 : if ( $image instanceof soo_txp_img )
596 : $file_path = $img_dir_path . $image->id . $image->ext;
597 :
598 : elseif ( $image instanceof soo_html_img ) {
599 : $pattern = '&^(' . str_replace('.', '\.', hu) . "|\/)($img_dir.+)&";
600 : if ( preg_match($pattern, $image->src, $match) )
601 : $file_path = $match[2];
602 : }
603 :
604 : if ( empty($file_path) or ! file_exists($file_path) )
605 : return null;
606 :
607 : return $file_path;
608 : }
609 :
610 : function _soo_article_image_list() {
611 : global $thisarticle;
612 : return empty($thisarticle['article_image']) ?
613 : array() : _soo_range_to_list($thisarticle['article_image']);
614 : }
615 :
616 : function _soo_range_to_list( $csv ) {
617 :
618 : $items = do_list($csv);
619 : foreach ( $items as $item )
620 : if ( preg_match('/(\d+)(:|-)(\d+)/', $item, $match) ) {
621 : if ( $match[3] > $match[1] )
622 : for ( $i = $match[1]; $i <= $match[3]; $i++ )
623 : $out[] = $i;
624 : else
625 : for ( $i = $match[1]; $i >= $match[3]; $i-- )
626 : $out[] = $i;
627 : }
628 : else
629 : $out[] = $item;
630 : return $out;
631 : }
632 :
633 : function _soo_valid_ext( $in ) {
634 : $ok = array('jpg', 'jpeg', 'gif', 'png', 'bmp',
635 : '.jpg', '.jpeg', '.gif', '.png', '.bmp');
636 : $exts = is_array($in) ? $in : do_list($in);
637 : foreach ($exts as $i => $value)
638 : if ( !in_array($value, $ok) )
639 : unset($exts[$i]);
640 : else
641 : $exts[$i] = '.' . $value;
642 : return $exts;
643 : }
644 :
645 : function _soo_image_convert_ratio( $r ) {
646 : if ( is_numeric($r) )
647 : return $r;
648 : if ( ! preg_match('/(.+)(:|\/)(.+)/', $r, $match) )
649 : return null;
650 : if ( is_numeric($match[1]) and is_numeric($match[3]) and $match[3] != 0 )
651 : return $match[1] / $match[3];
652 : else
653 : return null;
654 : }
655 :
656 : function _soo_image_aspect_ratio( $query, $aspect_ratio ) {
657 :
658 : $fudge = 0.01;
659 : $pattern = '/^(\+|-)?([^-]+)(-)?(.+)?$/';
660 : if ( ! preg_match($pattern, $aspect_ratio, $match) )
661 : return;
662 :
663 : if ( $match[1] == '+' ) {
664 : $min_ar = _soo_image_convert_ratio($match[2]);
665 : if ( is_null($min_ar) )
666 : return;
667 : }
668 : elseif ( $match[1] == '-' ) {
669 : $max_ar = _soo_image_convert_ratio($match[2]);
670 : if ( is_null($max_ar) )
671 : return;
672 : }
673 :
674 : elseif ( isset($match[3]) ) {
675 : $min_ar = _soo_image_convert_ratio($match[2]);
676 : $max_ar = _soo_image_convert_ratio($match[4]);
677 : if ( is_null($min_ar) or is_null($max_ar) )
678 : return;
679 : if ( $max_ar < $min_ar )
680 : list($max_ar, $min_ar) = array($min_ar, $max_ar);
681 : }
682 : else {
683 : $ar = _soo_image_convert_ratio($match[2]);
684 : if ( is_null($ar) )
685 : return;
686 : $query->where('w/h', $ar + $fudge, '<')
687 : ->where('w/h', $ar - $fudge, '>');
688 : return;
689 : }
690 : if ( isset($min_ar) )
691 : $query->where('w/h', $min_ar - $fudge, '>=');
692 : if ( isset($max_ar) )
693 : $query->where('w/h', $max_ar + $fudge, '<=');
694 :
695 : }
696 :
697 : function _soo_image_by_context( ) {
698 : // Return a single image object (Txp or Html) for the current image context.
699 : // Image passed by parent soo_image_select tag, or;
700 : // first article image, or;
701 : // first selection from previous soo_image_select tag (subject to prefs).
702 : // Return false if image context is empty
703 :
704 : global $soo_image;
705 :
706 : if ( isset($soo_image['data_obj']) )
707 : $img = $soo_image['data_obj'];
708 :
709 : elseif ( isset($soo_image['url']) )
710 : $img = $soo_image['url'];
711 :
712 : elseif ( $article_image = array_shift(_soo_article_image_list()) )
713 : $img = $article_image;
714 :
715 : elseif ( ! empty($soo_image['first_selection']) )
716 : $img = $soo_image['first_selection'];
717 :
718 : if ( empty($img) ) return false;
719 : if ( is_object($img) ) return $img;
720 : if ( is_numeric($img) ) return new soo_txp_img($img);
721 : return new soo_html_img(array('src' => $img));
722 : }

