Foros del Web » Programando para Internet » PHP »

Error redimensionando imagenes png subidas al servidor.. funcion imagecolorsforindex

Estas en el tema de Error redimensionando imagenes png subidas al servidor.. funcion imagecolorsforindex en el foro de PHP en Foros del Web. Estoy haciendo una pagina web donde se suben imagenes desde un administrador (creado por mi), estas imagenes son redimensionadas para ser mostradas en distintas partes ...
  #1 (permalink)  
Antiguo 26/04/2012, 15:10
Avatar de HackGhost  
Fecha de Ingreso: marzo-2012
Ubicación: En la pesadilla de mis enemigos
Mensajes: 114
Antigüedad: 12 años, 1 mes
Puntos: 23
Pregunta Error redimensionando imagenes png subidas al servidor.. funcion imagecolorsforindex

Estoy haciendo una pagina web donde se suben imagenes desde un administrador (creado por mi), estas imagenes son redimensionadas para ser mostradas en distintas partes de la pagina.. el problema que tengo es que me aparece un error (o mas bien una advertencia) al momento en que una imagen .png es redimensionado..

Este es el mensaje:

Warning: imagecolorsforindex() [function.imagecolorsforindex]: Color index -1 out of range in C:\xampp\htdocs\educador\admin\upload.class.php on line 105


