[12796] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
| 3 | // | Piwigo - a PHP based photo gallery | |
---|
| 4 | // +-----------------------------------------------------------------------+ |
---|
[19703] | 5 | // | Copyright(C) 2008-2013 Piwigo Team http://piwigo.org | |
---|
[12796] | 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 | |
---|
[20516] | 22 | /*A source image is used to get a derivative image. A source image is either the original file for a jpg or a |
---|
[19878] | 23 | 'representative' image of a non image file or a standard icon for the non-image file.*/ |
---|
[12796] | 24 | final class SrcImage |
---|
| 25 | { |
---|
[12797] | 26 | const IS_ORIGINAL = 0x01; |
---|
| 27 | const IS_MIMETYPE = 0x02; |
---|
[14735] | 28 | const DIM_NOT_GIVEN = 0x04; |
---|
[12797] | 29 | |
---|
[12954] | 30 | public $id; |
---|
[12796] | 31 | public $rel_path; |
---|
[14143] | 32 | public $rotation = 0; |
---|
[12797] | 33 | |
---|
[12796] | 34 | private $size=null; |
---|
[12797] | 35 | private $flags=0; |
---|
[12796] | 36 | |
---|
[19878] | 37 | /*@param infos assoc array of data from images table*/ |
---|
[12796] | 38 | function __construct($infos) |
---|
| 39 | { |
---|
| 40 | global $conf; |
---|
[13038] | 41 | |
---|
[12954] | 42 | $this->id = $infos['id']; |
---|
[12796] | 43 | $ext = get_extension($infos['path']); |
---|
| 44 | if (in_array($ext, $conf['picture_ext'])) |
---|
| 45 | { |
---|
| 46 | $this->rel_path = $infos['path']; |
---|
[12797] | 47 | $this->flags |= self::IS_ORIGINAL; |
---|
[12796] | 48 | } |
---|
| 49 | elseif (!empty($infos['representative_ext'])) |
---|
| 50 | { |
---|
[12831] | 51 | $this->rel_path = original_to_representative($infos['path'], $infos['representative_ext']); |
---|
[12796] | 52 | } |
---|
| 53 | else |
---|
| 54 | { |
---|
[12797] | 55 | $ext = strtolower($ext); |
---|
| 56 | $this->rel_path = trigger_event('get_mimetype_location', get_themeconf('mime_icon_dir').$ext.'.png', $ext ); |
---|
| 57 | $this->flags |= self::IS_MIMETYPE; |
---|
[14735] | 58 | if ( ($size=@getimagesize(PHPWG_ROOT_PATH.$this->rel_path)) === false) |
---|
| 59 | { |
---|
[15431] | 60 | $this->rel_path = 'themes/default/icon/mimetypes/unknown.png'; |
---|
[14735] | 61 | $size = getimagesize(PHPWG_ROOT_PATH.$this->rel_path); |
---|
| 62 | } |
---|
| 63 | $this->size = array($size[0],$size[1]); |
---|
[12796] | 64 | } |
---|
| 65 | |
---|
[14735] | 66 | if (!$this->size) |
---|
[12796] | 67 | { |
---|
[14735] | 68 | if (isset($infos['width']) && isset($infos['height'])) |
---|
| 69 | { |
---|
| 70 | $width = $infos['width']; |
---|
| 71 | $height = $infos['height']; |
---|
[13843] | 72 | |
---|
[14735] | 73 | $this->rotation = intval($infos['rotation']) % 4; |
---|
| 74 | // 1 or 5 => 90 clockwise |
---|
| 75 | // 3 or 7 => 270 clockwise |
---|
| 76 | if ($this->rotation % 2) |
---|
| 77 | { |
---|
| 78 | $width = $infos['height']; |
---|
| 79 | $height = $infos['width']; |
---|
| 80 | } |
---|
[20516] | 81 | |
---|
[14735] | 82 | $this->size = array($width, $height); |
---|
| 83 | } |
---|
| 84 | elseif (!array_key_exists('width', $infos)) |
---|
[13843] | 85 | { |
---|
[14735] | 86 | $this->flags |= self::DIM_NOT_GIVEN; |
---|
[13843] | 87 | } |
---|
[12796] | 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
[12797] | 91 | function is_original() |
---|
| 92 | { |
---|
| 93 | return $this->flags & self::IS_ORIGINAL; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | function is_mimetype() |
---|
| 97 | { |
---|
| 98 | return $this->flags & self::IS_MIMETYPE; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | function get_path() |
---|
| 102 | { |
---|
| 103 | return PHPWG_ROOT_PATH.$this->rel_path; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | function get_url() |
---|
| 107 | { |
---|
[13489] | 108 | $url = get_root_url().$this->rel_path; |
---|
[20516] | 109 | if ( !($this->flags & self::IS_MIMETYPE) ) |
---|
[13489] | 110 | { |
---|
| 111 | $url = trigger_event('get_src_image_url', $url, $this); |
---|
| 112 | } |
---|
| 113 | return embellish_url($url); |
---|
[12797] | 114 | } |
---|
| 115 | |
---|
[12796] | 116 | function has_size() |
---|
| 117 | { |
---|
| 118 | return $this->size != null; |
---|
| 119 | } |
---|
| 120 | |
---|
[19878] | 121 | /* @return a 2-element array containing width/height or null if dimensions are not available*/ |
---|
[12796] | 122 | function get_size() |
---|
| 123 | { |
---|
| 124 | if ($this->size == null) |
---|
[14735] | 125 | { |
---|
| 126 | if ($this->flags & self::DIM_NOT_GIVEN) |
---|
| 127 | fatal_error('SrcImage dimensions required but not provided'); |
---|
| 128 | // probably not metadata synced |
---|
| 129 | if ( ($size = getimagesize( $this->get_path() )) !== false) |
---|
| 130 | { |
---|
| 131 | $this->size = array($size[0],$size[1]); |
---|
| 132 | pwg_query('UPDATE '.IMAGES_TABLE.' SET width='.$size[0].', height='.$size[1].' WHERE id='.$this->id); |
---|
| 133 | } |
---|
| 134 | } |
---|
[12796] | 135 | return $this->size; |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | |
---|
[19878] | 140 | /*Holds information (path, url, dimensions) about a derivative image. A derivative image is constructed from a source |
---|
| 141 | image (SrcImage class) and derivative parameters (DerivativeParams class). |
---|
| 142 | */ |
---|
[12796] | 143 | final class DerivativeImage |
---|
| 144 | { |
---|
| 145 | public $src_image; |
---|
| 146 | |
---|
| 147 | private $params; |
---|
[13452] | 148 | private $rel_path, $rel_url, $is_cached=true; |
---|
[12796] | 149 | |
---|
[19878] | 150 | /* |
---|
| 151 | @param type string of standard derivative param type (e.g. IMG_???) or a DerivativeParams object |
---|
| 152 | @param src_image the source image of this derivative*/ |
---|
| 153 | function __construct($type, SrcImage $src_image) |
---|
[12796] | 154 | { |
---|
| 155 | $this->src_image = $src_image; |
---|
| 156 | if (is_string($type)) |
---|
| 157 | { |
---|
| 158 | $this->params = ImageStdParams::get_by_type($type); |
---|
| 159 | } |
---|
| 160 | else |
---|
| 161 | { |
---|
| 162 | $this->params = $type; |
---|
| 163 | } |
---|
| 164 | |
---|
[13444] | 165 | self::build($src_image, $this->params, $this->rel_path, $this->rel_url, $this->is_cached); |
---|
[12796] | 166 | } |
---|
| 167 | |
---|
| 168 | static function thumb_url($infos) |
---|
| 169 | { |
---|
[12908] | 170 | return self::url(IMG_THUMB, $infos); |
---|
[12796] | 171 | } |
---|
| 172 | |
---|
[20516] | 173 | /** |
---|
[19878] | 174 | @return derivative image url |
---|
| 175 | @param type string of standard derivative param type (e.g. IMG_???) or a DerivativeParams object |
---|
| 176 | @param infos assoc array of data from images table or a SrcImage object |
---|
| 177 | */ |
---|
[12796] | 178 | static function url($type, $infos) |
---|
| 179 | { |
---|
[12908] | 180 | $src_image = is_object($infos) ? $infos : new SrcImage($infos); |
---|
[12796] | 181 | $params = is_string($type) ? ImageStdParams::get_by_type($type) : $type; |
---|
| 182 | self::build($src_image, $params, $rel_path, $rel_url); |
---|
[12954] | 183 | if ($params == null) |
---|
| 184 | { |
---|
| 185 | return $src_image->get_url(); |
---|
| 186 | } |
---|
| 187 | return embellish_url( |
---|
| 188 | trigger_event('get_derivative_url', |
---|
| 189 | get_root_url().$rel_url, |
---|
| 190 | $params, $src_image, $rel_url |
---|
| 191 | ) ); |
---|
[12796] | 192 | } |
---|
| 193 | |
---|
[19878] | 194 | /** |
---|
| 195 | @return an associative array of derivative images with keys all standard derivative image types: |
---|
| 196 | Disabled derivative types can be still found in the return mapped to an enabled derivative (e.g. the values are not |
---|
| 197 | unique in the return array). This is useful for any plugin/theme to just use $deriv[IMG_XLARGE] even if the XLARGE is |
---|
| 198 | disabled. |
---|
| 199 | */ |
---|
[12855] | 200 | static function get_all($src_image) |
---|
[12796] | 201 | { |
---|
| 202 | $ret = array(); |
---|
[19878] | 203 | // build enabled types |
---|
[12796] | 204 | foreach (ImageStdParams::get_defined_type_map() as $type => $params) |
---|
| 205 | { |
---|
| 206 | $derivative = new DerivativeImage($params, $src_image); |
---|
| 207 | $ret[$type] = $derivative; |
---|
| 208 | } |
---|
[19878] | 209 | // disabled types fqllbqck to enqbled types |
---|
[12796] | 210 | foreach (ImageStdParams::get_undefined_type_map() as $type => $type2) |
---|
| 211 | { |
---|
| 212 | $ret[$type] = $ret[$type2]; |
---|
| 213 | } |
---|
[12797] | 214 | |
---|
[12796] | 215 | return $ret; |
---|
| 216 | } |
---|
| 217 | |
---|
[13444] | 218 | private static function build($src, &$params, &$rel_path, &$rel_url, &$is_cached=null) |
---|
[12796] | 219 | { |
---|
| 220 | if ( $src->has_size() && $params->is_identity( $src->get_size() ) ) |
---|
[19878] | 221 | {// the source image is smaller than what we should do - we do not upsample |
---|
[16989] | 222 | if (!$params->will_watermark($src->get_size()) && !$src->rotation) |
---|
[19878] | 223 | {// no watermark, no rotation required -> we will use the source image |
---|
[14143] | 224 | $params = null; |
---|
| 225 | $rel_path = $rel_url = $src->rel_path; |
---|
| 226 | return; |
---|
| 227 | } |
---|
| 228 | $defined_types = array_keys(ImageStdParams::get_defined_type_map()); |
---|
| 229 | for ($i=0; $i<count($defined_types); $i++) |
---|
| 230 | { |
---|
| 231 | if ($defined_types[$i] == $params->type) |
---|
| 232 | { |
---|
| 233 | for ($i--; $i>=0; $i--) |
---|
| 234 | { |
---|
| 235 | $smaller = ImageStdParams::get_by_type($defined_types[$i]); |
---|
| 236 | if ($smaller->sizing->max_crop==$params->sizing->max_crop && $smaller->is_identity( $src->get_size() )) |
---|
| 237 | { |
---|
| 238 | $params = $smaller; |
---|
| 239 | self::build($src, $params, $rel_path, $rel_url, $is_cached); |
---|
| 240 | return; |
---|
| 241 | } |
---|
| 242 | } |
---|
| 243 | break; |
---|
| 244 | } |
---|
| 245 | } |
---|
[12796] | 246 | } |
---|
| 247 | |
---|
| 248 | $tokens=array(); |
---|
| 249 | $tokens[] = substr($params->type,0,2); |
---|
| 250 | |
---|
| 251 | if ($params->type==IMG_CUSTOM) |
---|
| 252 | { |
---|
| 253 | $params->add_url_tokens($tokens); |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | $loc = $src->rel_path; |
---|
| 257 | if (substr_compare($loc, './', 0, 2)==0) |
---|
| 258 | { |
---|
| 259 | $loc = substr($loc, 2); |
---|
| 260 | } |
---|
| 261 | elseif (substr_compare($loc, '../', 0, 3)==0) |
---|
| 262 | { |
---|
| 263 | $loc = substr($loc, 3); |
---|
| 264 | } |
---|
| 265 | $loc = substr_replace($loc, '-'.implode('_', $tokens), strrpos($loc, '.'), 0 ); |
---|
| 266 | |
---|
| 267 | $rel_path = PWG_DERIVATIVE_DIR.$loc; |
---|
| 268 | |
---|
| 269 | global $conf; |
---|
| 270 | $url_style=$conf['derivative_url_style']; |
---|
| 271 | if (!$url_style) |
---|
| 272 | { |
---|
| 273 | $mtime = @filemtime(PHPWG_ROOT_PATH.$rel_path); |
---|
| 274 | if ($mtime===false or $mtime < $params->last_mod_time) |
---|
| 275 | { |
---|
[13444] | 276 | $is_cached = false; |
---|
[12796] | 277 | $url_style = 2; |
---|
| 278 | } |
---|
| 279 | else |
---|
| 280 | { |
---|
| 281 | $url_style = 1; |
---|
| 282 | } |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | if ($url_style == 2) |
---|
| 286 | { |
---|
| 287 | $rel_url = 'i'; |
---|
| 288 | if ($conf['php_extension_in_urls']) $rel_url .= '.php'; |
---|
| 289 | if ($conf['question_mark_in_urls']) $rel_url .= '?'; |
---|
| 290 | $rel_url .= '/'.$loc; |
---|
| 291 | } |
---|
| 292 | else |
---|
| 293 | { |
---|
| 294 | $rel_url = $rel_path; |
---|
| 295 | } |
---|
| 296 | } |
---|
| 297 | |
---|
[12865] | 298 | function get_path() |
---|
| 299 | { |
---|
| 300 | return PHPWG_ROOT_PATH.$this->rel_path; |
---|
| 301 | } |
---|
| 302 | |
---|
[12796] | 303 | function get_url() |
---|
| 304 | { |
---|
[12954] | 305 | if ($this->params == null) |
---|
| 306 | { |
---|
| 307 | return $this->src_image->get_url(); |
---|
| 308 | } |
---|
| 309 | return embellish_url( |
---|
| 310 | trigger_event('get_derivative_url', |
---|
| 311 | get_root_url().$this->rel_url, |
---|
| 312 | $this->params, $this->src_image, $this->rel_url |
---|
| 313 | ) ); |
---|
[12796] | 314 | } |
---|
| 315 | |
---|
| 316 | function same_as_source() |
---|
| 317 | { |
---|
[12954] | 318 | return $this->params == null; |
---|
[12796] | 319 | } |
---|
| 320 | |
---|
| 321 | |
---|
[12797] | 322 | function get_type() |
---|
| 323 | { |
---|
[12954] | 324 | if ($this->params == null) |
---|
[13068] | 325 | return 'Original'; |
---|
[12797] | 326 | return $this->params->type; |
---|
| 327 | } |
---|
| 328 | |
---|
[12796] | 329 | /* returns the size of the derivative image*/ |
---|
| 330 | function get_size() |
---|
| 331 | { |
---|
[12954] | 332 | if ($this->params == null) |
---|
[12796] | 333 | { |
---|
| 334 | return $this->src_image->get_size(); |
---|
| 335 | } |
---|
[13038] | 336 | return $this->params->compute_final_size($this->src_image->get_size()); |
---|
[12796] | 337 | } |
---|
| 338 | |
---|
[16508] | 339 | function get_size_css() |
---|
| 340 | { |
---|
| 341 | $size = $this->get_size(); |
---|
| 342 | if ($size) |
---|
| 343 | { |
---|
| 344 | return 'width:'.$size[0].'px; height:'.$size[1].'px'; |
---|
| 345 | } |
---|
| 346 | } |
---|
| 347 | |
---|
[12796] | 348 | function get_size_htm() |
---|
| 349 | { |
---|
| 350 | $size = $this->get_size(); |
---|
| 351 | if ($size) |
---|
| 352 | { |
---|
[13170] | 353 | return 'width="'.$size[0].'" height="'.$size[1].'"'; |
---|
[12796] | 354 | } |
---|
| 355 | } |
---|
| 356 | |
---|
| 357 | function get_size_hr() |
---|
| 358 | { |
---|
| 359 | $size = $this->get_size(); |
---|
| 360 | if ($size) |
---|
| 361 | { |
---|
| 362 | return $size[0].' x '.$size[1]; |
---|
| 363 | } |
---|
| 364 | } |
---|
| 365 | |
---|
[13252] | 366 | function get_scaled_size($maxw, $maxh) |
---|
| 367 | { |
---|
| 368 | $size = $this->get_size(); |
---|
| 369 | if ($size) |
---|
| 370 | { |
---|
| 371 | $ratio_w = $size[0] / $maxw; |
---|
| 372 | $ratio_h = $size[1] / $maxh; |
---|
| 373 | if ($ratio_w>1 || $ratio_h>1) |
---|
| 374 | { |
---|
| 375 | if ($ratio_w > $ratio_h) |
---|
| 376 | { |
---|
| 377 | $size[0] = $maxw; |
---|
| 378 | $size[1] = floor($size[1] / $ratio_w); |
---|
| 379 | } |
---|
| 380 | else |
---|
| 381 | { |
---|
| 382 | $size[0] = floor($size[0] / $ratio_h); |
---|
| 383 | $size[1] = $maxh; |
---|
| 384 | } |
---|
| 385 | } |
---|
| 386 | } |
---|
| 387 | return $size; |
---|
| 388 | } |
---|
| 389 | |
---|
| 390 | function get_scaled_size_htm($maxw=9999, $maxh=9999) |
---|
| 391 | { |
---|
| 392 | $size = $this->get_scaled_size($maxw, $maxh); |
---|
| 393 | if ($size) |
---|
| 394 | { |
---|
| 395 | return 'width="'.$size[0].'" height="'.$size[1].'"'; |
---|
| 396 | } |
---|
| 397 | } |
---|
[13444] | 398 | |
---|
| 399 | function is_cached() |
---|
| 400 | { |
---|
| 401 | return $this->is_cached; |
---|
| 402 | } |
---|
[12796] | 403 | } |
---|
| 404 | |
---|
| 405 | ?> |
---|