Ver Mensaje Individual
  #3 (permalink)  
Antiguo 12/07/2011, 20:05
Avatar de carlos_belisario
carlos_belisario
Colaborador
 
Fecha de Ingreso: abril-2010
Ubicación: Venezuela Maracay Aragua
Mensajes: 3.156
Antigüedad: 14 años
Puntos: 461
Respuesta: Problemas con UTF8 y la librería GD

ok la clase esta algo larga y como tube que hacer algo como una libreria porque es para generar el reporte pero en formato imagen el porque es algo muy largo de contar, aca te voy a colocar lo que llevo hecho del metodo
Código PHP:
Ver original
  1. public function cell($text, $align = 'L', $border = 0,$shadow = false,  $width = NULL, $height = 20)
  2.     {          
  3.         if(empty($width)){
  4.             $width = $this->_width;
  5.         }  
  6.            
  7.         $background = imagecolorallocate($this->_img, 0xFF, 0xFF, 0xFF);
  8.         $borderColor = imagecolorallocate($this->_img, 0x00, 0x00, 0x00);                          
  9.         if(self::$i === 0 || self::$nFillCell == self::$nFillCellActual){
  10.             // border top          
  11.             if(self::$nFillCell > 0 && self::$i === 0){
  12.                 $xTop1 = self::$_x - 5;
  13.                 $xTop2 = self::$_x + $width -5 -12;
  14.                 $yTop1 = $this->_y - $this->_fontSize; 
  15.                 self::$nFillCellActual = self::$nFillCell;
  16.                 self::$i = 0;
  17.             }                                      
  18.         }              
  19.         // border bottom
  20.         $xBottom1 = self::$_x -5;
  21.         $xBottom2 = self::$_x + $width -5 -12; 
  22.         $yBottom1 = $this->_y + 7;     
  23.         //border left          
  24.         $xLeft1 = self::$_x - 5;           
  25.         $yLeft1 = $this->_y - $this->_fontSize;
  26.         $yLeft2 = $this->_y + $height - $this->_fontSize;      
  27.         //border right
  28.         $xRight1 = self::$_x + $width - 5 -12;                 
  29.         $yRight1 = $this->_y - $this->_fontSize;
  30.         $yRight2 = $this->_y + $height - $this->_fontSize;     
  31.         // colocamos el borde
  32.         if($border > 0){
  33.             //top      
  34.             imageline($this->_img,$xTop1,$yTop1,$xTop2,$yTop1,$borderColor);
  35.             //bootom
  36.             imageline($this->_img,$xBottom1,$yBottom1,$xBottom2,$yBottom1,$borderColor);
  37.             //left
  38.             imagefilledrectangle($this->_img,$xLeft1,$yLeft1,$xLeft1,$yLeft2,$borderColor);
  39.             //right
  40.             $rec = imagefilledrectangle($this->_img,$xRight1,$yRight1,$xRight1,$yRight2,$borderColor);
  41.             self::$i++;
  42.         }else{
  43.             self::$nFillCell++;
  44.             self::$i = 0;
  45.         }                              
  46.         // Reemplace la ruta por la de su propia fuente
  47.         //$fuente = "helper/$this->_font.ttf";     
  48.         $fuente = "$this->_font.ttf";                
  49.         //$fuente = "$this->_font.ttf";
  50.         // Añadir algo de sombra al texto      
  51.         // Añadir el texto
  52.         if($align == 'L'){
  53.             $left = self::$_x;
  54.             $right = $this->_y + 3;
  55.         }elseif($align == 'C'){        
  56.             $var = imagettfbbox($this->_fontSize, 0, $fuente, $text);
  57.             $left = ($width - $var[2]) / 2;                                
  58.             $right = $this->_y + 3;        
  59.         }elseif($align == 'R'){        
  60.             $right = $this->_y + 3;
  61.             $var = imagettfbbox($this->_fontSize, 0, $fuente, $text);
  62.             $left = $width - ($var[2] + 15);             
  63.            
  64.         }      
  65.         else{
  66.             throw new FormatToImageGdException("Unsopport Align");
  67.         }
  68. //el dilema es aca, genera el texto bien, pero cuando le paso algo con ñ o tilde me sale el cuadrito
  69.         imagettftext($this->_img, $this->_fontSize, 0, $left, $right, $border, $fuente, $text);        
  70.         self::$_x = $xRight1 + 5;                  
  71.         if(self::$_x + 5 >= $this->_width){
  72.             self::$i++;                                            
  73.             $this->ln();
  74.             self::$_x = 12;
  75.         }  
  76.     }
los parametros son
$this->_img es la imagen creada con imagecreatetruecolor
$this->_fontSize es el tamaño de la letra, yo en la clase lo asigno con un metodo.
los demas parametros se llenan con los parametros que se le pasan al metodo, un ejemplo

Código PHP:
Ver original
  1. $gd->cell('Hola Mundo','L',0,false,110);
te deberia de dar la palabra hola mundo en una imagen, agregale un caracter Ñ y ve que te sale, no coloco toda la clase porque el problema esta en esta funcion, de hecho es la que mas he trabajado porque es la que he estado necesitando, luego cuando la termine la hare publica para los que necesiten aunque es algo inutil XD
__________________
aprende d tus errores e incrementa tu conocimientos
it's not a bug, it's an undocumented feature By @David
php the right way