<?php
/************************************************
*
* Class.GenerateGraphic.php
* Genera gráficas con los valores proporcionados y de colores a elección, necesita GD.
* Clase programada por Infinit.
*
*************************************************/


class GenerateGraphic {

    var
$negro;
    var
$act_num;
    var
$image;
    var
$width;
    var
$height;
    var
$color = array();
    var
$bars = array();

    
// Constructor
    
function GenerateGraphic($width, $height, $act_num = 1) {
        
header("Content-type: image/png");
        if (
$width > 0 || $height > 0) {
            
$this->width = $width;
            
$this->height = $height;
            
$this->image = ImageCreate($this->width, $this->height);
            
$this->act_num = $act_num;
        } else {
            
// En el caso de que la imagen sea 0x0 muestra error
            
trigger_error('La imagen debe ser mayor a 0 x 0', E_USER_WARNING);
            return;
        }
        
        
// En el caso de que la grafica sea muy pequeña no mostramos los datos porque queda feo
        
if (($width < 150) || ($height < 150)) $this->act_num = 0;

        
$this->negro = ImageColorAllocate($this->image, 0, 0, 0);
    }

    
// Funcion que dado un valor crea una barra
    
function AddBar($value) {
        
$this->bars[] = (int)$value;
    }
    
    
// Funcion que añade colores para ser utilizado en las barras
    
function AddColor($r, $g, $b) {
        if ((
$r < 0) || ($r > 255) || ($g < 0) || ($g > 255) || ($b < 0) || ($b > 255)) {
            
trigger_error('Color invalido', E_USER_WARNING);
            return;
        } else {
            
$this->color[] = ImageColorAllocate($this->image, $r, $g, $b);
        }
    }

    
// La funcion mas importante, genera la grafica.
    
function generate() {
        
// Hayamos el 10% del ancho y del alto para dejar un margen
        
$_10x100w = round(($this->width * 10) / 100);
        
$_10x100h = round(($this->height * 10) / 100);

        if ((
$countb = sizeof($this->bars)) == 0) {
            
// Generamos un error y cortamos la funcion si no se ha agregado ninguna barra
            
trigger_error('El numero de barras debe ser mayor a 0', E_USER_WARNING);
            return;
        }

        if ((
$countc = sizeof($this->color)) == 0) {
            
// Generamos un error y cortamos la funcion si no se ha agregado ningun color
            
trigger_error('El numero de colores debe de ser mayor a 0', E_USER_WARNING);
            return;
        }

        
// Conseguimos la barra que tenga el valor maximo
        
$max_value = 0;
        for (
$i = 0; $i < $countb; $i++) {
            if (
$this->bars[$i] > $max_value) $max_value = $this->bars[$i];
        }
        
        
$sizeb = round(($this->width - ($_10x100w * 2)) / $countb);
        
$div = ($this->height - ($_10x100h * 2)) / $max_value;
        
$c = 0;
        
$color = true;
        
        
// Alternamos dos colores para crear un fondo que ayude a diferenciar los valores de cada barra
        
for ($i = 0; $i < round($this->height / 20); $i++) {
            if (
$color) {
                
$r = 230; $g = 230; $b = 230; $color = false;
            } else {
                
$r = 200; $g = 200; $b = 200; $color = true;
            }
            
            
ImageFilledRectangle($this->image, 0, ($i * 20), $this->width, ($i * 20 + 20), ImageColorAllocate($this->image, $r, $g, $b));
        }

        
// Borde
        
ImageRectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $this->negro);

        for (
$i = 0; $i < $countb; $i++) {
            if (
$c == sizeof($this->color) - 1) $c = 0; else $c++;

            
$start_x = $_10x100w + ($i * $sizeb);
            
$end_x = $start_x + $sizeb;
            
$end_y = $this->height - $_10x100h;
            
$start_y = $end_y - ($this->bars[$i] * $div);
            
            
// Damos borde negro de 1px a cada barra
            
ImageRectangle($this->image, $start_x, $start_y, $end_x, $end_y , $this->negro);
            
// Dibujamos la barra rellena con el color que le toque
            
ImageFilledRectangle($this->image, $start_x + 1, $start_y + 1 , $end_x - 1, $end_y - 1, $this->color[$c]);

            
// Mostramos los valores en el caso de que se haya elegido esa opcion
            
if ($this->act_num) {
                
ImageString($this->image, 2, (($end_x + $start_x) / 2) - (strlen($this->bars[$i]) * 3), $start_y - 15, $this->bars[$i], $this->negro);
            }
        }

        
// Mostramos la imagen finalizada
        
ImagePng($this->image);

        
// Destruimos la imagen
        
ImageDestroy($this->image);
    }
}

// Ejemplo de uso:
// Creamos la imagen marcando el ancho, alto y si queremos mostrar los valores
$obj = new GenerateGraphic(149, 150, 1);

// Añadimos tantas barras como queramos y minimo uno.
$obj->AddBar(452);
$obj->AddBar(100);
$obj->AddBar(144);
$obj->AddBar(346);
$obj->AddBar(15);
$obj->AddBar(462);
$obj->AddBar(154);
//Agregamos los colores que deseemos, como minimo uno.
$obj->AddColor(122, 155, 122);
$obj->AddColor(0, 155, 122);

// Generamos la grafica
$obj->generate();

?>
PHP-Hispano.net - Porque al final, todos acabamos aprendiendo.