[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 | |
---|
| 22 | define('IMG_SQUARE', 'square'); |
---|
| 23 | define('IMG_THUMB', 'thumb'); |
---|
[13683] | 24 | define('IMG_XXSMALL', '2small'); |
---|
| 25 | define('IMG_XSMALL', 'xsmall'); |
---|
[12796] | 26 | define('IMG_SMALL', 'small'); |
---|
| 27 | define('IMG_MEDIUM', 'medium'); |
---|
| 28 | define('IMG_LARGE', 'large'); |
---|
| 29 | define('IMG_XLARGE', 'xlarge'); |
---|
| 30 | define('IMG_XXLARGE', 'xxlarge'); |
---|
| 31 | define('IMG_CUSTOM', 'custom'); |
---|
| 32 | |
---|
[12851] | 33 | final class WatermarkParams |
---|
| 34 | { |
---|
| 35 | public $file = ''; |
---|
| 36 | public $min_size = array(500,500); |
---|
| 37 | public $xpos = 50; |
---|
| 38 | public $ypos = 50; |
---|
| 39 | public $xrepeat = 0; |
---|
| 40 | public $opacity = 100; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | |
---|
[12796] | 44 | final class ImageStdParams |
---|
| 45 | { |
---|
[13683] | 46 | private static $all_types = array( |
---|
| 47 | IMG_SQUARE,IMG_THUMB,IMG_XXSMALL,IMG_XSMALL,IMG_SMALL,IMG_MEDIUM,IMG_LARGE,IMG_XLARGE,IMG_XXLARGE |
---|
| 48 | ); |
---|
[12796] | 49 | private static $all_type_map = array(); |
---|
| 50 | private static $type_map = array(); |
---|
| 51 | private static $undefined_type_map = array(); |
---|
[12851] | 52 | private static $watermark; |
---|
[13021] | 53 | public static $custom = array(); |
---|
[14649] | 54 | public static $quality=95; |
---|
[12820] | 55 | |
---|
[12796] | 56 | static function get_all_types() |
---|
| 57 | { |
---|
| 58 | return self::$all_types; |
---|
| 59 | } |
---|
[12820] | 60 | |
---|
[12796] | 61 | static function get_all_type_map() |
---|
| 62 | { |
---|
| 63 | return self::$all_type_map; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | static function get_defined_type_map() |
---|
| 67 | { |
---|
| 68 | return self::$type_map; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | static function get_undefined_type_map() |
---|
| 72 | { |
---|
| 73 | return self::$undefined_type_map; |
---|
| 74 | } |
---|
[12820] | 75 | |
---|
[12796] | 76 | static function get_by_type($type) |
---|
| 77 | { |
---|
| 78 | return self::$all_type_map[$type]; |
---|
| 79 | } |
---|
[13038] | 80 | |
---|
[13021] | 81 | static function get_custom($w, $h, $crop=0, $minw=null, $minh=null) |
---|
| 82 | { |
---|
| 83 | $params = new DerivativeParams( new SizingParams( array($w,$h), $crop, array($minw,$minh)) ); |
---|
| 84 | self::apply_global($params); |
---|
[12820] | 85 | |
---|
[13021] | 86 | $key = array(); |
---|
| 87 | $params->add_url_tokens($key); |
---|
| 88 | $key = implode('_',$key); |
---|
| 89 | if ( @self::$custom[$key] < time() - 24*3600) |
---|
| 90 | { |
---|
| 91 | self::$custom[$key] = time(); |
---|
| 92 | self::save(); |
---|
| 93 | } |
---|
| 94 | return $params; |
---|
| 95 | } |
---|
| 96 | |
---|
[12851] | 97 | static function get_watermark() |
---|
| 98 | { |
---|
| 99 | return self::$watermark; |
---|
| 100 | } |
---|
| 101 | |
---|
[12796] | 102 | static function load_from_db() |
---|
| 103 | { |
---|
[12820] | 104 | global $conf; |
---|
| 105 | $arr = @unserialize($conf['derivatives']); |
---|
| 106 | if (false!==$arr) |
---|
| 107 | { |
---|
| 108 | self::$type_map = $arr['d']; |
---|
[12851] | 109 | self::$watermark = @$arr['w']; |
---|
| 110 | if (!self::$watermark) self::$watermark = new WatermarkParams(); |
---|
[13038] | 111 | self::$custom = @$arr['c']; |
---|
| 112 | if (!self::$custom) self::$custom = array(); |
---|
[14649] | 113 | if (isset($arr['q'])) self::$quality = $arr['q']; |
---|
[12820] | 114 | } |
---|
| 115 | else |
---|
| 116 | { |
---|
[14228] | 117 | self::$watermark = new WatermarkParams(); |
---|
| 118 | self::$type_map = self::get_default_sizes(); |
---|
[14649] | 119 | self::save(); |
---|
[12820] | 120 | } |
---|
[12796] | 121 | self::build_maps(); |
---|
| 122 | } |
---|
| 123 | |
---|
[12851] | 124 | static function set_watermark($watermark) |
---|
| 125 | { |
---|
| 126 | self::$watermark = $watermark; |
---|
| 127 | } |
---|
[13038] | 128 | |
---|
[12820] | 129 | static function set_and_save($map) |
---|
| 130 | { |
---|
| 131 | self::$type_map = $map; |
---|
[13021] | 132 | self::save(); |
---|
| 133 | self::build_maps(); |
---|
| 134 | } |
---|
[12820] | 135 | |
---|
[13021] | 136 | static function save() |
---|
| 137 | { |
---|
| 138 | global $conf; |
---|
| 139 | |
---|
[12820] | 140 | $ser = serialize( array( |
---|
[12851] | 141 | 'd' => self::$type_map, |
---|
[14649] | 142 | 'q' => self::$quality, |
---|
[12851] | 143 | 'w' => self::$watermark, |
---|
[13021] | 144 | 'c' => self::$custom, |
---|
[12820] | 145 | ) ); |
---|
| 146 | conf_update_param('derivatives', addslashes($ser) ); |
---|
| 147 | } |
---|
| 148 | |
---|
[14228] | 149 | static function get_default_sizes() |
---|
[12796] | 150 | { |
---|
[14228] | 151 | $arr = array( |
---|
| 152 | IMG_SQUARE => new DerivativeParams( SizingParams::square(120,120) ), |
---|
| 153 | IMG_THUMB => new DerivativeParams( SizingParams::classic(144,144) ), |
---|
| 154 | IMG_XXSMALL => new DerivativeParams( SizingParams::classic(240,240) ), |
---|
| 155 | IMG_XSMALL => new DerivativeParams( SizingParams::classic(432,324) ), |
---|
| 156 | IMG_SMALL => new DerivativeParams( SizingParams::classic(576,432) ), |
---|
| 157 | IMG_MEDIUM => new DerivativeParams( SizingParams::classic(792,594) ), |
---|
| 158 | IMG_LARGE => new DerivativeParams( SizingParams::classic(1008,756) ), |
---|
| 159 | IMG_XLARGE => new DerivativeParams( SizingParams::classic(1224,918) ), |
---|
| 160 | IMG_XXLARGE => new DerivativeParams( SizingParams::classic(1656,1242) ), |
---|
| 161 | ); |
---|
| 162 | foreach($arr as $params) |
---|
| 163 | { |
---|
| 164 | $params->last_mod_time = time(); |
---|
| 165 | } |
---|
| 166 | return $arr; |
---|
[12796] | 167 | } |
---|
[12820] | 168 | |
---|
[13021] | 169 | static function apply_global($params) |
---|
[12851] | 170 | { |
---|
[14550] | 171 | $params->use_watermark = !empty(self::$watermark->file) && |
---|
[12851] | 172 | (self::$watermark->min_size[0]<=$params->sizing->ideal_size[0] |
---|
[14581] | 173 | or self::$watermark->min_size[1]<=$params->sizing->ideal_size[1] ); |
---|
[12851] | 174 | } |
---|
[13038] | 175 | |
---|
[12796] | 176 | private static function build_maps() |
---|
| 177 | { |
---|
| 178 | foreach (self::$type_map as $type=>$params) |
---|
| 179 | { |
---|
| 180 | $params->type = $type; |
---|
[12851] | 181 | self::apply_global($params); |
---|
[12796] | 182 | } |
---|
| 183 | self::$all_type_map = self::$type_map; |
---|
| 184 | |
---|
| 185 | for ($i=0; $i<count(self::$all_types); $i++) |
---|
| 186 | { |
---|
| 187 | $tocheck = self::$all_types[$i]; |
---|
| 188 | if (!isset(self::$type_map[$tocheck])) |
---|
| 189 | { |
---|
| 190 | for ($j=$i-1; $j>=0; $j--) |
---|
| 191 | { |
---|
| 192 | $target = self::$all_types[$j]; |
---|
| 193 | if (isset(self::$type_map[$target])) |
---|
| 194 | { |
---|
| 195 | self::$all_type_map[$tocheck] = self::$type_map[$target]; |
---|
| 196 | self::$undefined_type_map[$tocheck] = $target; |
---|
| 197 | break; |
---|
| 198 | } |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | } |
---|
[12820] | 203 | |
---|
[12796] | 204 | } |
---|
| 205 | |
---|
| 206 | ?> |
---|