source: extensions/derivatives/include/derivative.inc.php @ 12775

Last change on this file since 12775 was 12775, checked in by rvelices, 12 years ago

derivatives - can display several sizes on picture page

File size: 4.5 KB
Line 
1<?php
2
3final class SrcImage
4{
5  public $rel_path;
6 
7  public $coi=null;
8  private $size=null;
9
10  function __construct($infos)
11  {
12    global $conf;
13
14    $ext = get_extension($infos['path']);
15    if (in_array($ext, $conf['picture_ext']))
16    {
17      $this->rel_path = $infos['path'];
18    }
19    elseif (!empty($infos['representative_ext']))
20    {
21      $pi = pathinfo($infos['path']);
22      $file_wo_ext = get_filename_wo_extension($pi['basename']);
23      $this->rel_path = $pi['dirname'].'/pwg_representative/'
24        .$file_wo_ext.'.'.$infos['representative_ext'];
25    }
26    else
27    {
28      $this->rel_path = get_themeconf('mime_icon_dir').strtolower($ext).'.png';
29    }
30
31    $this->coi = @$infos['coi'];
32    if (isset($infos['width']) && isset($infos['height']))
33    {
34      $this->size = array($infos['width'], $infos['height']);
35    }
36  }
37
38  function has_size()
39  {
40    return $this->size != null;
41  }
42
43  function get_size()
44  {
45    if ($this->size == null)
46      not_implemented(); // get size from file
47    return $this->size;
48  }
49}
50
51
52
53final class DerivativeImage
54{
55  const SAME_AS_SRC = 0x10;
56
57  public $src_image;
58
59  private $requested_type;
60
61  private $flags = 0;
62  private $params;
63  private $rel_path, $rel_url;
64
65  function __construct($type, $src_image)
66  {
67    $this->src_image = $src_image;
68    if (is_string($type))
69    {
70      $this->requested_type = $type;
71      $this->params = ImageStdParams::get_by_type($type);
72    }
73    else
74    {
75      $this->requested_type = IMG_CUSTOM;
76      $this->params = $type;
77    }
78
79    self::build($src_image, $this->params, $this->rel_path, $this->rel_url, $this->flags);
80  }
81
82  static function thumb_url($infos)
83  {
84    $src_image = new SrcImage($infos);
85    self::build($src_image, ImageStdParams::get_by_type(IMG_THUMB), $rel_path, $rel_url);
86    return get_root_url().$rel_url;
87  }
88
89  static function url($type, $infos)
90  {
91    $src_image = new SrcImage($infos);
92    $params = is_string($type) ? ImageStdParams::get_by_type($type) : $type;
93    self::build($src_image, $params, $rel_path, $rel_url);
94    return get_root_url().$rel_url;
95  }
96
97  static function get_all($infos)
98  {
99    $src_image = new SrcImage($infos);
100    $ret = array();
101    foreach (ImageStdParams::get_defined_type_map() as $type => $params)
102    {
103      $derivative = new DerivativeImage($params, $src_image);
104      $ret[$type] = $derivative;
105    }
106    foreach (ImageStdParams::get_undefined_type_map() as $type => $type2)
107    {
108      $ret[$type] = $ret[$type2];
109    }
110   
111    return $ret;
112  }
113
114  private static function build($src, &$params, &$rel_path, &$rel_url, &$flags = null)
115  {
116    if ( $src->has_size() && $params->is_identity( $src->get_size() ) )
117    {
118      // todo - what if we have a watermark maybe return a smaller size?
119      $flags |= self::SAME_AS_SRC;
120      $params = null;
121      $rel_path = $rel_url = $src->rel_path;
122      return;
123    }
124
125    $tokens=array();
126    $tokens[] = substr($params->type,0,2);
127
128    if (!empty($src->coi))
129    {
130      $tokens[] = 'ci'.$src->coi;
131    }
132
133    if ($params->type==IMG_CUSTOM)
134    {
135      $params->add_url_tokens($tokens);
136    }
137
138    $loc = $src->rel_path;
139    if (substr_compare($loc, '../', 0, 3)==0)
140    {
141      $loc = substr($loc, 3);
142    }
143    $loc = substr_replace($loc, '-'.implode('_', $tokens), strrpos($loc, '.'), 0 );
144
145    $rel_path = PWG_DERIVATIVE_DIR.$loc;
146
147    global $conf;
148    $url_style=$conf['derivative_url_style'];
149    if (!$url_style)
150    {
151      $mtime = @filemtime(PHPWG_ROOT_PATH.$rel_path);
152      if ($mtime===false)
153      {
154        $url_style = 2;
155      }
156      else
157      {
158        $url_style = 1;
159      }
160    }
161
162    if ($url_style == 2)
163    {
164      $rel_url = 'i';
165      if ($conf['php_extension_in_urls']) $rel_url .= '.php';
166      if (!$conf['question_mark_in_urls']) $rel_url.= '?';
167      $rel_url .= $loc;
168    }
169    else
170    {
171      $rel_url = $rel_path;
172    }
173  }
174
175  function get_url()
176  {
177    return get_root_url().$this->rel_url;
178  }
179
180  function same_as_source()
181  {
182    return $this->flags & self::SAME_AS_SRC;
183  }
184
185
186  /* returns the size of the derivative image*/
187  function get_size()
188  {
189    if ($this->flags & self::SAME_AS_SRC)
190    {
191      return $this->src_image->get_size();
192    }
193    return $this->params->compute_final_size($this->src_image->get_size(), $this->src_image->coi);
194  }
195
196  function get_size_htm()
197  {
198    $size = $this->get_size();
199    if ($size)
200    {
201      return 'width="'.$size[0].'" height="'.$size[1].'"';
202    }
203  }
204
205  function get_size_hr()
206  {
207    $size = $this->get_size();
208    if ($size)
209    {
210      return $size[0].' x '.$size[1];
211    }
212  }
213
214}
215
216?>
Note: See TracBrowser for help on using the repository browser.