upload.class.php
Código PHP:
Ver original
  1. <?php
  2. class upload
  3. {
  4.     var $directory_name;
  5.     var $max_filesize;
  6.     var $error;
  7.  
  8.     var $user_tmp_name;
  9.     var $user_file_name;
  10.     var $user_file_size;
  11.     var $user_file_type;
  12.     var $user_full_name;
  13.     var $thumb_name;
  14.  
  15.     function set_directory($dir_name = "")
  16.     {
  17.      $this->directory_name = $dir_name;
  18.     }
  19.  
  20.     function set_max_size($max_file = 300000)
  21.     {
  22.      $this->max_filesize = $max_file;
  23.     }
  24.  
  25.     function error()
  26.     {
  27.      return $this->error;
  28.     }
  29.  
  30.     function is_ok()
  31.     {
  32.      if(isset($this->error))
  33.       return FALSE;
  34.      else
  35.       return TRUE;
  36.     }
  37.  
  38.     function set_tmp_name($temp_name)
  39.     {
  40.      $this->user_tmp_name = $temp_name;  
  41.     }
  42.  
  43.     function set_file_size($file_size)
  44.     {
  45.      $this->user_file_size = $file_size;
  46.     }
  47.  
  48.     function set_file_type($file_type)
  49.     {
  50.      $this->user_file_type = $file_type;     
  51.     }
  52.  
  53.     function set_file_name($file)
  54.     {
  55.         $this->user_file_name = $file;
  56.         $this->user_full_name = $this->directory_name."/".$this->user_file_name;
  57.     }
  58.  
  59.     function resize($max_width = 0, $max_height = 0 )
  60.     {
  61.     if(eregi("\.png$",$this->user_full_name))
  62.     {
  63.      $img = imagecreatefrompng($this->user_full_name);
  64.     }
  65.    
  66.     if(eregi("\.(jpg|jpeg)$",$this->user_full_name))
  67.     {
  68.      $img = imagecreatefromjpeg($this->user_full_name);
  69.     }
  70.    
  71.     if(eregi("\.gif$",$this->user_full_name))
  72.     {
  73.      $img = imagecreatefromgif($this->user_full_name);
  74.     }
  75.  
  76.         $FullImage_width = imagesx ($img);    
  77.         $FullImage_height = imagesy ($img);  
  78.  
  79.         if(isset($max_width) && isset($max_height) && $max_width != 0 && $max_height != 0)
  80.         {
  81.          $new_width = $max_width;
  82.          $new_height = $max_height;
  83.         }
  84.         else if(isset($max_width) && $max_width != 0)
  85.         {
  86.          $new_width = $max_width;
  87.          $new_height = ((int)($new_width * $FullImage_height) / $FullImage_width);
  88.         }
  89.         else if(isset($max_height) && $max_height != 0)
  90.         {
  91.          $new_height = $max_height;
  92.          $new_width = ((int)($new_height * $FullImage_width) / $FullImage_height);
  93.         }      
  94.         else
  95.         {
  96.          $new_height = $FullImage_height;
  97.          $new_width = $FullImage_width;
  98.         }  
  99.  
  100.         $full_id =  imagecreatetruecolor( $new_width , $new_height );
  101.         // Check transparent gif and pngs
  102.     if(eregi("\.png$",$this->user_full_name) || eregi("\.gif$",$this->user_full_name))
  103.         {
  104.             $trnprt_indx = imagecolortransparent($img);
  105.             $trnprt_color = imagecolorsforindex($img, $trnprt_indx);
  106.             $trnprt_indx = imagecolorallocate($full_id, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
  107.             imagefill($full_id, 0, 0, $trnprt_indx);
  108.             imagecolortransparent($full_id, $trnprt_indx);
  109.         }
  110.         imagecopyresampled( $full_id, $img, 0,0,0,0, $new_width, $new_height, $FullImage_width, $FullImage_height );
  111.        
  112.  
  113.         if(eregi("\.(jpg|jpeg)$",$this->user_full_name))
  114.         {
  115.          $full = imagejpeg( $full_id, $this->user_full_name,100);
  116.         }
  117.        
  118.         if(eregi("\.png$",$this->user_full_name))
  119.         {
  120.          $full = imagepng( $full_id, $this->user_full_name);
  121.         }
  122.        
  123.         if(eregi("\.gif$",$this->user_full_name))
  124.         {
  125.          $full = imagegif($full_id, $this->user_full_name);
  126.         }
  127.         imagedestroy( $full_id );
  128.         unset($max_width);
  129.         unset($max_height);
  130.     }
  131.  
  132.     function start_copy()
  133.     {
  134.         if(!isset($this->user_file_name))
  135.          $this->error = "You must define filename!";
  136.  
  137.         if ($this->user_file_size <= 0)
  138.          $this->error = "File size error (0): $this->user_file_size KB<br>";
  139.  
  140.         if ($this->user_file_size > $this->max_filesize)
  141.          $this->error = "File size error (1): $this->user_file_size KB<br>";
  142.  
  143.         if (!isset($this->error))
  144.         {
  145.             $filename = basename($this->user_file_name);
  146.  
  147.             if (!empty($this->directory_name))
  148.                 $destination = $this->user_full_name;
  149.             else
  150.                 $destination = $filename;
  151.                
  152.             if(!is_uploaded_file($this->user_tmp_name))
  153.              $this->error = "File ".$this->user_tmp_name." is not uploaded correctly.";
  154.      
  155.             if (!@move_uploaded_file ($this->user_tmp_name,$destination))
  156.              $this->error = "Impossible to copy ".$this->user_file_name." from $userfile to destination directory.";
  157.         }
  158.     }
  159.    
  160.     function set_thumbnail_name($thumbname)
  161.     {
  162.     if(eregi("\.png$",$this->user_full_name))
  163.      $this->thumb_name = $this->directory_name."/".$thumbname;
  164.     if(eregi("\.(jpg|jpeg)$",$this->user_full_name))
  165.      $this->thumb_name = $this->directory_name."/".$thumbname;
  166.     if(eregi("\.gif$",$this->user_full_name))
  167.      $this->thumb_name = $this->directory_name."/".$thumbname;
  168.     }
  169.    
  170.     function create_thumbnail()
  171.     {
  172.      if (!copy($this->user_full_name, $this->thumb_name))
  173.       {
  174.       echo "<br>".$this->user_full_name.", ".$this->thumb_name."<br>";
  175.        echo "failed to copy $file...<br />\n";
  176.       }
  177.     }
  178.    
  179.     function set_thumbnail_size($max_width = 0, $max_height = 0 )
  180.     {
  181.     if(eregi("\.png$",$this->thumb_name))
  182.     {
  183.      $img = ImageCreateFromPNG ($this->thumb_name);
  184.     }
  185.    
  186.     if(eregi("\.(jpg|jpeg)$",$this->thumb_name))
  187.     {
  188.      $img = ImageCreateFromJPEG ($this->thumb_name);
  189.     }
  190.    
  191.     if(eregi("\.gif$",$this->thumb_name))
  192.     {
  193.      $img = ImageCreateFromGif ($this->thumb_name);
  194.     }
  195.  
  196.         $FullImage_width = imagesx ($img);    
  197.         $FullImage_height = imagesy ($img);
  198.        
  199.         if(isset($max_width) && isset($max_height) && $max_width != 0 && $max_height != 0)
  200.         {
  201.          $new_width = $max_width;
  202.          $new_height = $max_height;
  203.         }
  204.         else if(isset($max_width) && $max_width != 0)
  205.         {
  206.          $new_width = $max_width;
  207.          $new_height = ((int)($new_width * $FullImage_height) / $FullImage_width);
  208.         }
  209.         else if(isset($max_height) && $max_height != 0)
  210.         {
  211.          $new_height = $max_height;
  212.          $new_width = ((int)($new_height * $FullImage_width) / $FullImage_height);
  213.         }      
  214.         else
  215.         {
  216.          $new_height = $FullImage_height;
  217.          $new_width = $FullImage_width;
  218.         }  
  219.         $full_id =  ImageCreateTrueColor ( $new_width , $new_height );
  220.  
  221.         // Check transparent gif and pngs
  222.         if(eregi("\.png$",$this->user_full_name) || eregi("\.gif$",$this->user_full_name))
  223.             {
  224.                 $trnprt_indx = imagecolortransparent($img);
  225.                 $trnprt_color = imagecolorsforindex($img, $trnprt_indx);
  226.                 $trnprt_indx = imagecolorallocate($full_id, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
  227.                 imagefill($full_id, 0, 0, $trnprt_indx);
  228.                 imagecolortransparent($full_id, $trnprt_indx);
  229.             }
  230.         ImageCopyResampled ( $full_id, $img, 0,0,0,0, $new_width, $new_height, $FullImage_width, $FullImage_height );
  231.        
  232.  
  233.         if(eregi("\.(jpg|jpeg)$",$this->thumb_name))
  234.         {
  235.          $full = ImageJPEG( $full_id, $this->thumb_name,100);
  236.         }
  237.        
  238.         if(eregi("\.png$",$this->thumb_name))
  239.         {
  240.          $full = ImagePNG( $full_id, $this->thumb_name);
  241.         }
  242.        
  243.         if(eregi("\.gif$",$this->thumb_name))
  244.         {
  245.          $full = ImageGIF($full_id, $this->thumb_name);
  246.         }
  247.         ImageDestroy( $full_id );
  248.         unset($max_width);
  249.         unset($max_height);
  250.     }
  251. }
  252. ?>
  #2 (permalink)  
Antiguo 26/04/2012, 16:25
Avatar de Triby
Mod on free time
 
Fecha de Ingreso: agosto-2008
Ubicación: $MX->Gto['León'];
Mensajes: 10.106
Antigüedad: 15 años, 8 meses
Puntos: 2237
Respuesta: Error redimensionando imagenes png subidas al servidor.. funcion imagecolo

Lo que pasa es que no necesariamente todas las imágenes en PNG tienen transparencia. Si imagecolortransparent() devuelve -1, seguramente es el caso y bastará un if para aplicar ese código sólo cuando sea necesario.

P.D. eregi() es una función obsoleta y deberías usar preg_match() en su lugar, sólo teniendo en cuenta que los patrones cambian un poco: http://www.forosdelweb.com/f18/aport...s-pcre-646110/
__________________
- León, Guanajuato
- GV-Foto
  #3 (permalink)  
Antiguo 27/04/2012, 07:26
Avatar de HackGhost  
Fecha de Ingreso: marzo-2012
Ubicación: En la pesadilla de mis enemigos
Mensajes: 114
Antigüedad: 12 años, 1 mes
Puntos: 23
Respuesta: Error redimensionando imagenes png subidas al servidor.. funcion imagecolo

Entiendo lo que dices.. pero necesito un poco de ayuda ya que esta clase no la hice yo.. solo la estoy usando..

Bueno. por lo que veo el if que dices deberia ir inmediatamente despues de comprobar si la imagen es png o gif (linea 222).. osea en esta parte:
Código PHP:
Ver original
  1. if(eregi("\.png$",$this->user_full_name) || eregi("\.gif$",$this->user_full_name))
  2.             {
  3.                //aqui si la transparencia es mayor a 0 ejecuto lo que sigue
  4.                 $trnprt_indx = imagecolortransparent($img);//esta variable tengo que utilizar para el if
  5.                 $trnprt_color = imagecolorsforindex($img, $trnprt_indx);
  6.                 $trnprt_indx = imagecolorallocate($full_id, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
  7.                 imagefill($full_id, 0, 0, $trnprt_indx);
  8.                 imagecolortransparent($full_id, $trnprt_indx);
  9.            //fin del if
  10.             }

Eso fue lo que entendi.. favor corrigeme si me equivocoo..(soy algo nuevo en esto)
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




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