Mixin_Dynamic_Thumbnails_Manager

The NextGEN Gallery Mixin Dynamic Thumbnails Manager class.

Defined (1)

The class is defined in the following location(s).

/products/photocrati_nextgen/modules/dynamic_thumbnails/package.module.dynamic_thumbnails.php  
  1. class Mixin_Dynamic_Thumbnails_Manager extends Mixin 
  2. function get_route_name() 
  3. return C_NextGen_Settings::get_instance()->get('dynamic_thumbnail_slug'); 
  4. function _get_params_sanitized($params) 
  5. if (isset($params['rotation'])) { 
  6. $rotation = intval($params['rotation']); 
  7. if ($rotation && in_array(abs($rotation), array(90, 180, 270))) { 
  8. $rotation = $rotation % 360; 
  9. if ($rotation < 0) { 
  10. $rotation = 360 - $rotation; 
  11. $params['rotation'] = $rotation; 
  12. } else { 
  13. unset($params['rotation']); 
  14. if (isset($params['flip'])) { 
  15. $flip = strtolower($params['flip']); 
  16. if (in_array($flip, array('h', 'v', 'hv'))) { 
  17. $params['flip'] = $flip; 
  18. } else { 
  19. unset($params['flip']); 
  20. return $params; 
  21. function get_uri_from_params($params) 
  22. $params = $this->object->_get_params_sanitized($params); 
  23. $image = isset($params['image']) ? $params['image'] : null; 
  24. $image_id = is_scalar($image) || is_null($image) ? (int) $image : $image->pid; 
  25. $image_width = isset($params['width']) ? $params['width'] : null; 
  26. $image_height = isset($params['height']) ? $params['height'] : null; 
  27. $image_quality = isset($params['quality']) ? $params['quality'] : null; 
  28. $image_type = isset($params['type']) ? $params['type'] : null; 
  29. $image_crop = isset($params['crop']) ? $params['crop'] : null; 
  30. $image_watermark = isset($params['watermark']) ? $params['watermark'] : null; 
  31. $image_rotation = isset($params['rotation']) ? $params['rotation'] : null; 
  32. $image_flip = isset($params['flip']) ? $params['flip'] : null; 
  33. $image_reflection = isset($params['reflection']) ? $params['reflection'] : null; 
  34. $uri = null; 
  35. $uri .= '/'; 
  36. $uri .= $this->object->get_route_name() . '/'; 
  37. $uri .= strval($image_id) . '/'; 
  38. $uri .= strval($image_width) . 'x' . strval($image_height); 
  39. if ($image_quality != null) { 
  40. $uri .= 'x' . strval($image_quality); 
  41. $uri .= '/'; 
  42. if ($image_type != null) { 
  43. $uri .= $image_type . '/'; 
  44. if ($image_crop) { 
  45. $uri .= 'crop/'; 
  46. if ($image_watermark) { 
  47. $uri .= 'watermark/'; 
  48. if ($image_rotation) { 
  49. $uri .= 'rotation-' . $image_rotation . '/'; 
  50. if ($image_flip) { 
  51. $uri .= 'flip-' . $image_flip . '/'; 
  52. if ($image_reflection) { 
  53. $uri .= 'reflection/'; 
  54. return $uri; 
  55. function get_image_uri($image, $params) 
  56. $params['image'] = $image; 
  57. $uri = $this->object->get_uri_from_params($params); 
  58. if (substr($uri, -1) != '/') { 
  59. $uri .= '/'; 
  60. $uri .= wp_hash($uri) . '/'; 
  61. return $uri; 
  62. function get_image_url($image, $params) 
  63. return C_Router::get_instance()->get_url($this->object->get_image_uri($image, $params), FALSE, 'root'); 
  64. function get_params_from_uri($uri) 
  65. $regex = '/\\/?' . $this->object->get_route_name() . '\\/(\\d+)(?:\\/(.*))?/'; 
  66. $match = null; 
  67. // XXX move this URL clean up to I_Router? 
  68. $uri = preg_replace('/\\/index.php\\//', '/', $uri, 1); 
  69. $uri = trim($uri, '/'); 
  70. if (@preg_match($regex, $uri, $match) > 0) { 
  71. $image_id = $match[1]; 
  72. $uri_args = isset($match[2]) ? explode('/', $match[2]) : array(); 
  73. $params = array('image' => $image_id); 
  74. foreach ($uri_args as $uri_arg) { 
  75. $uri_arg_set = explode('-', $uri_arg); 
  76. $uri_arg_name = array_shift($uri_arg_set); 
  77. $uri_arg_value = $uri_arg_set ? array_shift($uri_arg_set) : null; 
  78. $size_match = null; 
  79. if ($uri_arg == 'watermark') { 
  80. $params['watermark'] = true; 
  81. } else { 
  82. if ($uri_arg_name == 'rotation') { 
  83. $params['rotation'] = $uri_arg_value; 
  84. } else { 
  85. if ($uri_arg_name == 'flip') { 
  86. $params['flip'] = $uri_arg_value; 
  87. } else { 
  88. if ($uri_arg == 'reflection') { 
  89. $params['reflection'] = true; 
  90. } else { 
  91. if ($uri_arg == 'crop') { 
  92. $params['crop'] = true; 
  93. } else { 
  94. if (in_array(strtolower($uri_arg), array('gif', 'jpg', 'png'))) { 
  95. $params['type'] = $uri_arg; 
  96. } else { 
  97. if (preg_match('/(\\d+)x(\\d+)(?:x(\\d+))?/i', $uri_arg, $size_match) > 0) { 
  98. $params['width'] = $size_match[1]; 
  99. $params['height'] = $size_match[2]; 
  100. if (isset($size_match[3])) { 
  101. $params['quality'] = $size_match[3]; 
  102. return $this->object->_get_params_sanitized($params); 
  103. return null; 
  104. function _get_name_prefix_list() 
  105. return array('id' => 'nggid0', 'size' => 'ngg0dyn-', 'flags' => '00f0', 'flag' => array('w0' => 'watermark', 'c0' => 'crop', 'r1' => 'rotation', 'f1' => 'flip', 'r0' => 'reflection', 't0' => 'type'), 'flag_len' => 2, 'max_value_length' => 15); 
  106. function get_name_from_params($params, $only_size_name = false, $id_in_name = true) 
  107. $prefix_list = $this->object->_get_name_prefix_list(); 
  108. $id_prefix = $prefix_list['id']; 
  109. $size_prefix = $prefix_list['size']; 
  110. $flags_prefix = $prefix_list['flags']; 
  111. $flags = $prefix_list['flag']; 
  112. $max_value_length = $prefix_list['max_value_length']; 
  113. $params = $this->object->_get_params_sanitized($params); 
  114. $image = isset($params['image']) ? $params['image'] : null; 
  115. $image_width = isset($params['width']) ? $params['width'] : null; 
  116. $image_height = isset($params['height']) ? $params['height'] : null; 
  117. $image_quality = isset($params['quality']) ? $params['quality'] : null; 
  118. $extension = null; 
  119. $name = null; 
  120. // if $only_size_name is false then we include the file name and image id for the image 
  121. if (!$only_size_name) { 
  122. if (is_int($image)) { 
  123. $imap = C_Image_Mapper::get_instance(); 
  124. $image = $imap->find($image); 
  125. if ($image != null) { 
  126. // this is used to remove the extension and then add it back at the end of the name 
  127. $extension = M_I18n::mb_pathinfo($image->filename, PATHINFO_EXTENSION); 
  128. if ($extension != null) { 
  129. $extension = '.' . $extension; 
  130. $name .= M_I18n::mb_basename($image->filename, $extension); 
  131. $name .= '-'; 
  132. if ($id_in_name) { 
  133. $image_id = strval($image->pid); 
  134. $id_len = min($max_value_length, strlen($image_id)); 
  135. $id_len_hex = dechex($id_len); 
  136. // sanity check, should never occurr if $max_value_length is not messed up, ensure only 1 character is used to encode length or else skip parameter 
  137. if (strlen($id_len_hex) == 1) { 
  138. $name .= $id_prefix . $id_len . substr($image_id, 0, $id_len); 
  139. $name .= '-'; 
  140. $name .= $size_prefix; 
  141. $name .= strval($image_width) . 'x' . strval($image_height); 
  142. if ($image_quality != null) { 
  143. $name .= 'x' . $image_quality; 
  144. $name .= '-'; 
  145. $name .= $flags_prefix; 
  146. foreach ($flags as $flag_prefix => $flag_name) { 
  147. $flag_value = 0; 
  148. if (isset($params[$flag_name])) { 
  149. $flag_value = $params[$flag_name]; 
  150. if (!is_string($flag_value)) { 
  151. // only strings or ints allowed, sprintf is required because intval(0) returns '' and not '0' 
  152. $flag_value = intval($flag_value); 
  153. $flag_value = sprintf('%d', $flag_value); 
  154. $flag_value = strval($flag_value); 
  155. $flag_len = min($max_value_length, strlen($flag_value)); 
  156. $flag_len_hex = dechex($flag_len); 
  157. // sanity check, should never occurr if $max_value_length is not messed up, ensure only 1 character is used to encode length or else skip parameter 
  158. if (strlen($flag_len_hex) == 1) { 
  159. $name .= $flag_prefix . $flag_len . substr($flag_value, 0, $flag_len); 
  160. $name .= $extension; 
  161. return $name; 
  162. function get_size_name($params) 
  163. $name = $this->object->get_name_from_params($params, true); 
  164. return $name; 
  165. function get_image_name($image, $params) 
  166. $params['image'] = $image; 
  167. $name = $this->object->get_name_from_params($params); 
  168. return $name; 
  169. function get_params_from_name($name, $is_only_size_name = false) 
  170. $prefix_list = $this->object->_get_name_prefix_list(); 
  171. $id_prefix = $prefix_list['id']; 
  172. $size_prefix = $prefix_list['size']; 
  173. $flags_prefix = $prefix_list['flags']; 
  174. $max_value_length = $prefix_list['max_value_length']; 
  175. $size_name = null; 
  176. $id_name = null; 
  177. $params = array(); 
  178. if (!$is_only_size_name) { 
  179. $extension = M_I18n::mb_pathinfo($name, PATHINFO_EXTENSION); 
  180. if ($extension != null) { 
  181. $extension = '.' . $extension; 
  182. $name = M_I18n::mb_basename($name, $extension); 
  183. $size_index = strrpos($name, $size_prefix); 
  184. if ($size_index > 0 || $size_index === 0) { 
  185. // check if name contains dynamic size/params info by looking for prefix 
  186. $size_name = substr($name, $size_index); 
  187. if (!$is_only_size_name) { 
  188. // name should contain the image id, search for prefix 
  189. $id_index = strrpos($name, $id_prefix); 
  190. if ($id_index > 0 || $id_index === 0) { 
  191. if ($size_index > 0 && $size_index > $id_index) { 
  192. $id_name = substr($name, $id_index, $size_index - $id_index); 
  193. } else { 
  194. $id_name = substr($name, $id_index); 
  195. // Double check we got a correct dynamic size/params string 
  196. if (substr($size_name, 0, strlen($size_prefix)) == $size_prefix) { 
  197. $flags = $prefix_list['flag']; 
  198. // get the length of the flag id (the key in the $flags array) in the string (how many characters to consume) 
  199. $flag_id_len = $prefix_list['flag_len']; 
  200. $params_str = substr($size_name, strlen($size_prefix)); 
  201. $params_parts = explode('-', $params_str); 
  202. // $param_part is a single param, separated by '-' 
  203. foreach ($params_parts as $param_part) { 
  204. // Parse WxHxQ - Q=quality 
  205. $param_size = explode('x', $param_part); 
  206. $param_size_count = count($param_size); 
  207. if (substr($param_part, 0, strlen($flags_prefix)) == $flags_prefix) { 
  208. /** Set flags, using $flags keys as prefixes */ 
  209. // move string pointer up (after the main flags prefix) 
  210. $param_flags = substr($param_part, strlen($flags_prefix)); 
  211. $param_flags_len = strlen($param_flags); 
  212. $flags_todo = $flags; 
  213. while (true) { 
  214. // ensure we don't run into an infinite loop ;) 
  215. if (count($flags_todo) == 0 || strlen($param_flags) == 0) { 
  216. break; 
  217. // get the flag prefix (a key in the $flags array) using flag id length 
  218. $flag_prefix = substr($param_flags, 0, $flag_id_len); 
  219. // move string pointer up (after the single flag prefix) 
  220. $param_flags = substr($param_flags, $flag_id_len); 
  221. // get the length of the flag value in the string (how many characters to consume) 
  222. // flag value length is stored in a single hexadecimal character next to the flag prefix 
  223. $flag_value_len = min(hexdec(substr($param_flags, 0, 1)), min($max_value_length, strlen($param_flags) - 1)); 
  224. // get the flag value 
  225. $flag_value = substr($param_flags, 1, $flag_value_len); 
  226. // move string pointer up (after the entire flag) 
  227. $param_flags = substr($param_flags, $flag_value_len + 1); 
  228. // make sure the flag is supported 
  229. if (isset($flags[$flag_prefix])) { 
  230. $flag_name = $flags[$flag_prefix]; 
  231. if (is_numeric($flag_value)) { 
  232. // convert numerical flags to integers 
  233. $flag_value = intval($flag_value); 
  234. $params[$flag_name] = $flag_value; 
  235. if (isset($flags_todo[$flag_prefix])) { 
  236. unset($flags_todo[$flag_prefix]); 
  237. } else { 
  238. // XXX unknown flag? 
  239. } else { 
  240. if ($param_size_count == 2 || $param_size_count == 3) { 
  241. // Set W H Q 
  242. $params['width'] = intval($param_size[0]); 
  243. $params['height'] = intval($param_size[1]); 
  244. if (isset($param_size[2]) && intval($param_size[2]) > 0) { 
  245. $params['quality'] = intval($param_size[2]); 
  246. // Double check we got a correct id string 
  247. if (substr($id_name, 0, strlen($id_prefix)) == $id_prefix) { 
  248. // move string pointer up (after the prefix) 
  249. $id_name = substr($id_name, strlen($id_prefix)); 
  250. // get the length of the image id in the string (how many characters to consume) 
  251. $id_len = min(hexdec(substr($id_name, 0, 1)), min($max_value_length, strlen($id_name) - 1)); 
  252. // get the id based on old position and id length 
  253. $image_id = intval(substr($id_name, 1, $id_len)); 
  254. if ($image_id > 0) { 
  255. $params['image'] = $image_id; 
  256. return $this->object->_get_params_sanitized($params); 
  257. function is_size_dynamic($name, $is_only_size_name = false) 
  258. $params = $this->object->get_params_from_name($name, $is_only_size_name); 
  259. if (isset($params['width']) && isset($params['height'])) { 
  260. return true; 
  261. return false;