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

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

feature 2541 multisize

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