Ver Mensaje Individual
  #2 (permalink)  
Antiguo 09/03/2008, 17:27
Surfiction
 
Fecha de Ingreso: enero-2008
Mensajes: 132
Antigüedad: 16 años, 3 meses
Puntos: 6
Re: Como redimensionar imagenes con php

Código PHP:
<?
    $tam
=getimagesize("imagen.png"); 
    if(
$tam[0] > 500 OR $tam[1] > 500)
        {
        
cambiartam("imagen.png""nuevaimagen.png"200200);
        }

function 
cambiartam($nombre,$archivo,$ancho,$alto)
    {
    
$tmp=split(".",$nombre)[1];

    if (
preg_match('/jpg|jpeg|JPG/',$tmp))
        {
        
$imagen=imagecreatefromjpeg($nombre);
        }
    if (
preg_match('/png|PNG/',$tmp))
        {
        
$imagen=imagecreatefrompng($nombre);
        }
    if (
preg_match('/gif|GIF/',$tmp))
        {
        
$imagen=imagecreatefromgif($nombre);
        }

    
$x=imageSX($imagen);
    
$y=imageSY($imagen);

    if (
$x $y
        {
        
$w=$ancho;
        
$h=$y*($alto/$x);
        }

    if (
$x $y
        {
        
$w=$x*($ancho/$y);
        
$h=$alto;
        }

    if (
$x == $y
        {
        
$w=$ancho;
        
$h=$alto;
        }


    
$destino=ImageCreateTrueColor($w,$h);
    
imagecopyresampled($destino,$imagen,0,0,0,0,$w,$h,$x,$y); 


    if (
preg_match("/png/",$tmp))
        {
        
imagepng($destino,$archivo); 
        } 
    if (
preg_match("/gif/",$tmp))
        {
        
imagegif($destino,$archivo);
        }
    else 
        {
        
imagejpeg($destino,$archivo); 
        }

    
imagedestroy($destino); 
    
imagedestroy($imagen); 
}

?>