aboutsummaryrefslogtreecommitdiffstats
path: root/include/derivative.inc.php
blob: 11f1ebb7669a27a135ca976cedf460698a033204 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
// +-----------------------------------------------------------------------+
// | Piwigo - a PHP based photo gallery                                    |
// +-----------------------------------------------------------------------+
// | Copyright(C) 2008-2012 Piwigo Team                  http://piwigo.org |
// +-----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify  |
// | it under the terms of the GNU General Public License as published by  |
// | the Free Software Foundation                                          |
// |                                                                       |
// | This program is distributed in the hope that it will be useful, but   |
// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
// | General Public License for more details.                              |
// |                                                                       |
// | You should have received a copy of the GNU General Public License     |
// | along with this program; if not, write to the Free Software           |
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
// | USA.                                                                  |
// +-----------------------------------------------------------------------+

final class SrcImage
{
  public $rel_path;
  
  public $coi=null;
  private $size=null;

  function __construct($infos)
  {
    global $conf;

    $ext = get_extension($infos['path']);
    if (in_array($ext, $conf['picture_ext']))
    {
      $this->rel_path = $infos['path'];
    }
    elseif (!empty($infos['representative_ext']))
    {
      $pi = pathinfo($infos['path']);
      $file_wo_ext = get_filename_wo_extension($pi['basename']);
      $this->rel_path = $pi['dirname'].'/pwg_representative/'
        .$file_wo_ext.'.'.$infos['representative_ext'];
    }
    else
    {
      $this->rel_path = get_themeconf('mime_icon_dir').strtolower($ext).'.png';
    }

    $this->coi = @$infos['coi'];
    if (isset($infos['width']) && isset($infos['height']))
    {
      $this->size = array($infos['width'], $infos['height']);
    }
  }

  function has_size()
  {
    return $this->size != null;
  }

  function get_size()
  {
    if ($this->size == null)
      not_implemented(); // get size from file
    return $this->size;
  }
}



final class DerivativeImage
{
  const SAME_AS_SRC = 0x10;

  public $src_image;

  private $requested_type;

  private $flags = 0;
  private $params;
  private $rel_path, $rel_url;

  function __construct($type, $src_image)
  {
    $this->src_image = $src_image;
    if (is_string($type))
    {
      $this->requested_type = $type;
      $this->params = ImageStdParams::get_by_type($type);
    }
    else
    {
      $this->requested_type = IMG_CUSTOM;
      $this->params = $type;
    }

    self::build($src_image, $this->params, $this->rel_path, $this->rel_url, $this->flags);
  }

  static function thumb_url($infos)
  {
    $src_image = new SrcImage($infos);
    self::build($src_image, ImageStdParams::get_by_type(IMG_THUMB), $rel_path, $rel_url);
    return get_root_url().$rel_url;
  }

  static function url($type, $infos)
  {
    $src_image = new SrcImage($infos);
    $params = is_string($type) ? ImageStdParams::get_by_type($type) : $type;
    self::build($src_image, $params, $rel_path, $rel_url);
    return get_root_url().$rel_url;
  }

  static function get_all($infos)
  {
    $src_image = new SrcImage($infos);
    $ret = array();
    foreach (ImageStdParams::get_defined_type_map() as $type => $params)
    {
      $derivative = new DerivativeImage($params, $src_image);
      $ret[$type] = $derivative;
    }
    foreach (ImageStdParams::get_undefined_type_map() as $type => $type2)
    {
      $ret[$type] = $ret[$type2];
    }
    
    return $ret;
  }

  private static function build($src, &$params, &$rel_path, &$rel_url, &$flags = null)
  {
    if ( $src->has_size() && $params->is_identity( $src->get_size() ) )
    {
      // todo - what if we have a watermark maybe return a smaller size?
      $flags |= self::SAME_AS_SRC;
      $params = null;
      $rel_path = $rel_url = $src->rel_path;
      return;
    }

    $tokens=array();
    $tokens[] = substr($params->type,0,2);

    if (!empty($src->coi))
    {
      $tokens[] = 'ci'.$src->coi;
    }

    if ($params->type==IMG_CUSTOM)
    {
      $params->add_url_tokens($tokens);
    }

    $loc = $src->rel_path;
    if (substr_compare($loc, './', 0, 2)==0)
    {
      $loc = substr($loc, 2);
    }
    elseif (substr_compare($loc, '../', 0, 3)==0)
    {
      $loc = substr($loc, 3);
    }
    $loc = substr_replace($loc, '-'.implode('_', $tokens), strrpos($loc, '.'), 0 );

    $rel_path = PWG_DERIVATIVE_DIR.$loc;

    global $conf;
    $url_style=$conf['derivative_url_style'];
    if (!$url_style)
    {
      $mtime = @filemtime(PHPWG_ROOT_PATH.$rel_path);
      if ($mtime===false or $mtime < $params->last_mod_time)
      {
        $url_style = 2;
      }
      else
      {
        $url_style = 1;
      }
    }

    if ($url_style == 2)
    {
      $rel_url = 'i';
      if ($conf['php_extension_in_urls']) $rel_url .= '.php';
      if ($conf['question_mark_in_urls']) $rel_url .= '?';
      $rel_url .= '/'.$loc;
    }
    else
    {
      $rel_url = $rel_path;
    }
  }

  function get_url()
  {
    return get_root_url().$this->rel_url;
  }

  function same_as_source()
  {
    return $this->flags & self::SAME_AS_SRC;
  }


  /* returns the size of the derivative image*/
  function get_size()
  {
    if ($this->flags & self::SAME_AS_SRC)
    {
      return $this->src_image->get_size();
    }
    return $this->params->compute_final_size($this->src_image->get_size(), $this->src_image->coi);
  }

  function get_size_htm()
  {
    $size = $this->get_size();
    if ($size)
    {
      return 'width="'.$size[0].'" height="'.$size[1].'"';
    }
  }

  function get_size_hr()
  {
    $size = $this->get_size();
    if ($size)
    {
      return $size[0].' x '.$size[1];
    }
  }

}

?>