PageRenderTime 152ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/app/modules/Template/Model/Block.php

https://gitlab.com/gregtyka/SiberianCMS
PHP | 127 lines | 101 code | 25 blank | 1 comment | 38 complexity | d718767099e48591a3a7f3f59e318459 MD5 | raw file
  1. <?php
  2. class Template_Model_Block extends Core_Model_Default {
  3. const PATH_IMAGE = '/images/application';
  4. public function __construct($params = array()) {
  5. parent::__construct($params);
  6. $this->_db_table = 'Template_Model_Db_Table_Block';
  7. return $this;
  8. }
  9. public function findByDesign($design_id) {
  10. return $this->getTable()->findByDesign($design_id);
  11. }
  12. public function getName() {
  13. return $this->_($this->getData('name'));
  14. }
  15. public function useImageLink() {
  16. return $this->getData('use_image_link');
  17. }
  18. public function applyToAll() {
  19. return $this->getData('apply_to_all');
  20. }
  21. public function isUniform($block) {
  22. return
  23. $this->getId() == $block->getId()
  24. && ($this->getUseColor() == $block->getUseColor() || ($this->getUseColor() && !$block->getUseColor()))
  25. && ($this->getUseBackgroundColor() == $block->getUseBackgroundColor() || ($this->getUseBackgroundColor() && !$block->getUseBackgroundColor()))
  26. && ($this->useImageLink() == $block->useImageLink() || ($this->useImageLink() && !$block->useImageLink()))
  27. ;
  28. }
  29. public function getBackgroundImage($type = null) {
  30. $background_image = $this->getData('background_image');
  31. if(!empty($background_image)) {
  32. if($type == 'normal') $background_image .= '.jpg';
  33. else if($type == 'retina') $background_image .= '@2x.jpg';
  34. else if($type == 'retina4') $background_image .= '-568h@2x.jpg';
  35. }
  36. else {
  37. $background_image = '/media/admin/mobile/natif/no-background.png';
  38. }
  39. return $background_image;
  40. }
  41. public function setBackgroundImage($background_image) {
  42. $background_image = str_replace(self::PATH_IMAGE, "", $background_image);
  43. $this->setData('background_image', $background_image);
  44. }
  45. public function getImageColor() {
  46. if($this->getData('image_color')) return $this->getData('image_color');
  47. else return $this->getColor();
  48. }
  49. public function colorize($tile_path, $option, $color = null, $flat = true) {
  50. if(!is_file($tile_path)) return '';
  51. // Créé les chemins
  52. $application = $this->getApplication();
  53. $dst = '/'.$this->getCode().'/'.$option->getCode().'_'.uniqid().'.png';
  54. $base_dst = Application_Model_Application::getBaseImagePath().'/'.$dst;
  55. if(!is_dir(dirname($base_dst))) mkdir(dirname($base_dst), 0777, true);
  56. if(!$color) $color = $this->getImageColor();
  57. $color = str_replace('#', '', $color);
  58. $rgb = $this->toRgb($color);
  59. list($width, $height) = getimagesize($tile_path);
  60. $tile = imagecreatefromstring(file_get_contents($tile_path));
  61. if($tile) {
  62. for($x=0; $x<$width;$x++) {
  63. for($y=0;$y<$height;$y++) {
  64. $colors = imagecolorat($tile, $x, $y);
  65. $current_rgb = imagecolorsforindex($tile, $colors);
  66. if($flat) {
  67. $color = imagecolorallocatealpha($tile, $rgb['red'], $rgb['green'], $rgb['blue'], $current_rgb['alpha']);
  68. }
  69. else {
  70. $color = imagecolorallocatealpha($tile, $current_rgb['red']*$rgb['red']/255, $current_rgb['green']*$rgb['green']/255, $current_rgb['blue']*$rgb['blue']/255, $current_rgb['alpha']);
  71. }
  72. imagesetpixel($tile, $x, $y, $color);
  73. }
  74. }
  75. $filename = basename($tile_path);
  76. imagesavealpha($tile, true);
  77. if(!@imagepng($tile, $base_dst)) {
  78. $dst = '';
  79. }
  80. }
  81. return $dst;
  82. }
  83. public function toRgb($hexStr, $returnAsString = false, $seperator = ','){
  84. $hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr);
  85. $rgbArray = array();
  86. if (strlen($hexStr) == 6) {
  87. $colorVal = hexdec($hexStr);
  88. $rgbArray['red'] = 0xFF & ($colorVal >> 0x10);
  89. $rgbArray['green'] = 0xFF & ($colorVal >> 0x8);
  90. $rgbArray['blue'] = 0xFF & $colorVal;
  91. }
  92. elseif (strlen($hexStr) == 3) {
  93. $rgbArray['red'] = hexdec(str_repeat(substr($hexStr, 0, 1), 2));
  94. $rgbArray['green'] = hexdec(str_repeat(substr($hexStr, 1, 1), 2));
  95. $rgbArray['blue'] = hexdec(str_repeat(substr($hexStr, 2, 1), 2));
  96. }
  97. else {
  98. return false;
  99. }
  100. return $returnAsString ? implode($seperator, $rgbArray) : $rgbArray;
  101. }
  102. }