Foros del Web » Programando para Internet » PHP »

Aporte: Mapa del tiempo en GD

Estas en el tema de Aporte: Mapa del tiempo en GD en el foro de PHP en Foros del Web. Hola a todos aqui tengo un aporte para generar un mapa del tiempo a partir de 2 XML-s. Primero os digo la estructura de carpetas: ...
  #1 (permalink)  
Antiguo 16/05/2008, 16:47
Avatar de ZiTAL  
Fecha de Ingreso: marzo-2004
Ubicación: Bermio (Bizkaia)
Mensajes: 1.545
Antigüedad: 20 años, 1 mes
Puntos: 62
Aporte: Mapa del tiempo en GD

Hola a todos aqui tengo un aporte para generar un mapa del tiempo a partir de 2 XML-s.

Primero os digo la estructura de carpetas:

Código:
img/icon <- aqui tendremos los iconos para insertar en la imagen
img <- aqui tendremos la imagen con el mapa
output <- carpeta donde iran a parar las imagenes generadas, debe tener permisos de escritura
xml <- carpeta donde estan los XML que leeremos para el proceso.
. <- carpeta raiz donde ira el modulo correspondiente a este ejercicio ;)
XML-s:
1 estatico, vamos que siempre va a ser el mismo, el cual nos indica en que posiciones deben de ir los iconos. Y otro XML dinamico el cual nos dice que tiempo hace en cada sitio:

static.xml
Código:
<?xml version="1.0" encoding="utf-8"?>
<static>
	<ciudad id="1" x="90" y="35" es="La Coruña" gl="A Coruña"/>
	<ciudad id="2" x="145" y="80" es="Lugo" gl="Lugo"/>
	<ciudad id="3" x="50" y="125" es="Pontevedra" gl="Pontevedra"/>
	<ciudad id="4" x="105" y="150" es="Orense" gl="Ourense"/>
</static>
dinamic.xml
Código:
<?xml version="1.0" encoding="utf-8"?>
<dinamic>
	<ciudad id="1" icon="iconoCubierto"/>
	<ciudad id="2" icon="iconoChubascos"/>
	<ciudad id="3" icon="iconoDespejado"/>
	<ciudad id="4" icon="iconoGranizo"/>
</dinamic>
Aqui la clase:

gd.module.php
Código PHP:
<?php
/*
 * 
 * Extensiones soportadas: png, jpeg, jpg, gif
 * 
 */
class xml2img
 
