source: trunk/include/derivative.inc.php @ 13668

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

added event for src image url
simplify js in picture.tpl
action.php fix history saving

File size: 7.6 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2012 Piwigo Team                  http://piwigo.org |
6// +-----------------------------------------------------------------------+
7// | This program is free software; you can redistribute it and/or modify  |
8// | it under the terms of the GNU General Public License as published by  |
9// | the Free Software Foundation                                          |
10// |                                                                       |
11// | This program is distributed in the hope that it will be useful, but   |
12// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
13// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
14// | General Public License for more details.                              |
15// |                                                                       |
16// | You should have received a copy of the GNU General Public License     |
17// | along with this program; if not, write to the Free Software           |
18// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
19// | USA.                                                                  |
20// +-----------------------------------------------------------------------+
21
22final class SrcImage
23{
24  const IS_ORIGINAL = 0x01;
25  const IS_MIMETYPE = 0x02;
26
27  public $id;
28  public $rel_path;
29
30  private $size=null;
31  private $flags=0;
32
33  function __construct($infos)
34  {
35    global $conf;
36
37    $this->id = $infos['id'];
38    $ext = get_extension($infos['path']);
39    if (in_array($ext, $conf['picture_ext']))
40    {
41      $this->rel_path = $infos['path'];
42      $this->flags |= self::IS_ORIGINAL;
43    }
44    elseif (!empty($infos['representative_ext']))
45    {
46      $this->rel_path = original_to_representative($infos['path'], $infos['representative_ext']);
47    }
48    else
49    {
50      $ext = strtolower($ext);
51      $this->rel_path = trigger_event('get_mimetype_location', get_themeconf('mime_icon_dir').$ext.'.png', $ext );
52      $this->flags |= self::IS_MIMETYPE;
53      $this->size = @getimagesize(PHPWG_ROOT_PATH.$this->rel_path);
54    }
55
56    if (!$this->size && isset($infos['width']) && isset($infos['height']))
57    {
58      $this->size = array($infos['width'], $infos['height']);
59    }
60  }
61
62  function is_original()
63  {
64    return $this->flags & self::IS_ORIGINAL;
65  }
66
67  function is_mimetype()
68  {
69    return $this->flags & self::IS_MIMETYPE;
70  }
71
72  function get_path()
73  {
74    return PHPWG_ROOT_PATH.$this->rel_path;
75  }
76
77  function get_url()
78  {
79    $url = get_root_url().$this->rel_path;
80    if ($this->flags & self::IS_ORIGINAL)
81    {
82      $url = trigger_event('get_src_image_url', $url, $this);
83    }
84    return embellish_url($url);
85  }
86
87  function has_size()
88  {
89    return $this->size != null;
90  }
91
92  function get_size()
93  {
94    if ($this->size == null)
95      not_implemented(); // get size from file
96    return $this->size;
97  }
98}
99
100
101
102final class DerivativeImage
103{
104  public $src_image;
105
106  private $params;
107  private $rel_path, $rel_url, $is_cached=true;
108
109  function __construct($type, $src_image)
110  {
111    $this->src_image = $src_image;
112    if (is_string($type))
113    {
114      $this->params = ImageStdParams::get_by_type($type);
115    }
116    else
117    {
118      $this->params = $type;
119    }
120
121    self::build($src_image, $this->params, $this->rel_path, $this->rel_url, $this->is_cached);
122  }
123
124  static function thumb_url($infos)
125  {
126    return self::url(IMG_THUMB, $infos);
127  }
128
129  static function url($type, $infos)
130  {
131    $src_image = is_object($infos) ? $infos : new SrcImage($infos);
132    $params = is_string($type) ? ImageStdParams::get_by_type($type) : $type;
133    self::build($src_image, $params, $rel_path, $rel_url);
134    if ($params == null)
135    {
136      return $src_image->get_url();
137    }
138    return embellish_url(
139        trigger_event('get_derivative_url',
140          get_root_url().$rel_url,
141          $params, $src_image, $rel_url
142          ) );
143  }
144
145  static function get_all($src_image)
146  {
147    $ret = array();
148    foreach (ImageStdParams::get_defined_type_map() as $type => $params)
149    {
150      $derivative = new DerivativeImage($params, $src_image);
151      $ret[$type] = $derivative;
152    }
153    foreach (ImageStdParams::get_undefined_type_map() as $type => $type2)
154    {
155      $ret[$type] = $ret[$type2];
156    }
157
158    return $ret;
159  }
160
161  private static function build($src, &$params, &$rel_path, &$rel_url, &$is_cached=null)
162  {
163    if ( $src->has_size() && $params->is_identity( $src->get_size() ) )
164    {
165      // todo - what if we have a watermark maybe return a smaller size?
166      $params = null;
167      $rel_path = $rel_url = $src->rel_path;
168      return;
169    }
170
171    $tokens=array();
172    $tokens[] = substr($params->type,0,2);
173
174    if ($params->type==IMG_CUSTOM)
175    {
176      $params->add_url_tokens($tokens);
177    }
178
179    $loc = $src->rel_path;
180    if (substr_compare($loc, './', 0, 2)==0)
181    {
182      $loc = substr($loc, 2);
183    }
184    elseif (substr_compare($loc, '../', 0, 3)==0)
185    {
186      $loc = substr($loc, 3);
187    }
188    $loc = substr_replace($loc, '-'.implode('_', $tokens), strrpos($loc, '.'), 0 );
189
190    $rel_path = PWG_DERIVATIVE_DIR.$loc;
191
192    global $conf;
193    $url_style=$conf['derivative_url_style'];
194    if (!$url_style)
195    {
196      $mtime = @filemtime(PHPWG_ROOT_PATH.$rel_path);
197      if ($mtime===false or $mtime < $params->last_mod_time)
198      {
199        $is_cached = false;
200        $url_style = 2;
201      }
202      else
203      {
204        $url_style = 1;
205      }
206    }
207
208    if ($url_style == 2)
209    {
210      $rel_url = 'i';
211      if ($conf['php_extension_in_urls']) $rel_url .= '.php';
212      if ($conf['question_mark_in_urls']) $rel_url .= '?';
213      $rel_url .= '/'.$loc;
214    }
215    else
216    {
217      $rel_url = $rel_path;
218    }
219  }
220
221  function get_path()
222  {
223    return PHPWG_ROOT_PATH.$this->rel_path;
224  }
225
226  function get_url()
227  {
228    if ($this->params == null)
229    {
230      return $this->src_image->get_url();
231    }
232    return embellish_url(
233        trigger_event('get_derivative_url',
234          get_root_url().$this->rel_url,
235          $this->params, $this->src_image, $this->rel_url
236          ) );
237  }
238
239  function same_as_source()
240  {
241    return $this->params == null;
242  }
243
244
245  function get_type()
246  {
247    if ($this->params == null)
248      return 'Original';
249    return $this->params->type;
250  }
251
252  /* returns the size of the derivative image*/
253  function get_size()
254  {
255    if ($this->params == null)
256    {
257      return $this->src_image->get_size();
258    }
259    return $this->params->compute_final_size($this->src_image->get_size());
260  }
261
262  function get_size_htm()
263  {
264    $size = $this->get_size();
265    if ($size)
266    {
267      return 'width="'.$size[0].'" height="'.$size[1].'"';
268    }
269  }
270
271  function get_size_hr()
272  {
273    $size = $this->get_size();
274    if ($size)
275    {
276      return $size[0].' x '.$size[1];
277    }
278  }
279
280  function get_scaled_size($maxw, $maxh)
281  {
282    $size = $this->get_size();
283    if ($size)
284    {
285      $ratio_w = $size[0] / $maxw;
286      $ratio_h = $size[1] / $maxh;
287      if ($ratio_w>1 || $ratio_h>1)
288      {
289        if ($ratio_w > $ratio_h)
290        {
291          $size[0] = $maxw;
292          $size[1] = floor($size[1] / $ratio_w);
293        }
294        else
295        {
296          $size[0] = floor($size[0] / $ratio_h);
297          $size[1] = $maxh;
298        }
299      }
300    }
301    return $size;
302  }
303
304  function get_scaled_size_htm($maxw=9999, $maxh=9999)
305  {
306    $size = $this->get_scaled_size($maxw, $maxh);
307    if ($size)
308    {
309      return 'width="'.$size[0].'" height="'.$size[1].'"';
310    }
311  }
312
313  function is_cached()
314  {
315    return $this->is_cached;
316  }
317}
318
319?>
Note: See TracBrowser for help on using the repository browser.