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

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

feature 2548 multisize

  • added define_derivative template functiion for themes and plugins
  • code cleanup, new events ...
File size: 6.8 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  public $coi=null;
31  private $size=null;
32  private $flags=0;
33
34  function __construct($infos)
35  {
36    global $conf;
37   
38    $this->id = $infos['id'];
39    $ext = get_extension($infos['path']);
40    if (in_array($ext, $conf['picture_ext']))
41    {
42      $this->rel_path = $infos['path'];
43      $this->flags |= self::IS_ORIGINAL;
44    }
45    elseif (!empty($infos['representative_ext']))
46    {
47      $this->rel_path = original_to_representative($infos['path'], $infos['representative_ext']);
48    }
49    else
50    {
51      $ext = strtolower($ext);
52      $this->rel_path = trigger_event('get_mimetype_location', get_themeconf('mime_icon_dir').$ext.'.png', $ext );
53      $this->flags |= self::IS_MIMETYPE;
54      $this->size = @getimagesize(PHPWG_ROOT_PATH.$this->rel_path);
55    }
56
57    $this->coi = @$infos['coi'];
58    if (!$this->size && isset($infos['width']) && isset($infos['height']))
59    {
60      $this->size = array($infos['width'], $infos['height']);
61    }
62  }
63
64  function is_original()
65  {
66    return $this->flags & self::IS_ORIGINAL;
67  }
68
69  function is_mimetype()
70  {
71    return $this->flags & self::IS_MIMETYPE;
72  }
73
74  function get_path()
75  {
76    return PHPWG_ROOT_PATH.$this->rel_path;
77  }
78
79  function get_url()
80  {
81    return embellish_url(get_root_url().$this->rel_path);
82  }
83
84  function has_size()
85  {
86    return $this->size != null;
87  }
88
89  function get_size()
90  {
91    if ($this->size == null)
92      not_implemented(); // get size from file
93    return $this->size;
94  }
95}
96
97
98
99final class DerivativeImage
100{
101  public $src_image;
102
103  private $params;
104  private $rel_path, $rel_url;
105
106  function __construct($type, $src_image)
107  {
108    $this->src_image = $src_image;
109    if (is_string($type))
110    {
111      $this->params = ImageStdParams::get_by_type($type);
112    }
113    else
114    {
115      $this->params = $type;
116    }
117
118    self::build($src_image, $this->params, $this->rel_path, $this->rel_url);
119  }
120
121  static function thumb_url($infos)
122  {
123    return self::url(IMG_THUMB, $infos);
124  }
125
126  static function url($type, $infos)
127  {
128    $src_image = is_object($infos) ? $infos : new SrcImage($infos);
129    $params = is_string($type) ? ImageStdParams::get_by_type($type) : $type;
130    self::build($src_image, $params, $rel_path, $rel_url);
131    if ($params == null)
132    {
133      return $src_image->get_url();
134    }
135    return embellish_url(
136        trigger_event('get_derivative_url',
137          get_root_url().$rel_url,
138          $params, $src_image, $rel_url
139          ) );
140  }
141
142  static function get_all($src_image)
143  {
144    $ret = array();
145    foreach (ImageStdParams::get_defined_type_map() as $type => $params)
146    {
147      $derivative = new DerivativeImage($params, $src_image);
148      $ret[$type] = $derivative;
149    }
150    foreach (ImageStdParams::get_undefined_type_map() as $type => $type2)
151    {
152      $ret[$type] = $ret[$type2];
153    }
154
155    return $ret;
156  }
157
158  private static function build($src, &$params, &$rel_path, &$rel_url)
159  {
160    if ( $src->has_size() && $params->is_identity( $src->get_size() ) )
161    {
162      // todo - what if we have a watermark maybe return a smaller size?
163      $params = null;
164      $rel_path = $rel_url = $src->rel_path;
165      return;
166    }
167
168    $tokens=array();
169    $tokens[] = substr($params->type,0,2);
170
171    if ($params->sizing->max_crop != 0 and !empty($src->coi))
172    {
173      $tokens[] = 'ci'.$src->coi;
174    }
175
176    if ($params->type==IMG_CUSTOM)
177    {
178      $params->add_url_tokens($tokens);
179    }
180
181    $loc = $src->rel_path;
182    if (substr_compare($loc, './', 0, 2)==0)
183    {
184      $loc = substr($loc, 2);
185    }
186    elseif (substr_compare($loc, '../', 0, 3)==0)
187    {
188      $loc = substr($loc, 3);
189    }
190    $loc = substr_replace($loc, '-'.implode('_', $tokens), strrpos($loc, '.'), 0 );
191
192    $rel_path = PWG_DERIVATIVE_DIR.$loc;
193
194    global $conf;
195    $url_style=$conf['derivative_url_style'];
196    if (!$url_style)
197    {
198      $mtime = @filemtime(PHPWG_ROOT_PATH.$rel_path);
199      if ($mtime===false or $mtime < $params->last_mod_time)
200      {
201        $url_style = 2;
202      }
203      else
204      {
205        $url_style = 1;
206      }
207    }
208
209    if ($url_style == 2)
210    {
211      $rel_url = 'i';
212      if ($conf['php_extension_in_urls']) $rel_url .= '.php';
213      if ($conf['question_mark_in_urls']) $rel_url .= '?';
214      $rel_url .= '/'.$loc;
215    }
216    else
217    {
218      $rel_url = $rel_path;
219    }
220  }
221
222  function get_path()
223  {
224    return PHPWG_ROOT_PATH.$this->rel_path;
225  }
226
227  function get_url()
228  {
229    if ($this->params == null)
230    {
231      return $this->src_image->get_url();
232    }
233    return embellish_url(
234        trigger_event('get_derivative_url',
235          get_root_url().$this->rel_url,
236          $this->params, $this->src_image, $this->rel_url
237          ) );
238  }
239
240  function same_as_source()
241  {
242    return $this->params == null;
243  }
244
245
246  function get_type()
247  {
248    if ($this->params == null)
249      return 'original';
250    return $this->params->type;
251  }
252
253  /* returns the size of the derivative image*/
254  function get_size()
255  {
256    if ($this->params == null)
257    {
258      return $this->src_image->get_size();
259    }
260    return $this->params->compute_final_size($this->src_image->get_size(), $this->src_image->coi);
261  }
262
263  function get_size_htm()
264  {
265    $size = $this->get_size();
266    if ($size)
267    {
268      return 'width='.$size[0].' height='.$size[1];
269    }
270  }
271
272  function get_size_hr()
273  {
274    $size = $this->get_size();
275    if ($size)
276    {
277      return $size[0].' x '.$size[1];
278    }
279  }
280
281}
282
283?>
Note: See TracBrowser for help on using the repository browser.