[12796] | 1 | <?php |
---|
| 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 | // +-----------------------------------------------------------------------+ |
---|
| 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(); |
---|
[12820] | 54 | |
---|
[12796] | 55 | static function get_all_types() |
---|
| 56 | { |
---|
| 57 | return self::$all_types; |
---|
| 58 | } |
---|
[12820] | 59 | |
---|
[12796] | 60 | static function get_all_type_map() |
---|
| 61 | { |
---|
| 62 | return self::$all_type_map; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | static function get_defined_type_map() |
---|
| 66 | { |
---|
| 67 | return self::$type_map; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | static function get_undefined_type_map() |
---|
| 71 | { |
---|
| 72 | return self::$undefined_type_map; |
---|
| 73 | } |
---|
[12820] | 74 | |
---|
[12796] | 75 | static function get_by_type($type) |
---|
| 76 | { |
---|
| 77 | return self::$all_type_map[$type]; |
---|
| 78 | } |
---|
[13038] | 79 | |
---|
[13021] | 80 | static function get_custom($w, $h, $crop=0, $minw=null, $minh=null) |
---|
| 81 | { |
---|
| 82 | $params = new DerivativeParams( new SizingParams( array($w,$h), $crop, array($minw,$minh)) ); |
---|
| 83 | self::apply_global($params); |
---|
[12820] | 84 | |
---|
[13021] | 85 | $key = array(); |
---|
| 86 | $params->add_url_tokens($key); |
---|
| 87 | $key = implode('_',$key); |
---|
| 88 | if ( @self::$custom[$key] < time() - 24*3600) |
---|
| 89 | { |
---|
| 90 | self::$custom[$key] = time(); |
---|
| 91 | self::save(); |
---|
| 92 | } |
---|
| 93 | return $params; |
---|
| 94 | } |
---|
| 95 | |
---|
[12851] | 96 | static function get_watermark() |
---|
| 97 | { |
---|
| 98 | return self::$watermark; |
---|
| 99 | } |
---|
| 100 | |
---|
[12796] | 101 | static function load_from_db() |
---|
| 102 | { |
---|
[12820] | 103 | global $conf; |
---|
| 104 | $arr = @unserialize($conf['derivatives']); |
---|
| 105 | if (false!==$arr) |
---|
| 106 | { |
---|
| 107 | self::$type_map = $arr['d']; |
---|
[12851] | 108 | self::$watermark = @$arr['w']; |
---|
| 109 | if (!self::$watermark) self::$watermark = new WatermarkParams(); |
---|
[13038] | 110 | self::$custom = @$arr['c']; |
---|
| 111 | if (!self::$custom) self::$custom = array(); |
---|
[12820] | 112 | } |
---|
| 113 | else |
---|
| 114 | { |
---|
| 115 | self::make_default(); |
---|
| 116 | } |
---|
[12796] | 117 | self::build_maps(); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | static function load_from_file() |
---|
| 121 | { |
---|
[12820] | 122 | global $conf; |
---|
| 123 | $arr = @unserialize(@file_get_contents(PHPWG_ROOT_PATH.$conf['data_location'].'derivatives.dat')); |
---|
| 124 | if (false!==$arr) |
---|
| 125 | { |
---|
| 126 | self::$type_map = $arr['d']; |
---|
[12851] | 127 | self::$watermark = @$arr['w']; |
---|
| 128 | if (!self::$watermark) self::$watermark = new WatermarkParams(); |
---|
[13021] | 129 | self::$custom = @$arr['c']; |
---|
| 130 | if (!self::$custom) self::$custom = array(); |
---|
[12820] | 131 | } |
---|
| 132 | else |
---|
| 133 | { |
---|
| 134 | self::make_default(); |
---|
| 135 | } |
---|
[12796] | 136 | self::build_maps(); |
---|
| 137 | } |
---|
| 138 | |
---|
[12851] | 139 | static function set_watermark($watermark) |
---|
| 140 | { |
---|
| 141 | self::$watermark = $watermark; |
---|
| 142 | } |
---|
[13038] | 143 | |
---|
[12820] | 144 | static function set_and_save($map) |
---|
| 145 | { |
---|
| 146 | self::$type_map = $map; |
---|
[13021] | 147 | self::save(); |
---|
| 148 | self::build_maps(); |
---|
| 149 | } |
---|
[12820] | 150 | |
---|
[13021] | 151 | static function save() |
---|
| 152 | { |
---|
| 153 | global $conf; |
---|
| 154 | |
---|
[12820] | 155 | $ser = serialize( array( |
---|
[12851] | 156 | 'd' => self::$type_map, |
---|
| 157 | 'w' => self::$watermark, |
---|
[13021] | 158 | 'c' => self::$custom, |
---|
[12820] | 159 | ) ); |
---|
| 160 | conf_update_param('derivatives', addslashes($ser) ); |
---|
| 161 | file_put_contents(PHPWG_ROOT_PATH.$conf['data_location'].'derivatives.dat', $ser); |
---|
| 162 | } |
---|
| 163 | |
---|
[12851] | 164 | private static function make_default() |
---|
[12796] | 165 | { |
---|
[12851] | 166 | self::$watermark = new WatermarkParams(); |
---|
[13472] | 167 | self::$type_map[IMG_SQUARE] = new DerivativeParams( SizingParams::square(120,120) ); |
---|
[12820] | 168 | self::$type_map[IMG_THUMB] = new DerivativeParams( SizingParams::classic(144,144) ); |
---|
[13683] | 169 | self::$type_map[IMG_XXSMALL] = new DerivativeParams( SizingParams::classic(240,240) ); |
---|
| 170 | self::$type_map[IMG_XSMALL] = new DerivativeParams( SizingParams::classic(432,324) ); |
---|
| 171 | self::$type_map[IMG_SMALL] = new DerivativeParams( SizingParams::classic(576,432) ); |
---|
| 172 | self::$type_map[IMG_MEDIUM] = new DerivativeParams( SizingParams::classic(792,594) ); |
---|
| 173 | self::$type_map[IMG_LARGE] = new DerivativeParams( SizingParams::classic(1008,756) ); |
---|
| 174 | self::$type_map[IMG_XLARGE] = new DerivativeParams( SizingParams::classic(1224,918) ); |
---|
| 175 | self::$type_map[IMG_XXLARGE] = new DerivativeParams( SizingParams::classic(1656,1242) ); |
---|
[12796] | 176 | } |
---|
[12820] | 177 | |
---|
[13021] | 178 | static function apply_global($params) |
---|
[12851] | 179 | { |
---|
| 180 | if (!empty(self::$watermark->file) && |
---|
| 181 | (self::$watermark->min_size[0]<=$params->sizing->ideal_size[0] |
---|
| 182 | && self::$watermark->min_size[1]<=$params->sizing->ideal_size[1] ) ) |
---|
| 183 | { |
---|
| 184 | $params->use_watermark = true; |
---|
| 185 | } |
---|
| 186 | } |
---|
[13038] | 187 | |
---|
[12796] | 188 | private static function build_maps() |
---|
| 189 | { |
---|
| 190 | foreach (self::$type_map as $type=>$params) |
---|
| 191 | { |
---|
| 192 | $params->type = $type; |
---|
[12851] | 193 | self::apply_global($params); |
---|
[12796] | 194 | } |
---|
| 195 | self::$all_type_map = self::$type_map; |
---|
| 196 | |
---|
| 197 | for ($i=0; $i<count(self::$all_types); $i++) |
---|
| 198 | { |
---|
| 199 | $tocheck = self::$all_types[$i]; |
---|
| 200 | if (!isset(self::$type_map[$tocheck])) |
---|
| 201 | { |
---|
| 202 | for ($j=$i-1; $j>=0; $j--) |
---|
| 203 | { |
---|
| 204 | $target = self::$all_types[$j]; |
---|
| 205 | if (isset(self::$type_map[$target])) |
---|
| 206 | { |
---|
| 207 | self::$all_type_map[$tocheck] = self::$type_map[$target]; |
---|
| 208 | self::$undefined_type_map[$tocheck] = $target; |
---|
| 209 | break; |
---|
| 210 | } |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | } |
---|
| 214 | } |
---|
[12820] | 215 | |
---|
[12796] | 216 | } |
---|
| 217 | |
---|
| 218 | ?> |
---|