[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 | |
---|
[12851] | 120 | static function set_watermark($watermark) |
---|
| 121 | { |
---|
| 122 | self::$watermark = $watermark; |
---|
| 123 | } |
---|
[13038] | 124 | |
---|
[12820] | 125 | static function set_and_save($map) |
---|
| 126 | { |
---|
| 127 | self::$type_map = $map; |
---|
[13021] | 128 | self::save(); |
---|
| 129 | self::build_maps(); |
---|
| 130 | } |
---|
[12820] | 131 | |
---|
[13021] | 132 | static function save() |
---|
| 133 | { |
---|
| 134 | global $conf; |
---|
| 135 | |
---|
[12820] | 136 | $ser = serialize( array( |
---|
[12851] | 137 | 'd' => self::$type_map, |
---|
| 138 | 'w' => self::$watermark, |
---|
[13021] | 139 | 'c' => self::$custom, |
---|
[12820] | 140 | ) ); |
---|
| 141 | conf_update_param('derivatives', addslashes($ser) ); |
---|
| 142 | } |
---|
| 143 | |
---|
[12851] | 144 | private static function make_default() |
---|
[12796] | 145 | { |
---|
[12851] | 146 | self::$watermark = new WatermarkParams(); |
---|
[13472] | 147 | self::$type_map[IMG_SQUARE] = new DerivativeParams( SizingParams::square(120,120) ); |
---|
[12820] | 148 | self::$type_map[IMG_THUMB] = new DerivativeParams( SizingParams::classic(144,144) ); |
---|
[13683] | 149 | self::$type_map[IMG_XXSMALL] = new DerivativeParams( SizingParams::classic(240,240) ); |
---|
| 150 | self::$type_map[IMG_XSMALL] = new DerivativeParams( SizingParams::classic(432,324) ); |
---|
| 151 | self::$type_map[IMG_SMALL] = new DerivativeParams( SizingParams::classic(576,432) ); |
---|
| 152 | self::$type_map[IMG_MEDIUM] = new DerivativeParams( SizingParams::classic(792,594) ); |
---|
| 153 | self::$type_map[IMG_LARGE] = new DerivativeParams( SizingParams::classic(1008,756) ); |
---|
| 154 | self::$type_map[IMG_XLARGE] = new DerivativeParams( SizingParams::classic(1224,918) ); |
---|
| 155 | self::$type_map[IMG_XXLARGE] = new DerivativeParams( SizingParams::classic(1656,1242) ); |
---|
[12796] | 156 | } |
---|
[12820] | 157 | |
---|
[13021] | 158 | static function apply_global($params) |
---|
[12851] | 159 | { |
---|
| 160 | if (!empty(self::$watermark->file) && |
---|
| 161 | (self::$watermark->min_size[0]<=$params->sizing->ideal_size[0] |
---|
| 162 | && self::$watermark->min_size[1]<=$params->sizing->ideal_size[1] ) ) |
---|
| 163 | { |
---|
| 164 | $params->use_watermark = true; |
---|
| 165 | } |
---|
| 166 | } |
---|
[13038] | 167 | |
---|
[12796] | 168 | private static function build_maps() |
---|
| 169 | { |
---|
| 170 | foreach (self::$type_map as $type=>$params) |
---|
| 171 | { |
---|
| 172 | $params->type = $type; |
---|
[12851] | 173 | self::apply_global($params); |
---|
[12796] | 174 | } |
---|
| 175 | self::$all_type_map = self::$type_map; |
---|
| 176 | |
---|
| 177 | for ($i=0; $i<count(self::$all_types); $i++) |
---|
| 178 | { |
---|
| 179 | $tocheck = self::$all_types[$i]; |
---|
| 180 | if (!isset(self::$type_map[$tocheck])) |
---|
| 181 | { |
---|
| 182 | for ($j=$i-1; $j>=0; $j--) |
---|
| 183 | { |
---|
| 184 | $target = self::$all_types[$j]; |
---|
| 185 | if (isset(self::$type_map[$target])) |
---|
| 186 | { |
---|
| 187 | self::$all_type_map[$tocheck] = self::$type_map[$target]; |
---|
| 188 | self::$undefined_type_map[$tocheck] = $target; |
---|
| 189 | break; |
---|
| 190 | } |
---|
| 191 | } |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | } |
---|
[12820] | 195 | |
---|
[12796] | 196 | } |
---|
| 197 | |
---|
| 198 | ?> |
---|