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