source: extensions/derivatives/include/derivative.inc.php @ 12779

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

derivatives

  • fix url generation
File size: 6.0 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  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, 3)==0)
159    {
160      $loc = substr($loc, 3);
161    }
162    $loc = substr_replace($loc, '-'.implode('_', $tokens), strrpos($loc, '.'), 0 );
163
164    $rel_path = PWG_DERIVATIVE_DIR.$loc;
165
166    global $conf;
167    $url_style=$conf['derivative_url_style'];
168    if (!$url_style)
169    {
170      $mtime = @filemtime(PHPWG_ROOT_PATH.$rel_path);
171      if ($mtime===false or $mtime < $params->last_mod_time)
172      {
173        $url_style = 2;
174      }
175      else
176      {
177        $url_style = 1;
178      }
179    }
180
181    if ($url_style == 2)
182    {
183      $rel_url = 'i';
184      if ($conf['php_extension_in_urls']) $rel_url .= '.php';
185      if ($conf['question_mark_in_urls']) $rel_url .= '?';
186      $rel_url .= '/'.$loc;
187    }
188    else
189    {
190      $rel_url = $rel_path;
191    }
192  }
193
194  function get_url()
195  {
196    return get_root_url().$this->rel_url;
197  }
198
199  function same_as_source()
200  {
201    return $this->flags & self::SAME_AS_SRC;
202  }
203
204
205  /* returns the size of the derivative image*/
206  function get_size()
207  {
208    if ($this->flags & self::SAME_AS_SRC)
209    {
210      return $this->src_image->get_size();
211    }
212    return $this->params->compute_final_size($this->src_image->get_size(), $this->src_image->coi);
213  }
214
215  function get_size_htm()
216  {
217    $size = $this->get_size();
218    if ($size)
219    {
220      return 'width="'.$size[0].'" height="'.$size[1].'"';
221    }
222  }
223
224  function get_size_hr()
225  {
226    $size = $this->get_size();
227    if ($size)
228    {
229      return $size[0].' x '.$size[1];
230    }
231  }
232
233}
234
235?>
Note: See TracBrowser for help on using the repository browser.