Ver Mensaje Individual
  #3 (permalink)  
Antiguo 25/09/2010, 19:11
sandinosaso
 
Fecha de Ingreso: julio-2008
Mensajes: 208
Antigüedad: 15 años, 9 meses
Puntos: 5
Respuesta: Subir varias imagen a carpeta y URL a BD con PHP y crear thumbs - Ayuda

Facil...

Pon esta funcion al inicio de tu codigo:

function resize($img, $thumb_width, $newfilename)
{
$max_width=$thumb_width;

//Check if GD extension is loaded
if (!extension_loaded('gd') && !extension_loaded('gd2'))
{
trigger_error("GD is not loaded", E_USER_WARNING);
return false;
}

//Get Image size info
list($width_orig, $height_orig, $image_type) = getimagesize($img);

switch ($image_type)
{
case 1: $im = imagecreatefromgif($img); break;
case 2: $im = imagecreatefromjpeg($img); break;
case 3: $im = imagecreatefrompng($img); break;
default: trigger_error('Unsupported filetype!', E_USER_WARNING); break;
}

/*** calculate the aspect ratio ***/
$aspect_ratio = (float) $height_orig / $width_orig;

/*** calulate the thumbnail width based on the height ***/
$thumb_height = round($thumb_width * $aspect_ratio);


while($thumb_height>$max_width)
{
$thumb_width-=10;
$thumb_height = round($thumb_width * $aspect_ratio);
}

$newImg = imagecreatetruecolor($thumb_width, $thumb_height);

/* Check if this image is PNG or GIF, then set if Transparent*/
if(($image_type == 1) OR ($image_type==3))
{
imagealphablending($newImg, false);
imagesavealpha($newImg,true);
$transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
imagefilledrectangle($newImg, 0, 0, $thumb_width, $thumb_height, $transparent);
}
imagecopyresampled($newImg, $im, 0, 0, 0, 0, $thumb_width, $thumb_height, $width_orig, $height_orig);

//Generate the file, and rename it to $newfilename
switch ($image_type)
{
case 1: imagegif($newImg,$newfilename); break;
case 2: imagejpeg($newImg,$newfilename); break;
case 3: imagepng($newImg,$newfilename); break;
default: trigger_error('Failed resize image!', E_USER_WARNING); break;
}

return $newfilename;
}


y Luego en tu codigo te quedara asi:

Código:
<?php


include('../co/conectame.php');
    
 
function resize($img, $thumb_width, $newfilename)
{
$max_width=$thumb_width;

//Check if GD extension is loaded
if (!extension_loaded('gd') && !extension_loaded('gd2'))
{
trigger_error("GD is not loaded", E_USER_WARNING);
return false;
}

//Get Image size info
list($width_orig, $height_orig, $image_type) = getimagesize($img);

switch ($image_type)
{
case 1: $im = imagecreatefromgif($img); break;
case 2: $im = imagecreatefromjpeg($img); break;
case 3: $im = imagecreatefrompng($img); break;
default: trigger_error('Unsupported filetype!', E_USER_WARNING); break;
}

/*** calculate the aspect ratio ***/
$aspect_ratio = (float) $height_orig / $width_orig;

/*** calulate the thumbnail width based on the height ***/
$thumb_height = round($thumb_width * $aspect_ratio);


while($thumb_height>$max_width)
{
$thumb_width-=10;
$thumb_height = round($thumb_width * $aspect_ratio);
}

$newImg = imagecreatetruecolor($thumb_width, $thumb_height);

/* Check if this image is PNG or GIF, then set if Transparent*/
if(($image_type == 1) OR ($image_type==3))
{
imagealphablending($newImg, false);
imagesavealpha($newImg,true);
$transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
imagefilledrectangle($newImg, 0, 0, $thumb_width, $thumb_height, $transparent);
}
imagecopyresampled($newImg, $im, 0, 0, 0, 0, $thumb_width, $thumb_height, $width_orig, $height_orig);

//Generate the file, and rename it to $newfilename
switch ($image_type)
{
case 1: imagegif($newImg,$newfilename); break;
case 2: imagejpeg($newImg,$newfilename); break;
case 3: imagepng($newImg,$newfilename); break;
default: trigger_error('Failed resize image!', E_USER_WARNING); break;
}

return $newfilename;
}
                
                 
   //Preguntamos si nuetro arreglo 'archivos' fue definido
         if (isset($_FILES["archivos"])) {
             
        
         //de se asi, para procesar los archivos subidos al servidor solo debemos recorrerlo
         //obtenemos la cantidad de elementos que tiene el arreglo archivos
         $tot = count($_FILES["archivos"]["name"]);

         //este for recorre el arreglo
         for ($i = 0; $i < $tot; $i++){
             
             //inserto los textos en la base de datos
             $sql = "INSERT INTO galeria_arte (big) VALUES (";    
                                                    
            if(is_uploaded_file($_FILES["archivos"]["tmp_name"][$i])){
         //con el indice $i, podremos obtener la propiedad que desemos de cada archivo
         //para trabajar con este como si fuera un array continuo
            $tmp_name = $_FILES["archivos"]["tmp_name"][$i];
            $name = $_FILES["archivos"]["name"][$i];
            $tamano = $_FILES["archivos"]["size"][$i];
            $tipo = $_FILES["archivos"]["type"][$i]; 
            
            
                    if (!((strpos($tipo, "gif") || strpos($tipo, "png")|| strpos($tipo, "jpeg")) && ($tamano < 500000))) {
                    echo '<div style="width:500px; height:80px; padding:10px; background-color:#FF0000;">';
                    echo '<p style="font-size:15px; color:#FFF;"><strong>ERROR DE FORMATO O TAMANHO</strong></p></div>';
                    
                    }else{

                            /* Guardar el archivo */
                            $destino="../galerias/fotos/";
                            $destino_thumb="../galerias/thumbs/";
                            $width_thumb=120;
                            resize($destino.$name,$width_thumb, $destino_thumb.$name);
                            if(move_uploaded_file($_FILES["archivos"]["tmp_name"][$i],$destino.$name)){
                            
                                $sql.= "'".$name."'";
                        }//if move_uploaded_file
                        
                    }//else
                    
                }//if is_uploaded_file
                
                $sql.=")"; //cierro mi consulta $sql
                 mysql_query($sql,$fausto_conn) or die(mysql_error($fausto_conn)); 
            }//for
            
        
      } //if
          
     
?>
Eso es todo solo agregue a tu codigo:

$destino_thumb="../galerias/thumbs/";
$width_thumb=120;
resize($destino.$name,$width_thumb, $destino_thumb.$name);

Deberias agregar la carpeta llamada thumbs en tu servidor.
La funcion resize ya te la guarda a la imagen en esa carpeta con el mismo nombre que la foto original y un ancho maximo de 120 , el alto lo calcula proporcionalmente de forma de no distorcionar la imagen.

Comenta como te fue..
Saludos.

Última edición por sandinosaso; 25/09/2010 a las 19:30