PageIconGenerator.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. // Call it like this:
  3. // $PageIconGenerator = new PageIconGenerator(1024);
  4. // $PageIconGenerator->Generate(array(
  5. // 'NodeContactUs',
  6. // 'NodePageVigourouxFolder',
  7. // 'NodePageVigourouxHome',
  8. // 'NodePageVigourouxPage',
  9. // 'NodePageVigourouxSection'
  10. // ));
  11. /**
  12. * Layout Icon Generator
  13. */
  14. class PageIconGenerator {
  15. protected $colors = array();
  16. function __construct($resolution = 800) {
  17. $this->defineResolution($resolution);
  18. $this->colors = array();
  19. $this->colors['on']['border'] = '#DF151A';
  20. $this->colors['on']['content'] = '#FF8080';
  21. $this->colors['off']['border'] = '#C0C0C0';
  22. $this->colors['off']['content'] = '#F0F0F0';
  23. }
  24. protected $ratio = array('width' => 1, 'height' => 1);
  25. function defineResolution($width) {
  26. $ratio = 80 / $width;
  27. $this->ratio = array('width' => $ratio, 'height' => $ratio);
  28. }
  29. protected $areas = array();
  30. function addArea($top, $left, $width, $height) {
  31. $data['top'] = round($top * $this->ratio['height']);
  32. $data['left'] = round($left * $this->ratio['width']);
  33. $data['width'] = round($width * $this->ratio['width']);
  34. $data['height'] = round($height * $this->ratio['height']);
  35. $this->areas[] = $data;
  36. }
  37. function addAreas($areas) {
  38. foreach($areas as $area) {
  39. $this->addArea($area[0], $area[1], $area[2], $area[3]);
  40. }
  41. }
  42. protected function GenerateTemplate($filename) {
  43. $im = imagecreate(82, 62);
  44. // image background
  45. $color = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
  46. imagefilledrectangle($im, 0, 0, 81, 61, $color);
  47. // image border
  48. $color = imagecolorallocate($im, 0, 0, 0);
  49. imagerectangle($im, 0, 0, 81, 61, $color);
  50. // Border
  51. $color = sscanf($this->colors['off']['border'], '#%2x%2x%2x');
  52. $borderColor = imagecolorallocate($im, $color[0], $color[1], $color[2]);
  53. // Content
  54. $color = sscanf($this->colors['off']['content'], '#%2x%2x%2x');
  55. $contentColor = imagecolorallocate($im, $color[0], $color[1], $color[2]);
  56. // Draw areas
  57. foreach($this->areas as $area) {
  58. imagefilledrectangle($im, $area['top'] + 1, $area['left'] + 1, $area['top'] + $area['width'] + 1, $area['left'] + $area['height'] + 1 , $contentColor);
  59. imagerectangle($im, $area['top'] + 1, $area['left'] + 1, $area['top'] + $area['width'] + 1, $area['left'] + $area['height'] + 1, $borderColor);
  60. }
  61. imagegif($im, $filename);
  62. imagedestroy($im);
  63. }
  64. protected function GenerateIcon($area, $filename, $template) {
  65. $im = imagecreatefromgif($template);
  66. // Border
  67. $color = sscanf($this->colors['on']['border'], '#%2x%2x%2x');
  68. $borderColor = imagecolorallocate($im, $color[0], $color[1], $color[2]);
  69. // Content
  70. $color = sscanf($this->colors['on']['content'], '#%2x%2x%2x');
  71. $contentColor = imagecolorallocate($im, $color[0], $color[1], $color[2]);
  72. imagefilledrectangle($im, $area['top'] + 1, $area['left'] + 1, $area['top'] + $area['width'] + 1, $area['left'] + $area['height'] + 1 , $contentColor);
  73. imagerectangle($im, $area['top'] + 1, $area['left'] + 1, $area['top'] + $area['width'] + 1, $area['left'] + $area['height'] + 1, $borderColor);
  74. imagegif($im, $filename);
  75. imagedestroy($im);
  76. }
  77. function clear() {
  78. $this->areas = array();
  79. }
  80. function Generate($className) {
  81. if (is_array($className)) {
  82. foreach($className as $item) {
  83. $this->Generate($item);
  84. }
  85. } else {
  86. $this->clear();
  87. $path = realpath(dirname(__FILE__) . '/../../../web/images') . DIRECTORY_SEPARATOR . 'page-icons';
  88. if (!is_dir($path)) {
  89. mkdir($path);
  90. }
  91. if (strpos($className, 'Node') === 0) {
  92. $folder = substr($className, 4);
  93. } else {
  94. $folder = $className;
  95. }
  96. $path .= '/' . $folder;
  97. if (!is_dir($path)) {
  98. mkdir($path);
  99. }
  100. $areas = call_user_func(array($className, 'getLayoutAreas'));
  101. $this->addAreas($areas);
  102. $path .= DIRECTORY_SEPARATOR;
  103. $template = $path . 'template.gif';
  104. $this->GenerateTemplate($template);
  105. foreach($this->areas as $index => $area) {
  106. $this->GenerateIcon($area, $path . $index . '.gif', $template);
  107. }
  108. }
  109. }
  110. }
  111. ?>