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

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

feature 2541 multisize

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