Ver Mensaje Individual
  #1  
Antiguo 18-05-2010
CniL CniL is offline
Miembro
 
Registrado: may 2010
Posts: 10
Reputación: 0
CniL Va por buen camino
Post Como subir imagenes de tamaño definido, transparentes o no. PNG, JPG y GIF

Bueno, no me atribuyo la autoría de este código, si compilarlo, ya que son piezas de varios códigos y me ayuda bastante a la hora de crear servicios de subida de imágenes donde el cliente no tenga que preocuparse más que de subir el archivo.

Código PHP:
    if($_FILES['*NOMBRE DE VARIABLE DEL POST*']['name'])
        {
        
$tmpname=$_FILES['logo']['tmp_name'];
        
$datos = getimagesize($tmpname);
//LOS NOMBRES SON ESTANDAR SEGÚN FECHA, de modo tal no se repitan los nombres (Tambien pueden dejar los nombres originales)
        
if($datos[2]==1){$nombre="img-".date("Y-m-d-H-i-s")."1.gif";}
        if(
$datos[2]==2){$nombre="img-".date("Y-m-d-H-i-s")."1.jpg";}
        if(
$datos[2]==3){$nombre="img-".date("Y-m-d-H-i-s")."1.png";}
//AHORA LLAMO LA FUNCIÓN
        
$datos2=crearimagen($tmpname,*TAMAÑO FIJO X,TAMAÑO FIJO Y,"../imagenes",$nombre);
        
$evento="./imagenes/".$datos2;
//acá ya puedo subir a una base de datos el nombre de la imagen
        
}

// ESTA ES LA FUNCIÓN
function crearimagen($imagen,$anchura,$altura,$directorio,$nombre)
    {
    
$datos = getimagesize($imagen);
    if(
$datos[2]==1){$img = @imagecreatefromgif($imagen);}
    if(
$datos[2]==2){$img = @imagecreatefromjpeg($imagen);}
    if(
$datos[2]==3){$img = @imagecreatefrompng($imagen);}
    
$thumb = imagecreatetruecolor($anchura,$altura); 
    if(
$datos[2]==3)
        {
        
imagealphablending($thumb, false);
        
imagesavealpha($thumb, true);
        }
    
$colorTransparancia=imagecolortransparent($img);
    if(
$colorTransparancia!=-1)
        {
        
$colorTransparente = imagecolorsforindex($img, $colorTransparancia);
        
$idColorTransparente = imagecolorallocatealpha($thumb, $colorTransparente['red'], $colorTransparente['green'], $colorTransparente['blue'], $colorTransparente['alpha']);
        
imagefill($thumb, 0, 0, $idColorTransparente);
        
imagecolortransparent($thumb, $idColorTransparente);
        }
    
imagecopyresized($thumb, $img, 0, 0, 0, 0, $anchura, $altura, $datos[0], $datos[1]);
    if(
$datos[2]==1){imagegif($thumb, "./".$directorio."/".$nombre);}
    if(
$datos[2]==2){imagejpeg($thumb, "./".$directorio."/".$nombre, 75);}
    if(
$datos[2]==3){imagepng($thumb, "./".$directorio."/".$nombre); }
    
unlink($imagen);
    return(
$nombre);
    } 
Responder Con Cita