Ver Mensaje Individual
  #21 (permalink)  
Antiguo 25/05/2011, 21:35
Avatar de CCB
CCB
 
Fecha de Ingreso: noviembre-2009
Ubicación: Perú
Mensajes: 65
Antigüedad: 14 años, 6 meses
Puntos: 3
Respuesta: Upload para varias imagenes en php

Código PHP:
Ver original
  1. <?php
  2. class uploadIMG{
  3.  
  4.     # Obtenemos el nombre del $_FILES[]
  5.    var $file;
  6.     # Obtenemos el temporal del $_FILES[]
  7.    var $temp;
  8.     # Obtenemos el nombre de la carpeta para la imagen      
  9.    var $carpeta;
  10.     # Obtenemos el nombre del archivo  
  11.    var $name;
  12.     # Obtenemos el tipo de archivo
  13.    var $tipo;
  14.     # Obtenemos el tamaño del archivo en bits
  15.    var $tam_file_bt;
  16.     # Obtenemos el tamaño del archivo en KB
  17.    var $tam_file_kb;
  18.     # Asignamos un tamaño maximo del archivo en KB
  19.    var $tam_max;
  20.     # Asignamos un nombre nuevo al archivo
  21.    var $newname;
  22.     # Obtenemos la extension del archivo
  23.    var $ext;
  24.     # Obtenemos la extension del archivo
  25.    var $ext2;
  26.     # Asignamos extensiones permitidas para el archivo
  27.    var $exton;
  28.     var $datosimg;
  29.     # Definimos un nuevo alto para la imagen
  30.    var $new_alto;
  31.     # Definimos un nuevo ancho para la imagen
  32.    var $new_ancho;
  33.     # Definimos un nuevo alto para la imagen
  34.    var $new_alto2;
  35.     # Definimos un nuevo ancho para la imagen
  36.    var $new_ancho2;
  37.     # Definimos el alto maximo de la imagen
  38.    var $hmax;
  39.     # Definimos el ancho maximo de la imagen
  40.    var $wmax;
  41.     # Definimos una posicion x para la imagen
  42.    var $image_x;
  43.     # Definimos una posicion y para la imagen
  44.    var $image_y;
  45.     # Definimos la ratio del ancho y alto de la imagen
  46.    var $ratio;
  47.     # Obtenemos la direccion del logo para la imagen
  48.    var $logo;
  49.     # Creamos la imagen del logo para utilizarla
  50.    var $src;
  51.     # Creamos la imagen apartir del archivo temporal
  52.    var $imagen;
  53.     # Creamos la imagen para utilizarla
  54.    var $img;
  55.     # Variable para Thumb
  56.    var $img_thumb;
  57.     var $imagen_thumb;
  58.     # Alto de la Imagen Thumb;
  59.    var $alto_thumb;
  60.     # Ancho de la Imagen Thumb
  61.    var $ancho_thumb;
  62.     # Ratio thumb
  63.    var $ratio_thumb;
  64.     # Ancho Maximo Thumb
  65.    var $ancho_thumb_max;
  66.     # Alto Maximo Thumb
  67.    var $alto_thumb_max;
  68.     var $listo;
  69.     var $listo_thumb;
  70.     var $copiado;
  71.     var $copy_thumb;
  72.     var $message;
  73.     var $end;
  74.     var $init;
  75.    
  76.     function load(){
  77.         $this->exton            = array("image/jpeg",
  78.                                         "image/gif",
  79.                                         "image/png");       // Extensiones validas
  80.         $this->ext              = "";
  81.         $this->ext2             = array("jpg","gif","png","jpeg");
  82.         $this->image_x          = 0;                        // Posicion X de la imagen
  83.         $this->image_y          = 0;                        // Posicion Y de la imagen
  84.         $this->ratio            = 0;                        // Ratio de la imagen
  85.         $this->img              = "";                       // Dejamos en blanco la variable img
  86.         $this->src              = "";                       // Dejamos en blanca la variable src
  87.         $this->new_alto         = 0;                        // Nuevo alto de la imagen
  88.         $this->new_ancho        = 0;                        // Nuevo ancho de la imagen
  89.         $this->hmax             = 0;                        // Alto maximo de la imagen
  90.         $this->wmax             = 0;                        // Ancho maximo de la imagen
  91.         $this->alto_thumb_max   = 0;                        // Alto del Thumb maximo
  92.         $this->ancho_thumb_max  = 0;                        // Ancho del Thumb Maximo
  93.         $this->imagen           = "";                       // Varible que obtine la imagen del temporal
  94.         $this->listo            = false;                    // Valor true o false para guardar imagen
  95.         $this->listo_thumb      = false;                    // Dejamos en blancado listado_thumb
  96.         $this->copiado          = "";                       // Comprobar si se guardo la imagen
  97.         $this->newname          = "";                       // Nuevo nombre en blanco
  98.         $this->datosimg         = "";                       // Valor vacio a datosimag
  99.         $this->tam_file_bt      = 0;                        // Tamaño de la imagen en 0 bits
  100.         $this->tam_file_kb      = 0;                        // Tamaño de la imagen en 0 kb
  101.         $this->tipo             = "";                       // Tipo de imagen vacio
  102.         $this->tam_max          = 0;                        // Tamaño maximo de imagen en 0;
  103.         $this->message          = "";                       // Mensaje en blanco
  104.         $this->init             = "";                       // Variable de inicio
  105.         $this->end              = "";                       // Variable final
  106.     }
  107.     function addDate($file, $carpeta, $tam_max, $logo){
  108.        $this->file          =   $_FILES[$file];
  109.        $this->carpeta       =   $carpeta;
  110.        $this->tam_max       =   $tam_max;
  111.        $this->logo          =   $logo;
  112.        $this->temp          =   $this->file['tmp_name'];
  113.        $this->name          =   $this->file['name'];
  114.        $this->tipo          =   $this->file['type'];
  115.        $this->tam_file_bt   =   $this->file['size'];
  116.         $source = substr(md5(uniqid(rand())),0,50) ."-_";
  117.         $source = str_split($source,1);
  118.         for($i=1; $i<=30; $i++)
  119.         {
  120.             mt_srand((double)microtime() * 1000000);
  121.             $num            = mt_rand(1,count($source));
  122.             $this->newname .= $source[$num-1];
  123.         }
  124.         $this->ext           =   strtolower(substr($this->name, strrpos($this->name, '.')+1));
  125.         $this->tam_file_kb   =   round(filesize($this->temp)/1024,2);
  126.         $this->newname      .=   ".jpg";
  127.         $this->datosimg         =   getimagesize($this->temp);
  128.         if( ! getimagesize($this->temp) )
  129.         {
  130.                 $this->message = "Este archivo no es valido";
  131.                 $this->init = false;
  132.         }
  133.         else{$this->init = true;}
  134.     }
  135.     function analizaIMG(){
  136.        if($init == true)
  137.         {
  138.             if(!is_uploaded_file($this->temp)){
  139.                 $this->message = "La imagen no fue copiada";
  140.                 $this->listo = false;
  141.             }elseif (!in_array($this->tipo, $this->exton)){
  142.                 $this->message = "Tipo de Imagen no válido";
  143.                 $this->listo = false;
  144.             }elseif(!in_array($this->ext, $this->ext2)){
  145.                 $this->message = "Tipo de Extensión No permitida";
  146.                 $this->listo = false;
  147.             }elseif($this->tam_file_kb > $this->tam_max){
  148.                 $this->message = "El archivo supera el peso limite. " . $this->tam_file_kb . " kb.";
  149.                 $this->listo = false;
  150.             }elseif($this->datosimg[0]< 300){
  151.                 $this->message = "La imagen es demasiado pequeña, lo recomendable es 800 x 600";
  152.                 $this->listo = false;
  153.             }elseif($this->datosimg[1] < 300){
  154.                 $this->message = "La imagen es pequeña, lo recomendable es 800 x 600. Minimos 300px x 300px ";
  155.                 $this->listo = false;
  156.             }else{
  157.                 $this->listo = true;
  158.             }
  159.         }
  160.     }
  161.     function copiarIMG(){
  162.         if ($this->listo == true){
  163.             switch($this->datosimg['mime'])
  164.             {
  165.                 case $this->exton[0]:
  166.                     $this->imagen = imagecreatefromjpeg($this->temp);
  167.                     $this->listo_thumb = true;
  168.                     break;
  169.      
  170.                 case $this->exton[1]:
  171.                     $this->imagen = imagecreatefromgif($this->temp);
  172.                     $this->listo_thumb = true;
  173.                     break;
  174.              
  175.                 case $this->exton[2]:
  176.                     $this->imagen = imagecreatefrompng($this->temp);
  177.                     $this->listo_thumb = true;
  178.                     break;
  179.              
  180.                 default:
  181.                     $this->listo_thumb = false;
  182.             }
  183.                 if ($this->listo_thumb == true)
  184.                 {
  185.                     $this->hmax             = 600;
  186.                     $this->new_ancho        = 800;
  187.                    
  188.                     $this->ratio            = ($this->datosimg[0] / $this->new_ancho);
  189.                     $this->new_alto         = ($this->datosimg[1] / $this->ratio);
  190.  
  191.                     if ($this->datosimg[0]< $this->new_ancho && $this->datosimg[1]< $this->hmax){
  192.                        
  193.                         $this->new_ancho = $this->datosimg[0];
  194.                         $this->new_alto  = $this->datosimg[1];
  195.                        
  196.                     }elseif($this->new_alto>$this->hmax){
  197.                        
  198.                         $this->wmax         = $this->hmax*$this->new_ancho/$this->new_alto;
  199.                         $this->new_alto     = $this->hmax;
  200.                         $this->new_ancho    = $this->wmax;
  201.                     }
  202.  
  203.                     $this->src = imagecreatefrompng($this->logo);
  204.                     $this->img = imagecreatetruecolor($this->new_ancho, $this->new_alto);
  205.                                
  206.                     imagecopyresampled($this->img, $this->imagen, 0, 0, 0, 0, $this->new_ancho, $this->new_alto, $this->datosimg[0], $this->datosimg[1]);
  207.                    
  208.                     imagecopy($this->img, $this->src, $this->new_ancho - 126, $this->new_alto - 32, 0, 0, 125, 30);
  209.                                
  210.                     $this->copiado = imagejpeg($this->img,$this->carpeta . $this->newname, 80);
  211.            
  212.                     if (isset($this->copiado)){
  213.                         imagedestroy($this->src);
  214.                         imagedestroy($this->img);
  215.                         $this->message = "Copiado correctamente, el tipo de archivo es:" . $this->tipo . "<br />";
  216.                        
  217.                     }
  218.                 }
  219.         }
  220.     }
  221.     function Thumb(){
  222.        
  223.         if ($this->listo_thumb == true){
  224.                          
  225.             $this->img_thumb = imagecreatetruecolor(150, 150);
  226.            
  227.  
  228.             $this->new_ancho2 = $this->datosimg[0];
  229.             $this->new_alto2 = $this->datosimg[1];
  230.            
  231.            
  232.             $this->ancho_thumb_max = 150;
  233.             $this->alto_thumb_max = 150;
  234.  
  235.             if($this->ancho_thumb_max/$this->new_ancho2 > $this->alto_thumb_max/$this->new_alto2)
  236.             {
  237.                 $this->ancho_thumb = $this->ancho_thumb_max;
  238.                 $this->alto_thumb = ($this->new_alto2 * $this->ancho_thumb) / $this->new_ancho2;
  239.                 $this->image_x = 0;
  240.                 $this->image_y = ($this->alto_thumb_max - $this->alto_thumb) / 2;
  241.             }
  242.             else
  243.             {
  244.                 $this->alto_thumb = $this->alto_thumb_max;
  245.                 $this->ancho_thumb = ($this->new_ancho2 * $this->alto_thumb) / $this->new_alto2;
  246.                 $this->image_y = 0;
  247.                 $this->image_x = ($this->ancho_thumb_max - $this->ancho_thumb) / 2;
  248.             }
  249.  
  250.                
  251.             imagecopyresampled($this->img_thumb, $this->imagen, $this->image_x, $this->image_y, 0, 0, $this->ancho_thumb, $this->alto_thumb, $this->new_ancho2, $this->new_alto2);
  252.          
  253.            
  254.             $this->copy_thumb = @imagejpeg($this->img_thumb,$this->carpeta . "thumb/" . $this->newname, 80);
  255.    
  256.             if ($this->copy_thumb){
  257.                 imagedestroy($this->img_thumb);
  258.                 imagedestroy($this->imagen);
  259.                 $this->message = "Copiado correctamente Thumb <br />";
  260.                 $this->end = "ok";
  261.             }
  262.         }
  263.     }
  264.    
  265. }
  266. ?>