aboutsummaryrefslogtreecommitdiffstats
path: root/include/derivative.inc.php
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--include/derivative.inc.php239
1 files changed, 239 insertions, 0 deletions
diff --git a/include/derivative.inc.php b/include/derivative.inc.php
new file mode 100644
index 000000000..11f1ebb76
--- /dev/null
+++ b/include/derivative.inc.php
@@ -0,0 +1,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];
+ }
+ }
+
+}
+
+?> \ No newline at end of file