{
     public 
$staticXmlpath// path de los xml
     
public $staticXmlfile// nombre de fichero de los xml
     
public $staticNodename// nombre del nodo en el xml
     
     
public $dinamicXmlpath// path de los xml
     
public $dinamicXmlfile// nombre de fichero de los xml
     
public $dinamicNodename// nombre del nodo en el xml     
     
     
public $imgpath// path de las imagenes
     
public $imgfile// nombre de la imagen en la cual se dibujaran las demas
     
public $iconpath// path donde estaran los iconos
     
public $iconext// extension para los iconos
     
     
public $savepath// path donde se guardara la imagen
     
public $filename// nombre de la imagen    
     
     
private $img// imagen generada
     
     
public function XmlToImg()
      {                    
          if(@!
$staticxml=simplexml_load_file($this->staticXmlpath.$this->staticXmlfile)) // cargar xml
           
{
               echo 
"Failed loading: ".$this->staticXmlpath.$this->staticXmlfile." file.";
               return;
           }
          if(@!
$dinamicxml=simplexml_load_file($this->dinamicXmlpath.$this->dinamicXmlfile)) // cargar xml
           
{
               echo 
"Failed loading: ".$this->dinamicXmlpath.$this->dinamicXmlfile." file.";
               return;
           }           
          
$extension=$this->getExtension($this->imgpath.$this->imgfile);
          switch(
$extension)
           {
               case 
"png":
                  if(!
$this->img=imagecreatefrompng($this->imgpath.$this->imgfile))    // cargar img
                   
{
                       echo 
"Failed loading: ".$this->imgpath.$this->imgfile." file.";
                       return;
                   }                          
                   break;
               case 
"jpg":
               case 
"jpeg":
                  if(@!
$this->img=imagecreatefromjpeg($this->imgpath.$this->imgfile))    // cargar img
                   
{
                       echo 
"Failed loading: ".$this->imgpath.$this->imgfile." file.";
                       return;
                   }               
                   break;
               case 
"gif":
                  if(@!
$this->img=imagecreatefromgif ($this->imgpath.$this->imgfile))    // cargar img
                   
{
                       echo 
"Failed loading: ".$this->imgpath.$this->imgfile." file.";
                       return;
                   }                 
                   break;
               default:
                   echo 
"Invalid Image type: ".$this->imgpath.$this->imgfile;
                   return;                   
           } 
          
imagealphablending($this->img,true);    // Guardar el fondo transparente      
          
imagesavealpha($this->img,true);         // Guardar el fondo transparente
        // Recorrer XML
        
$tmpnode=$this->staticNodename;
          foreach(
$staticxml->$tmpnode as $node
          {    
              
$xmlarray['id']=$node['id'];              
              
$xmlarray['x']=$node['x'];
              
$xmlarray['y']=$node['y'];
              
$xmlarray['icon']=$this->getIcon($node['id'],$dinamicxml);
              
$this->img=$this->PasteIcon($this->img,$xmlarray);
              unset(
$xmlarray);
          }          
         
// Copiar imagen generada en el path indicado
         
if(!imagepng($this->img,$this->savepath.$this->filename))
          {
              echo 
"Failed copying generated image ".$this->filename."in ".$this->savepath;
          }
      } 
     
// funcion para obtener extension
     
private function getExtension($file)
      {
          return 
strtolower(substr(strrchr($file,"."),1,strlen(strrchr($file,"."))));
      }
     
// funcion para recorren el xml en busca del icono
     
private function getIcon($id,$dinamicxml)
      {
          
$tmpnode=$this->dinamicNodename;
          foreach(
$dinamicxml->$tmpnode as $node
          {    
              if(
$node['id']=="$id")
               {
                   return 
$node['icon'];
               }              
          }                  
      }
     
// funcion para pegar el icono en la imagen
     
private function PasteIcon($img,$xmlarray)
      {                                        
              
$this->iconext=strtolower($this->iconext);
              
$icon=$this->iconpath.$xmlarray['icon'].".".$this->iconext;
              
$size=getimagesize($icon);                                  
               switch(
$this->iconext)
                {
                    case 
"png":
                      if(@!
$icon=imagecreatefrompng($icon))    // cargar icono
                       
{
                           echo 
"Failed getting icon image ".$xmlarray['icon'].$this->icoext;
                           return;
                       }                          
                       break;                         
                    case 
"jpg":
                    case 
"jpeg":
                      if(@!
$icon=imagecreatefromjpeg($icon))    // cargar icono
                       
{
                           echo 
"Failed getting icon image ".$xmlarray['icon'].$this->icoext;
                           return;
                       }                    
                        break;
                    case 
"gif":
                      if(@!
$icon=imagecreatefromgif($icon))    // cargar icono
                       
{
                           echo 
"Failed getting icon image ".$xmlarray['icon'].$this->icoext;
                           return;
                       }                    
                        break;
                    default:
                        echo 
"Failed getting icon image ".$xmlarray['icon'].$this->icoext;
                        return;
                        break;
                }          
               
imagecopy($img,$icon,$xmlarray['x'],$xmlarray['y'],0,0,$size[0],$size[1]);
               
imagedestroy($icon);
               return 
$img;           
      }
     public function 
Show()
      {
          
$tmpext=$this->getExtension($this->imgfile);
          
header('Cache-Control: no-store, no-cache, must-revalidate');
          
header("Content-Type: image/".$tmpext);
          
$tmpimg=imagepng($this->img);
          echo 
$tmpimg;     
          
destroy($tmpimg);      
      }
 }
?>
Aqui llamamos a la clase para que haga su trabajo ;)
index.php

Código PHP:
<?php
include("gd.module.php");

    
$date=date("Ymd"); // fecha de hoy

    
$a= new xml2img();
    
    
$a->staticXmlpath="xml/"//path xml estatico
    
$a->staticXmlfile="static.xml"// nombre xml estatico
    
$a->staticNodename="ciudad"// nombre del nodo en el xml estatico
    
    
$a->dinamicXmlpath="xml/"//path xml dinamico
    
$a->dinamicXmlfile="dinamic.xml"// nombre xml dinamico
    
$a->dinamicNodename="ciudad"// nombre del nodo en el xml dinamico    
    
    
$a->imgpath="img/"// path imagenes
    
$a->imgfile="mapa.png"// nombre imagen         
    
$a->iconpath="img/icon/"// path iconos
    
$a->iconext="png"// extension para los iconos
    
    
$a->savepath="output/"// path de salida
    
$a->filename="mapa".$date.".png"// nombre fichero salida
    
    
$a->XmlToImg(); // ejecutar transformacion
    
$a->Show(); // mostrar imagen    
?>
Descarga: http://zital.no-ip.org/gd/gd.tar.bz2
__________________
http://zital.no-ip.org
____________________

Euskerie ahuen eta bijotzan
  #2 (permalink)  
Antiguo 16/05/2008, 18:27
Avatar de jaronu  
Fecha de Ingreso: febrero-2008
Mensajes: 2.183
Antigüedad: 16 años, 2 meses
Puntos: 52
Respuesta: Aporte: Mapa del tiempo en GD

Muy bueno zital

lo provare, ahora no puedo, pero ya te contare.

Gracias por el aporte.
  #3 (permalink)  
Antiguo 16/05/2008, 22:38
Avatar de SidP  
Fecha de Ingreso: febrero-2006
Ubicación: /etc/php.ini
Mensajes: 129
Antigüedad: 18 años, 2 meses
Puntos: 2
Respuesta: Aporte: Mapa del tiempo en GD

humm interesante......vamos a ver que tal funciona...buen aporte

Salu2

Última edición por SidP; 16/05/2008 a las 22:40 Razón: aporte
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta

SíEste tema le ha gustado a 1 personas




La zona horaria es GMT -6. Ahora son las 11:56.