Ver Mensaje Individual
  #3 (permalink)  
Antiguo 18/10/2009, 11:50
xfer2
(Desactivado)
 
Fecha de Ingreso: junio-2009
Mensajes: 256
Antigüedad: 14 años, 10 meses
Puntos: 1
Respuesta: [aporte] subir y redimencionar imagen parte I

parte III.

archivo que hace el proceso de agregar y editar imagen.

Código PHP:

    
include("includes/conexion.php");
    include(
"includes/resize.php");
    
    
$nombres $_POST['nombres'];
    
$apellidos $_POST['apellidos'];

    if(!isset(
$_POST['editar'])){
    
        
//preguntamos si el archivo imagen esta vacio
        
if (is_uploaded_file($_FILES['imagen']['tmp_name'])) {        
            
/*prefijo de cadena*/
            
$str "abcdefghijklmnopqrstuvwxyz1234567890";
            
$cad "";
            for(
$i=0;$i<12;$i++) {
                
$cad .= substr($str,rand(0,36),1);
            }         
            
#subiendo archivos al servidor 
            
$filetype $_FILES['imagen']['type'];
            
$imagen $_FILES['imagen']['name'];
            
$test explode(".",$imagen);
            
            
$size getimagesize($_FILES['imagen']['tmp_name']);//obtengo el tamaño de la imagen    
    
            //ruta del archivo
            
$path  "imagenes/"//crear la carpeta imagenes.
            
$path2 "thumbnail/"//crear la carpeta thumbnail.

                
if(strtoupper($test[1])=="JPG" || strtoupper($test[1])=="JPEG" || strtoupper($test[1])=="GIF" || strtoupper($test[1])=="PNG")
                {        
                    
$newfilename $cad.'.'.$test[1];
                    
move_uploaded_file($_FILES['imagen']['tmp_name'],$path.$newfilename); 
                    
$thb_filename "thb_".$newfilename;
                    
$thumb = new thumbnail($path.$newfilename);
                    
$thumb->size_width(172); //ancho de la imagen.
                    
$thumb->size_height(155); //alto de la imagen.
                    
$thumb->jpeg_quality(80); //calidad de imagen. es opcional.
                    
$thumb->save($path2.$thb_filename);
                    
                    
$sql  "insert into imagenes(nombres,apellidos,imagen,imagen_thb)";
                    
$sql .= "values('$nombres','$apellidos','$newfilename','$thb_filename')";
                    
                    
mysql_query($sql) or die(mysql_error());
                    
                    
header("Location:index.php");
                
                }else{
                    echo 
"<span class=form_reg_error>La imagen que intenta grabar, debe ser de formato JPG, GIF o PNG.</span>";
                    echo 
"<br />";
                    echo 
"<a href='javascript:history.back()'>&lt;&lt; Clic aqu' para regresar </a>";
                }
                
        
//Si no hay imagenes para agregar en foto usuario, procederemos a registrar los datos sin imagen:        
        
}else{
            
$nombres $_POST['nombres'];
            
$apellidos $_POST['apellidos'];
            
            
$sql  "insert into imagenes(nombres,apellidos,imagen,imagen_thb)";
            
$sql .= "values('$nombres','$apellidos','$newfilename','$thb_filename')";
            
            
header("Location:index.php");                        
        
        }
                
    }

    
#se hace el proceso de editar.
    
if(isset($_POST['editar'])){
        if(
$_POST['editar']=='1'){
                if(
$_FILES['imagen']['size'] != '0' ){ //pregunto que si el tamaño de la imagen es diferente a 0.                
                    /*prefijo de cadena*/
                    
$str "abcdefghijklmnopqrstuvwxyz1234567890";
                    
$cad "";
                    for(
$i=0;$i<12;$i++) {
                        
$cad .= substr($str,rand(0,36),1);
                    }
                                         
                    
$foto $_FILES['imagen']['name'];
                    
$test explode(".",$foto);
                    
                    
$ruta "imagenes/";
                    
$ruta_2 "thumbnail/";
                    
                        if(
strtoupper($test[1])=="JPG" || strtoupper($test[1])=="JPEG" || strtoupper($test[1])=="GIF" || strtoupper($test[1])=="PNG"){
                            
                            
$newphoto $cad.'.'.$test[1];
                            
move_uploaded_file($_FILES['imagen']['tmp_name'],$ruta.$newphoto);
                            
$thumb = new thumbnail($ruta.$newphoto);
                            
$thb_newphoto "thb_".$newphoto;
                            
$thumb->size_width(172);
                            
$thumb->size_height(155);
                            
$thumb->jpeg_quality(80);
                            
$thumb->save($ruta_2.$thb_newphoto); 
                                
                            
$sql_edit " update imagenes set nombres = '".$_POST['nombres']."',
                                           apellidos = '"
.$_POST['apellidos']."', imagen = '$newphoto',
                                           imagen_thb = '$thb_newphoto' where idimagen = '"
.$_POST['codigo']."'";
                                            
                            
mysql_query($sql_edit) or die(mysql_error());                        
                            
                        }else{
                            echo 
"<span class=form_reg_error>La imagen que intenta grabar, debe ser de formato JPG, GIF o PNG.</span>";
                            echo 
"<br />";
                            echo 
"<a href='javascript:history.back()'>&lt;&lt; Clic aqu' para regresar </a>";
                        }    
                        
                                    
//echo "con foto";
                
}else{ 
                        
//caso contrario que se mantenga los nombres de las imagenes.
                        
$newphoto $_POST['nombreFoto'];
                        
$thb_newphoto $_POST['nombreFotoThb'];
                            
//echo "sin foto".$_POST['nombreFoto'];
                            
                        
$sql_edit " update imagenes set nombres = '".$_POST['nombres']."',
                                       apellidos = '"
.$_POST['apellidos']."', imagen = '$newphoto',
                                       imagen_thb = '$thb_newphoto'
                                       where idimagen = '"
.$_POST['codigo']."'";
                                        
                        
mysql_query($sql_edit) or die(mysql_error());
                         
                        
header("Location:index.php");                            
                }    
                 

        }
    } 

Última edición por xfer2; 18/10/2009 a las 16:17 Razón: correcion del texto.