<?php
// Call it like this:
// $PageIconGenerator = new PageIconGenerator(1024);
// $PageIconGenerator->Generate(array(
// 'NodeContactUs',
// 'NodePageVigourouxFolder',
// 'NodePageVigourouxHome',
// 'NodePageVigourouxPage',
// 'NodePageVigourouxSection'
// ));
/**
 * Layout Icon Generator
 */
class PageIconGenerator {
    protected $colors = array();
    function __construct($resolution = 800) {
        $this->defineResolution($resolution);
        $this->colors = array();
        $this->colors['on']['border'] = '#DF151A';
        $this->colors['on']['content'] = '#FF8080';
        $this->colors['off']['border'] = '#C0C0C0';
        $this->colors['off']['content'] = '#F0F0F0';
    }
    protected $ratio = array('width' => 1, 'height' => 1);
    function defineResolution($width) {
        $ratio = 80 / $width;
        $this->ratio = array('width' => $ratio, 'height' => $ratio);
    }

    protected $areas = array();
    function addArea($top, $left, $width, $height) {
        $data['top'] = round($top * $this->ratio['height']);
        $data['left'] = round($left * $this->ratio['width']);
        $data['width'] = round($width * $this->ratio['width']);
        $data['height'] = round($height * $this->ratio['height']);
        $this->areas[] = $data;
    }

    function addAreas($areas) {
        foreach($areas as $area) {
            $this->addArea($area[0], $area[1], $area[2], $area[3]);
        }
    }

    protected function GenerateTemplate($filename) {
        $im = imagecreate(82, 62);
        // image background
        $color = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
        imagefilledrectangle($im, 0, 0, 81, 61, $color);
        // image border
        $color = imagecolorallocate($im, 0, 0, 0);
        imagerectangle($im, 0, 0, 81, 61, $color);
        // Border
        $color = sscanf($this->colors['off']['border'], '#%2x%2x%2x');
        $borderColor = imagecolorallocate($im, $color[0], $color[1], $color[2]);
        // Content
        $color = sscanf($this->colors['off']['content'], '#%2x%2x%2x');
        $contentColor = imagecolorallocate($im, $color[0], $color[1], $color[2]);
        // Draw areas
        foreach($this->areas as $area) {
            imagefilledrectangle($im, $area['top'] + 1, $area['left'] + 1, $area['top'] + $area['width'] + 1, $area['left'] + $area['height'] + 1 , $contentColor);
            imagerectangle($im, $area['top'] + 1, $area['left'] + 1, $area['top'] + $area['width'] + 1, $area['left'] + $area['height'] + 1, $borderColor);
        }

        imagegif($im, $filename);
        imagedestroy($im);
    }

    protected function GenerateIcon($area, $filename, $template) {
        $im = imagecreatefromgif($template);
        // Border
        $color = sscanf($this->colors['on']['border'], '#%2x%2x%2x');
        $borderColor = imagecolorallocate($im, $color[0], $color[1], $color[2]);
        // Content
        $color = sscanf($this->colors['on']['content'], '#%2x%2x%2x');
        $contentColor = imagecolorallocate($im, $color[0], $color[1], $color[2]);
        imagefilledrectangle($im, $area['top'] + 1, $area['left'] + 1, $area['top'] + $area['width'] + 1, $area['left'] + $area['height'] + 1 , $contentColor);
        imagerectangle($im, $area['top'] + 1, $area['left'] + 1, $area['top'] + $area['width'] + 1, $area['left'] + $area['height'] + 1, $borderColor);
        imagegif($im, $filename);
        imagedestroy($im);
    }
    function clear() {
        $this->areas = array();
    }

    function Generate($className) {
        if (is_array($className)) {
            foreach($className as $item) {
                $this->Generate($item);
            }
        } else {
            $this->clear();
            $path = realpath(dirname(__FILE__) . '/../../../web/images') . DIRECTORY_SEPARATOR . 'page-icons';
            if (!is_dir($path)) {
                mkdir($path);
            }
            if (strpos($className, 'Node') === 0) {
                $folder = substr($className, 4);
            } else {
                $folder = $className;
            }
            $path .= '/' . $folder;
            if (!is_dir($path)) {
                mkdir($path);
            }
            $areas = call_user_func(array($className, 'getLayoutAreas'));
            $this->addAreas($areas);
            $path .= DIRECTORY_SEPARATOR;
            $template = $path . 'template.gif';
            $this->GenerateTemplate($template);
            foreach($this->areas as $index => $area) {
                $this->GenerateIcon($area, $path . $index . '.gif', $template);
            }
        }
    }
}

?>