/modules/cms3/images/classes/model/image.php

https://github.com/cms3/cms3 · PHP · 112 lines · 89 code · 23 blank · 0 comment · 3 complexity · fa4896f93a1b8cd2f1928f5c32e55771 MD5 · raw file

  1. <?php
  2. namespace CMS3\Images;
  3. use CMS3\Engine\Model;
  4. use CMS3\Engine\ORM;
  5. use CMS3\Engine\ORM_Meta;
  6. class Model_Image extends Model
  7. {
  8. protected $_config = NULL;
  9. protected $_resize_params = array(
  10. 'width' => 0,
  11. 'height' => 0,
  12. 'zoom' => 100,
  13. 'crop' => FALSE
  14. );
  15. public function resize_params($params)
  16. {
  17. if ($params != NULL)
  18. {
  19. $this->_resize_params = $params;
  20. }
  21. return $this->_resize_params;
  22. }
  23. public static function initialize(ORM_Meta $meta)
  24. {
  25. $meta->table('files__images'); // TODO: убрать эту хрень
  26. $meta->fields(array(
  27. 'id' => ORM::field('primary'),
  28. 'title' => ORM::field('string', array(
  29. 'multilang' => TRUE
  30. )),
  31. 'description' => ORM::field('text', array(
  32. 'multilang' => TRUE
  33. )),
  34. 'file' => ORM::field('belongsto', array(
  35. 'namespace' => 'cms3\files'
  36. )),
  37. 'url' => ORM::field('string'),
  38. 'width' => ORM::field('integer'),
  39. 'height' => ORM::field('integer'),
  40. ));
  41. }
  42. public function __construct($key = NULL)
  43. {
  44. $this->_config = \CMS3::$config->load('cms3\images');
  45. $this->_config = array
  46. (
  47. 'thumbs_dir' => APPPATH.'cache'.DIRECTORY_SEPARATOR.'thumbs',
  48. 'resize_params' => array(
  49. 'width' => 0,
  50. 'height' => 0,
  51. 'zoom' => 100,
  52. 'crop' => FALSE
  53. )
  54. );
  55. $this->_resize_params = $this->_config['resize_params'];
  56. return parent::__construct($key);
  57. }
  58. public function thumbnail(array $params = array(), $generate = TRUE)
  59. {
  60. if (is_string($params))
  61. {
  62. $params = $this->decode_params($params);
  63. }
  64. $orig_file = $this->file->filepath();
  65. $params = (array)$params + $this->_resize_params;
  66. $thumb_file = $this->_thumbnail_filename($orig_file, $params);
  67. $thumb = Thumbnail::factory($orig_file, $thumb_file, $params, $generate);
  68. return $thumb;
  69. }
  70. protected function _thumbnail_filename($filename, $params)
  71. {
  72. $thumbs_dir = $this->_config['thumbs_dir'];
  73. $pathinfo = pathinfo($filename);
  74. $suffix = '_' . $this->encode_params($params);
  75. $ext = ($pathinfo['extension'] ? '.' : '') . $pathinfo['extension'];
  76. $thumb = $thumbs_dir . DIRECTORY_SEPARATOR . $pathinfo['filename'] . $suffix . $ext;
  77. return $thumb;
  78. }
  79. public function encode_params($params)
  80. {
  81. $string = base64_encode(serialize($params));
  82. return rtrim($string, '=');
  83. }
  84. public function decode_params($string)
  85. {
  86. $params = unserialize(base64_decode($string));
  87. return $params;
  88. }
  89. }