PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/fsn-site-central/mediatheque/include/derivative_std_params.inc.php

https://gitlab.com/team_fsn/fsn-php
PHP | 283 lines | 166 code | 24 blank | 93 comment | 9 complexity | d575e13cdf865833a9e80923bd102686 MD5 | raw file
  1. <?php
  2. // +-----------------------------------------------------------------------+
  3. // | Piwigo - a PHP based photo gallery |
  4. // +-----------------------------------------------------------------------+
  5. // | Copyright(C) 2008-2016 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. * @package Derivatives
  23. */
  24. define('IMG_SQUARE', 'square');
  25. define('IMG_THUMB', 'thumb');
  26. define('IMG_XXSMALL', '2small');
  27. define('IMG_XSMALL', 'xsmall');
  28. define('IMG_SMALL', 'small');
  29. define('IMG_MEDIUM', 'medium');
  30. define('IMG_LARGE', 'large');
  31. define('IMG_XLARGE', 'xlarge');
  32. define('IMG_XXLARGE', 'xxlarge');
  33. define('IMG_CUSTOM', 'custom');
  34. /**
  35. * Container for watermark configuration.
  36. */
  37. final class WatermarkParams
  38. {
  39. /** @var string */
  40. public $file = '';
  41. /** @var int[] */
  42. public $min_size = array(500,500);
  43. /** @var int */
  44. public $xpos = 50;
  45. /** @var int */
  46. public $ypos = 50;
  47. /** @var int */
  48. public $xrepeat = 0;
  49. /** @var int */
  50. public $yrepeat = 0;
  51. /** @var int */
  52. public $opacity = 100;
  53. }
  54. /**
  55. * Container for standard derivatives parameters.
  56. */
  57. final class ImageStdParams
  58. {
  59. /** @var string[] */
  60. private static $all_types = array(
  61. IMG_SQUARE, IMG_THUMB, IMG_XXSMALL, IMG_XSMALL, IMG_SMALL,
  62. IMG_MEDIUM, IMG_LARGE, IMG_XLARGE, IMG_XXLARGE
  63. );
  64. /** @var DerivativeParams[] */
  65. private static $all_type_map = array();
  66. /** @var DerivativeParams[] */
  67. private static $type_map = array();
  68. /** @var DerivativeParams[] */
  69. private static $undefined_type_map = array();
  70. /** @var WatermarkParams */
  71. private static $watermark;
  72. /** @var array */
  73. public static $custom = array();
  74. /** @var int */
  75. public static $quality=95;
  76. /**
  77. * @return string[]
  78. */
  79. static function get_all_types()
  80. {
  81. return self::$all_types;
  82. }
  83. /**
  84. * @return DerivativeParams[]
  85. */
  86. static function get_all_type_map()
  87. {
  88. return self::$all_type_map;
  89. }
  90. /**
  91. * @return DerivativeParams[]
  92. */
  93. static function get_defined_type_map()
  94. {
  95. return self::$type_map;
  96. }
  97. /**
  98. * @return DerivativeParams[]
  99. */
  100. static function get_undefined_type_map()
  101. {
  102. return self::$undefined_type_map;
  103. }
  104. /**
  105. * @return DerivativeParams
  106. */
  107. static function get_by_type($type)
  108. {
  109. return self::$all_type_map[$type];
  110. }
  111. /**
  112. * @param int $w
  113. * @param int $h
  114. * @param float $crop
  115. * @param int $minw
  116. * @param int $minh
  117. * @return DerivativeParams
  118. */
  119. static function get_custom($w, $h, $crop=0, $minw=null, $minh=null)
  120. {
  121. $params = new DerivativeParams( new SizingParams( array($w,$h), $crop, array($minw,$minh)) );
  122. self::apply_global($params);
  123. $key = array();
  124. $params->add_url_tokens($key);
  125. $key = implode('_',$key);
  126. if ( @self::$custom[$key] < time() - 24*3600)
  127. {
  128. self::$custom[$key] = time();
  129. self::save();
  130. }
  131. return $params;
  132. }
  133. /**
  134. * @return WatermarkParams
  135. */
  136. static function get_watermark()
  137. {
  138. return self::$watermark;
  139. }
  140. /**
  141. * Loads derivative configuration from database or initializes it.
  142. */
  143. static function load_from_db()
  144. {
  145. global $conf;
  146. $arr = @unserialize($conf['derivatives']);
  147. if (false!==$arr)
  148. {
  149. self::$type_map = $arr['d'];
  150. self::$watermark = @$arr['w'];
  151. if (!self::$watermark) self::$watermark = new WatermarkParams();
  152. self::$custom = @$arr['c'];
  153. if (!self::$custom) self::$custom = array();
  154. if (isset($arr['q'])) self::$quality = $arr['q'];
  155. }
  156. else
  157. {
  158. self::$watermark = new WatermarkParams();
  159. self::$type_map = self::get_default_sizes();
  160. self::save();
  161. }
  162. self::build_maps();
  163. }
  164. /**
  165. * @param WatermarkParams $watermark
  166. */
  167. static function set_watermark($watermark)
  168. {
  169. self::$watermark = $watermark;
  170. }
  171. /**
  172. * @see ImageStdParams::save()
  173. *
  174. * @param DerivativeParams[] $map
  175. */
  176. static function set_and_save($map)
  177. {
  178. self::$type_map = $map;
  179. self::save();
  180. self::build_maps();
  181. }
  182. /**
  183. * Saves the configuration in database.
  184. */
  185. static function save()
  186. {
  187. $ser = serialize( array(
  188. 'd' => self::$type_map,
  189. 'q' => self::$quality,
  190. 'w' => self::$watermark,
  191. 'c' => self::$custom,
  192. ) );
  193. conf_update_param('derivatives', addslashes($ser) );
  194. }
  195. /**
  196. * @return DerivativeParams[]
  197. */
  198. static function get_default_sizes()
  199. {
  200. $arr = array(
  201. IMG_SQUARE => new DerivativeParams( SizingParams::square(120,120) ),
  202. IMG_THUMB => new DerivativeParams( SizingParams::classic(144,144) ),
  203. IMG_XXSMALL => new DerivativeParams( SizingParams::classic(240,240) ),
  204. IMG_XSMALL => new DerivativeParams( SizingParams::classic(432,324) ),
  205. IMG_SMALL => new DerivativeParams( SizingParams::classic(576,432) ),
  206. IMG_MEDIUM => new DerivativeParams( SizingParams::classic(792,594) ),
  207. IMG_LARGE => new DerivativeParams( SizingParams::classic(1008,756) ),
  208. IMG_XLARGE => new DerivativeParams( SizingParams::classic(1224,918) ),
  209. IMG_XXLARGE => new DerivativeParams( SizingParams::classic(1656,1242) ),
  210. );
  211. $now = time();
  212. foreach($arr as $params)
  213. {
  214. $params->last_mod_time = $now;
  215. }
  216. return $arr;
  217. }
  218. /**
  219. * Compute 'apply_watermark'
  220. *
  221. * @param DerivativeParams $params
  222. */
  223. static function apply_global($params)
  224. {
  225. $params->use_watermark = !empty(self::$watermark->file) &&
  226. (self::$watermark->min_size[0]<=$params->sizing->ideal_size[0]
  227. or self::$watermark->min_size[1]<=$params->sizing->ideal_size[1] );
  228. }
  229. /**
  230. * Build 'type_map', 'all_type_map' and 'undefined_type_map'.
  231. */
  232. private static function build_maps()
  233. {
  234. foreach (self::$type_map as $type=>$params)
  235. {
  236. $params->type = $type;
  237. self::apply_global($params);
  238. }
  239. self::$all_type_map = self::$type_map;
  240. for ($i=0; $i<count(self::$all_types); $i++)
  241. {
  242. $tocheck = self::$all_types[$i];
  243. if (!isset(self::$type_map[$tocheck]))
  244. {
  245. for ($j=$i-1; $j>=0; $j--)
  246. {
  247. $target = self::$all_types[$j];
  248. if (isset(self::$type_map[$target]))
  249. {
  250. self::$all_type_map[$tocheck] = self::$type_map[$target];
  251. self::$undefined_type_map[$tocheck] = $target;
  252. break;
  253. }
  254. }
  255. }
  256. }
  257. }
  258. }
  259. ?>