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

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

feature 2548 multisize

  • rewrote local site sync + metadata sync
File size: 6.7 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      $this->rel_path = original_to_representative($infos['path'], $infos['representative_ext']);
46    }
47    else
48    {
49      $ext = strtolower($ext);
50      $this->rel_path = trigger_event('get_mimetype_location', get_themeconf('mime_icon_dir').$ext.'.png', $ext );
51      $this->flags |= self::IS_MIMETYPE;
52      $this->size = @getimagesize(PHPWG_ROOT_PATH.$this->rel_path);
53    }
54
55    $this->coi = @$infos['coi'];
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    return get_root_url().$this->rel_path;
80  }
81
82  function has_size()
83  {
84    return $this->size != null;
85  }
86
87  function get_size()
88  {
89    if ($this->size == null)
90      not_implemented(); // get size from file
91    return $this->size;
92  }
93}
94
95
96
97final class DerivativeImage
98{
99  const SAME_AS_SRC = 0x10;
100
101  public $src_image;
102
103  private $requested_type;
104
105  private $flags = 0;
106  private $params;
107  private $rel_path, $rel_url;
108
109  function __construct($type, $src_image)
110  {
111    $this->src_image = $src_image;
112    if (is_string($type))
113    {
114      $this->requested_type = $type;
115      $this->params = ImageStdParams::get_by_type($type);
116    }
117    else
118    {
119      $this->requested_type = IMG_CUSTOM;
120      $this->params = $type;
121    }
122
123    self::build($src_image, $this->params, $this->rel_path, $this->rel_url, $this->flags);
124  }
125
126  static function thumb_url($infos)
127  {
128    $src_image = new SrcImage($infos);
129    self::build($src_image, ImageStdParams::get_by_type(IMG_THUMB), $rel_path, $rel_url);
130    return get_root_url().$rel_url;
131  }
132
133  static function url($type, $infos)
134  {
135    $src_image = new SrcImage($infos);
136    $params = is_string($type) ? ImageStdParams::get_by_type($type) : $type;
137    self::build($src_image, $params, $rel_path, $rel_url);
138    return get_root_url().$rel_url;
139  }
140
141  static function get_all($infos)
142  {
143    $src_image = new SrcImage($infos);
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, &$flags = null)
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      $flags |= self::SAME_AS_SRC;
164      $params = null;
165      $rel_path = $rel_url = $src->rel_path;
166      return;
167    }
168
169    $tokens=array();
170    $tokens[] = substr($params->type,0,2);
171
172    if ($params->sizing->max_crop != 0 and !empty($src->coi))
173    {
174      $tokens[] = 'ci'.$src->coi;
175    }
176
177    if ($params->type==IMG_CUSTOM)
178    {
179      $params->add_url_tokens($tokens);
180    }
181
182    $loc = $src->rel_path;
183    if (substr_compare($loc, './', 0, 2)==0)
184    {
185      $loc = substr($loc, 2);
186    }
187    elseif (substr_compare($loc, '../', 0, 3)==0)
188    {
189      $loc = substr($loc, 3);
190    }
191    $loc = substr_replace($loc, '-'.implode('_', $tokens), strrpos($loc, '.'), 0 );
192
193    $rel_path = PWG_DERIVATIVE_DIR.$loc;
194
195    global $conf;
196    $url_style=$conf['derivative_url_style'];
197    if (!$url_style)
198    {
199      $mtime = @filemtime(PHPWG_ROOT_PATH.$rel_path);
200      if ($mtime===false or $mtime < $params->last_mod_time)
201      {
202        $url_style = 2;
203      }
204      else
205      {
206        $url_style = 1;
207      }
208    }
209
210    if ($url_style == 2)
211    {
212      $rel_url = 'i';
213      if ($conf['php_extension_in_urls']) $rel_url .= '.php';
214      if ($conf['question_mark_in_urls']) $rel_url .= '?';
215      $rel_url .= '/'.$loc;
216    }
217    else
218    {
219      $rel_url = $rel_path;
220    }
221  }
222
223  function get_url()
224  {
225    return get_root_url().$this->rel_url;
226  }
227
228  function same_as_source()
229  {
230    return $this->flags & self::SAME_AS_SRC;
231  }
232
233
234  function get_type()
235  {
236    if ($this->flags & self::SAME_AS_SRC)
237      return 'original';
238    return $this->params->type;
239  }
240
241  /* returns the size of the derivative image*/
242  function get_size()
243  {
244    if ($this->flags & self::SAME_AS_SRC)
245    {
246      return $this->src_image->get_size();
247    }
248    return $this->params->compute_final_size($this->src_image->get_size(), $this->src_image->coi);
249  }
250
251  function get_size_htm()
252  {
253    $size = $this->get_size();
254    if ($size)
255    {
256      return 'width="'.$size[0].'" height="'.$size[1].'"';
257    }
258  }
259
260  function get_size_hr()
261  {
262    $size = $this->get_size();
263    if ($size)
264    {
265      return $size[0].' x '.$size[1];
266    }
267  }
268
269}
270
271?>
Note: See TracBrowser for help on using the repository browser.