Ver Mensaje Individual
  #4 (permalink)  
Antiguo 14/02/2008, 02:08
Avatar de ZiTAL
ZiTAL
 
Fecha de Ingreso: marzo-2004
Ubicación: Bermio (Bizkaia)
Mensajes: 1.545
Antigüedad: 20 años, 2 meses
Puntos: 62
Re: Libreria GD: Problema con transparecias PNG

Pues no no funciona, de todas pondre el codigo para que veais como lo hace:

- Hay 2 XML, uno estatico y otro dinamico:
- XML estatico: Guarda las ID's y las posiciones X e Y de las ciudades
- XML dinamico: Guardar las ID's y el nombre del icono del tiempo.
- Imagen principal.
- El programa recorre el xml estatico y por cada ID recorre el XML dinamico y coge el valor del tiempo y pinta el icono del tiempo en las coordenadas correspondientes.

Estructura de directorio:

raiz: / : index.php y gd.module
img: /img : imagenes principales
icon: /img/icon : imagenes de los iconos del tiempo.
xml: /xml : ruta de los xml's
outpur: /output : path para los archivos generados.

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    
?>
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);          
          
imagesavealpha($this->img,true);
        
// 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);      
      }
 }
?>
antes de recorrer el xml hago un

Código PHP:
imagealphablending($this->img,true); 
pero no funciona :(

Edito se me olvido poner los XML:

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>
vamos que habra en la carpeta icons:

- iconoCubierto.png
- iconoChubascos.png
- iconoDespejado.png
- iconoGranizo.png
__________________
http://zital.no-ip.org
____________________

Euskerie ahuen eta bijotzan

Última edición por ZiTAL; 14/02/2008 a las 10:08