Ver Mensaje Individual
  #1 (permalink)  
Antiguo 29/06/2007, 08:47
Avatar de ZiTAL
ZiTAL
 
Fecha de Ingreso: marzo-2004
Ubicación: Bermio (Bizkaia)
Mensajes: 1.545
Antigüedad: 20 años, 2 meses
Puntos: 62
Mi primera classe: Subir ficheros y redimensionar imagenes

Hola a todos aqui estoy con mi primera classe que sirve para subir archivos, y redimensionar imagenes, no se porque pero al final he puesto todo en ingles, empece a hacerlo en euskera pero... no se venadas mias:

Código PHP:
<?php
class ufri // upload file & resize image
 
{
     var 
$file_form_name// name in the form, default=file
    
var $where// directory where upload file, default=./
      
var $max_file_size// max file size, default=2048000 ' 2MB
      
var $replace// to replace existing file, values 'Yes' or 'No', Default=No
      
var $random// create aleatory numer to not overwrite existing file, values 'Yes' or 'No', Default=No
      //
      
var $name// name of file to resize, obligatory
      
var $max_width// maximum width of file, default 200 pixel
      
var $max_height// maximum heught of file, default 200 pixel
      
var $scalable// scalable values 'Yes' or 'No', default=Yes
      
var $wheretocopy// where to copy the new image, default=$where
      //
      
function uf() // upload file
       
{
           
// Default options assignation
           //
        
$file_form_name=$this->file_form_name;
        if(
$file_form_name==NULL)
         {
            
$file_form_name="file";
         }
        
$where=$this->where;
        if(
$where==NULL)
         {
            
$where="./";
         }
        
$max_file_size=$this->max_file_size;
        if(
$max_file_size==NULL)
         {
            
$max_file_size=2048000;
         }
        
$replace=$this->replace;
        if(
$replace==NULL)
         {
            
$replace="No";
         }
        
$random=$this->random;
        if(
$random==NULL)
         {
            
$random="No";
         }
        
// -> End of default options assignation
        
if($_FILES[$file_form_name]['size']>$max_file_size)
         {
            return array(
"Error","The file: ".$_FILES[$file_form_name]['name']." size: ".$_FILES[$file_form_name]['size']." is bigger than ".$max_file_size);
         }
        else
         {
            if(
file_exists($where.$_FILES[$file_form_name]['name']))
             {
                if(
$replace=="No")
                 {
                    if(
$random=="No")
                     {
                        return array(
"Error","Error the file: ".$_FILES[$file_form_name]['name']." exist");
                     }
                    elseif(
$random=="Yes")
                     {
                         
$tmp_file=$_FILES[$file_form_name]['name'];
                        
$tmp_name=substr($_FILES[$file_form_name]['name'],0,strlen($_FILES[$file_form_name]['name'])-strlen(strrchr($_FILES[$file_form_name]['name'],'.'))); // name without extension
                        
$tmp_extension=strrchr($_FILES[$file_form_name]['name'],'.'); // name with extension, includes dot .
                        //
                        
while(file_exists($where.$tmp_file))
                         {
                            
mt_srand((double)microtime()*1000000);
                            
$tmp_file=$tmp_name.mt_rand(0,99999).$tmp_extension;
                            
$_FILES[$file_form_name]['name']=$tmp_file;
                         }
                        if(
copy($_FILES[$file_form_name]['tmp_name'],$where.$_FILES[$file_form_name]['name']))
                         {
                            
// OK :)
                            
$this->name=$_FILES[$file_form_name]['name'];
                         }
                        else
                         {
                            return array(
"Error","Error copying: ".$_FILES[$file_form_name]['name']." file in path: ".$where);
                         }
                     }
                 }
                elseif(
$replace=="Yes")
                 {
                    if(
copy($_FILES[$file_form_name]['tmp_name'],$where.$_FILES[$file_form_name]['name']))
                     {
                        
// OK :)
                        
$this->name=$_FILES[$file_form_name]['name'];
                     }
                    else
                     {
                        return array(
"Error","Error copying: ".$_FILES[$file_form_name]['name']." file in path: ".$where);
                     }
                 }
             }
            else
             {
                if(
copy($_FILES[$file_form_name]['tmp_name'],$where.$_FILES[$file_form_name]['name']))
                 {
                    
// OK :)
                    
$this->name=$_FILES[$file_form_name]['name'];
                 }
                else
                 {
                    return array(
"Error","Error copying: ".$_FILES[$file_form_name]['name']." file in path: ".$where);
                 }
             }
         } 
// else
       
// function uf
      
function ri() // resize image, jpg/jpeg, gif and png file types
       
{
           
// Default options assignation
           //
        
$name=$this->name;
        
$where=$this->where;
        
//
          
$scalable=$this->scalable;
          if(
$scalable==NULL)
           {
            
$scalable="Yes";
           }
          
$max_width=$this->max_width;
          if(
$max_width==NULL)
           {
            
$max_width=200;
           }
          
$max_height=$this->max_height;
          if(
$max_height==NULL)
           {
            
$max_height=200;
           }
          
$wheretocopy=$this->wheretocopy;
          if(
$wheretocopy==NULL)
           {
            
$wheretocopy=$where;
           }
        
// -> End of default options assignation
        
if(!file_exists($where.$name))
         {
            return array(
"Error","Error ".$where.$name." file doesn't exist");
         }
        else
         {
            if(
strtolower(substr($name,strlen($name)-3,3))!="jpg" && strtolower(substr($name,strlen($name)-4,4))!="jpeg" && strtolower(substr($name,strlen($name)-3,3))!="gif" && strtolower(substr($name,strlen($name)-3,3))!="png")
             {
                return array(
"Error","The file ".$name." isn't image");
             }
            else 
// file type ok
             
{
                
$InfoImage=getimagesize($where.$name);
                
//
                
$width=$InfoImage[0];
                
$height=$InfoImage[1];
                
//
                
if($scalable=="Yes")
                 {
                     if(
$height>$max_height || $width>$max_width)
                      {
                        if(
$height>$width)
                         {
                          
$ratio=$height/$width;
                          
$width=$max_width/$ratio;
                          
$height=$max_height;
                         }
                        elseif(
$height<$width)
                         {
                          
$ratio=$width/$height;
                          
$height=$max_height/$ratio;
                          
$width=$max_width;
                         }
                        else
                         {
                           
$ratio=1;
                          
$height=$max_height;
                          
$width=$max_width;
                         }
                      } 
// end if($height>$max_height || $width>$max_width)
                 
// scalable
                
else
                 {
                    
$width=$max_width;
                    
$height=$max_height;
                 }
                 
// type: 1 gif, 2 jpg, 3 png
                 
$type=$InfoImage[2];
                 switch(
$type)
                  {
                    case 
1//gif
                     
{
                          
$img ImageCreateFromGIF($where.$name);
                          
$thumb imagecreatetruecolor($width,$height);
                        
imagecopyresampled($thumb,$img,0,0,0,0,$width,$height,imagesx($img),imagesy($img));
                        
ImageGIF($thumb,$wheretocopy.$name,100);
                        break;
                     }
                    case 
2//jpg,jpeg
                     
{
                          
$img ImageCreateFromJPEG($where.$name);
                          
$thumb imagecreatetruecolor($width,$height);
                         
imagecopyresampled($thumb,$img,0,0,0,0,$width,$height,imagesx($img),imagesy($img));
                         
ImageJPEG($thumb,$wheretocopy.$name,100);
                        break;
                     }
                    case 
3//png
                     
{
                          
$img ImageCreateFromPNG($where.$name);
                          
$thumb imagecreatetruecolor($width,$height);
                        
imagecopyresampled($thumb,$img,0,0,0,0,$width,$height,imagesx($img),imagesy($img));
                        
ImagePNG($thumb,$wheretocopy.$name);
                        break;
                     }
                  } 
// switch
             
// end else file type ok
         
// else
       
// end ri
 
// class
?>
Un poco de explicacion:

var $file_form_name; // nombre en el formulario por defecto=file
var $where; // a que carpeta subiremos el archivo, default=./
var $max_file_size; // tamaño maximo de archivo, default=2048000 ' 2MB
var $replace; // para reemplazar archivo, values 'Yes' or 'No', Default=No
var $random; // crear numero aleatorio para no sobreescribir el fichero, values 'Yes' or 'No', Default=No
//
var $name; // nombre del fichero
var $max_width; // maximo de ancho, default 200 pixel
var $max_height; // maximo del altura, default 200 pixel
var $scalable; // escalar 'Yes' or 'No', default=Yes
var $wheretocopy; // karpeta donde queremos copiarlo, default= where (lo reemplaza)

Ejemplo:
Código PHP:
<?php
include("functions.php");
if(
$_GET["Bota"]=="Bai")
 {
     
//
    
$file = new ufri;
    
$file->replace="Yes";
    
$a=$file->uf();
    
//
    
if($a[0]=="Error")
     {
         echo 
$a[1];
     }
    
//
    
$file->scalable="Yes";
    
$file->max_width=800;
    
$file->max_height=800;
    
$file->wheretocopy="./images/";
    
$a=$file->ri();
    if(
$a[0]=="Error")
     {
         echo 
$a[1];
     }
    exit();
 }
?>
Código HTML:
<html>
<head>
</head>
<body>

  <form action="?Bota=Bai" method="post" enctype="multipart/form-data">
  <input type="file" name="file" /><br/>
  <input type="submit" value="value"/>

  </form>
</body>
</html> 
Bueno esta es la version 1.0, tengo previsto ponerle una funcion para restringir extensiones.

Pedonad por mi penoso ingles y mi horrible castellano.

Eskerrik asko ;)
__________________
http://zital.no-ip.org
____________________

Euskerie ahuen eta bijotzan