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

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

derivatives fix url

File size: 6.0 KB
RevLine 
[12770]1<?php
[12778]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// +-----------------------------------------------------------------------+
[12770]21
22final class SrcImage
23{
24  public $rel_path;
[12775]25 
[12770]26  public $coi=null;
27  private $size=null;
[12775]28
[12770]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    }
[12775]49
[12770]50    $this->coi = @$infos['coi'];
51    if (isset($infos['width']) && isset($infos['height']))
52    {
53      $this->size = array($infos['width'], $infos['height']);
54    }
55  }
[12775]56
57  function has_size()
58  {
59    return $this->size != null;
60  }
61
[12770]62  function get_size()
63  {
64    if ($this->size == null)
65      not_implemented(); // get size from file
66    return $this->size;
67  }
68}
69
[12775]70
71
[12770]72final class DerivativeImage
73{
[12775]74  const SAME_AS_SRC = 0x10;
75
[12770]76  public $src_image;
[12775]77
78  private $requested_type;
79
80  private $flags = 0;
[12770]81  private $params;
82  private $rel_path, $rel_url;
[12775]83
84  function __construct($type, $src_image)
[12770]85  {
[12775]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);
[12770]99  }
[12775]100
[12770]101  static function thumb_url($infos)
102  {
103    $src_image = new SrcImage($infos);
[12775]104    self::build($src_image, ImageStdParams::get_by_type(IMG_THUMB), $rel_path, $rel_url);
[12770]105    return get_root_url().$rel_url;
106  }
107
108  static function url($type, $infos)
109  {
110    $src_image = new SrcImage($infos);
[12775]111    $params = is_string($type) ? ImageStdParams::get_by_type($type) : $type;
112    self::build($src_image, $params, $rel_path, $rel_url);
[12770]113    return get_root_url().$rel_url;
114  }
[12775]115
116  static function get_all($infos)
[12770]117  {
[12775]118    $src_image = new SrcImage($infos);
119    $ret = array();
120    foreach (ImageStdParams::get_defined_type_map() as $type => $params)
[12770]121    {
[12775]122      $derivative = new DerivativeImage($params, $src_image);
123      $ret[$type] = $derivative;
[12770]124    }
[12775]125    foreach (ImageStdParams::get_undefined_type_map() as $type => $type2)
[12770]126    {
[12775]127      $ret[$type] = $ret[$type2];
[12770]128    }
129   
[12775]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
[12770]144    $tokens=array();
[12775]145    $tokens[] = substr($params->type,0,2);
146
[12770]147    if (!empty($src->coi))
148    {
149      $tokens[] = 'ci'.$src->coi;
150    }
[12775]151
152    if ($params->type==IMG_CUSTOM)
[12770]153    {
154      $params->add_url_tokens($tokens);
155    }
[12775]156
[12770]157    $loc = $src->rel_path;
[12784]158    if (substr_compare($loc, './', 0, 2)==0)
[12770]159    {
[12784]160      $loc = substr($loc, 2);
161    }
162    elseif (substr_compare($loc, '../', 0, 3)==0)
163    {
[12770]164      $loc = substr($loc, 3);
165    }
166    $loc = substr_replace($loc, '-'.implode('_', $tokens), strrpos($loc, '.'), 0 );
167
168    $rel_path = PWG_DERIVATIVE_DIR.$loc;
[12775]169
[12770]170    global $conf;
171    $url_style=$conf['derivative_url_style'];
172    if (!$url_style)
173    {
174      $mtime = @filemtime(PHPWG_ROOT_PATH.$rel_path);
[12778]175      if ($mtime===false or $mtime < $params->last_mod_time)
[12770]176      {
177        $url_style = 2;
178      }
179      else
180      {
181        $url_style = 1;
182      }
183    }
[12775]184
[12770]185    if ($url_style == 2)
186    {
187      $rel_url = 'i';
188      if ($conf['php_extension_in_urls']) $rel_url .= '.php';
[12779]189      if ($conf['question_mark_in_urls']) $rel_url .= '?';
190      $rel_url .= '/'.$loc;
[12770]191    }
192    else
193    {
194      $rel_url = $rel_path;
195    }
196  }
197
[12775]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
[12770]209  /* returns the size of the derivative image*/
210  function get_size()
211  {
[12775]212    if ($this->flags & self::SAME_AS_SRC)
213    {
214      return $this->src_image->get_size();
215    }
[12770]216    return $this->params->compute_final_size($this->src_image->get_size(), $this->src_image->coi);
217  }
[12775]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
[12770]237}
238
239?>
Note: See TracBrowser for help on using the repository browser.