<?php
class uploadIMG{
 
    # Obtenemos el nombre del $_FILES[]
    var $file;
    # Obtenemos el temporal del $_FILES[]
    var $temp;
    # Obtenemos el nombre de la carpeta para la imagen      
    var $carpeta;
    # Obtenemos el nombre del archivo  
    var $name;
    # Obtenemos el tipo de archivo
    var $tipo;
    # Obtenemos el tamaño del archivo en bits
    var $tam_file_bt;
    # Obtenemos el tamaño del archivo en KB
    var $tam_file_kb;
    # Asignamos un tamaño maximo del archivo en KB
    var $tam_max;
    # Asignamos un nombre nuevo al archivo
    var $newname;
    # Obtenemos la extension del archivo
    var $ext;
    # Obtenemos la extension del archivo
    var $ext2;
    # Asignamos extensiones permitidas para el archivo
    var $exton;
    var $datosimg;
    # Definimos un nuevo alto para la imagen
    var $new_alto;
    # Definimos un nuevo ancho para la imagen
    var $new_ancho;
    # Definimos un nuevo alto para la imagen
    var $new_alto2;
    # Definimos un nuevo ancho para la imagen
    var $new_ancho2;
    # Definimos el alto maximo de la imagen
    var $hmax;
    # Definimos el ancho maximo de la imagen
    var $wmax;
    # Definimos una posicion x para la imagen
    var $image_x;
    # Definimos una posicion y para la imagen
    var $image_y;
    # Definimos la ratio del ancho y alto de la imagen
    var $ratio;
    # Obtenemos la direccion del logo para la imagen
    var $logo;
    # Creamos la imagen del logo para utilizarla 
    var $src;
    # Creamos la imagen apartir del archivo temporal
    var $imagen;
    # Creamos la imagen para utilizarla 
    var $img;
    # Variable para Thumb
    var $img_thumb;
    var $imagen_thumb;
    # Alto de la Imagen Thumb;
    var $alto_thumb;
    # Ancho de la Imagen Thumb
    var $ancho_thumb;
    # Ratio thumb
    var $ratio_thumb;
    # Ancho Maximo Thumb
    var $ancho_thumb_max;
    # Alto Maximo Thumb
    var $alto_thumb_max;
    var $listo;
    var $listo_thumb;
    var $copiado;
    var $copy_thumb;
    var $message;
    var $end;
    var $init;
    
    function load(){
        $this->exton            = array("image/jpeg",                                         "image/gif", 
                                        "image/png");       // Extensiones validas
        $this->ext              = "";
        $this->ext2             = array("jpg","gif","png","jpeg");         $this->image_x          = 0;                        // Posicion X de la imagen
        $this->image_y          = 0;                        // Posicion Y de la imagen
        $this->ratio            = 0;                        // Ratio de la imagen
        $this->img              = "";                       // Dejamos en blanco la variable img
        $this->src              = "";                       // Dejamos en blanca la variable src
        $this->new_alto         = 0;                        // Nuevo alto de la imagen
        $this->new_ancho        = 0;                        // Nuevo ancho de la imagen
        $this->hmax             = 0;                        // Alto maximo de la imagen
        $this->wmax             = 0;                        // Ancho maximo de la imagen
        $this->alto_thumb_max   = 0;                        // Alto del Thumb maximo
        $this->ancho_thumb_max  = 0;                        // Ancho del Thumb Maximo
        $this->imagen           = "";                       // Varible que obtine la imagen del temporal
        $this->listo            = false;                    // Valor true o false para guardar imagen
        $this->listo_thumb      = false;                    // Dejamos en blancado listado_thumb
        $this->copiado          = "";                       // Comprobar si se guardo la imagen
        $this->newname          = "";                       // Nuevo nombre en blanco
        $this->datosimg         = "";                       // Valor vacio a datosimag
        $this->tam_file_bt      = 0;                        // Tamaño de la imagen en 0 bits
        $this->tam_file_kb      = 0;                        // Tamaño de la imagen en 0 kb
        $this->tipo             = "";                       // Tipo de imagen vacio
        $this->tam_max          = 0;                        // Tamaño maximo de imagen en 0;
        $this->message          = "";                       // Mensaje en blanco
        $this->init             = "";                       // Variable de inicio
        $this->end              = "";                       // Variable final     }
    function addDate($file, $carpeta, $tam_max, $logo){ 
       $this->file          =   $_FILES[$file];        $this->carpeta       =   $carpeta;
       $this->tam_max       =   $tam_max;
       $this->logo          =   $logo;
       $this->temp          =   $this->file['tmp_name'];        $this->name          =   $this->file['name'];        $this->tipo          =   $this->file['type'];        $this->tam_file_bt   =   $this->file['size'];         for($i=1; $i<=30; $i++)
        {
            $this->newname .= $source[$num-1];
        }
        $this->newname      .=   ".jpg";
        {
                $this->message = "Este archivo no es valido";
                $this->init = false;
        }
        else{$this->init = true;}
    }
    function analizaIMG(){
       if($init == true)
        {
                $this->message = "La imagen no fue copiada";
                $this->listo = false;
            }elseif (!in_array($this->tipo, $this->exton)){                 $this->message = "Tipo de Imagen no válido";
                $this->listo = false;
            }elseif(!in_array($this->ext, $this->ext2)){                 $this->message = "Tipo de Extensión No permitida";
                $this->listo = false;
            }elseif($this->tam_file_kb > $this->tam_max){
                $this->message = "El archivo supera el peso limite. " . $this->tam_file_kb . " kb.";
                $this->listo = false;
            }elseif($this->datosimg[0]< 300){
                $this->message = "La imagen es demasiado pequeña, lo recomendable es 800 x 600";
                $this->listo = false;
            }elseif($this->datosimg[1] < 300){
                $this->message = "La imagen es pequeña, lo recomendable es 800 x 600. Minimos 300px x 300px ";
                $this->listo = false;
            }else{
                $this->listo = true;
            }
        }
    }
    function copiarIMG(){
        if ($this->listo == true){
            switch($this->datosimg['mime'])
            {
                case $this->exton[0]:
                    $this->listo_thumb = true;
                    break;
     
                case $this->exton[1]:
                    $this->listo_thumb = true;
                    break;
             
                case $this->exton[2]:
                    $this->listo_thumb = true;
                    break;
             
                default:
                    $this->listo_thumb = false;
            }
                if ($this->listo_thumb == true)
                {
                    $this->hmax             = 600;
                    $this->new_ancho        = 800;
                    
                    $this->ratio            = ($this->datosimg[0] / $this->new_ancho);
                    $this->new_alto         = ($this->datosimg[1] / $this->ratio);
 
                    if ($this->datosimg[0]< $this->new_ancho && $this->datosimg[1]< $this->hmax){
                        
                        $this->new_ancho = $this->datosimg[0];
                        $this->new_alto  = $this->datosimg[1];
                        
                    }elseif($this->new_alto>$this->hmax){
                        
                        $this->wmax         = $this->hmax*$this->new_ancho/$this->new_alto;
                        $this->new_alto     = $this->hmax;
                        $this->new_ancho    = $this->wmax;
                    }
 
                                
                    imagecopyresampled($this->img, $this->imagen, 0, 0, 0, 0, $this->new_ancho, $this->new_alto, $this->datosimg[0], $this->datosimg[1]);                     
                    imagecopy($this->img, $this->src, $this->new_ancho - 126, $this->new_alto - 32, 0, 0, 125, 30);                                
                    $this->copiado = imagejpeg($this->img,$this->carpeta . $this->newname, 80);            
                    if (isset($this->copiado)){                         $this->message = "Copiado correctamente, el tipo de archivo es:" . $this->tipo . "<br />";
                        
                    }
                }
        }
    }
    function Thumb(){
        
        if ($this->listo_thumb == true){
                          
            
 
            $this->new_ancho2 = $this->datosimg[0]; 
            $this->new_alto2 = $this->datosimg[1]; 
            
            
            $this->ancho_thumb_max = 150;
            $this->alto_thumb_max = 150;
 
            if($this->ancho_thumb_max/$this->new_ancho2 > $this->alto_thumb_max/$this->new_alto2)
            {
                $this->ancho_thumb = $this->ancho_thumb_max;
                $this->alto_thumb = ($this->new_alto2 * $this->ancho_thumb) / $this->new_ancho2;
                $this->image_x = 0;
                $this->image_y = ($this->alto_thumb_max - $this->alto_thumb) / 2;
            }
            else
            {
                $this->alto_thumb = $this->alto_thumb_max;
                $this->ancho_thumb = ($this->new_ancho2 * $this->alto_thumb) / $this->new_alto2;
                $this->image_y = 0;
                $this->image_x = ($this->ancho_thumb_max - $this->ancho_thumb) / 2;
            }
 
                
            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);           
            
            $this->copy_thumb = @imagejpeg($this->img_thumb,$this->carpeta . "thumb/" . $this->newname, 80);    
            if ($this->copy_thumb){
                $this->message = "Copiado correctamente Thumb <br />";
            }
        }
    }
    
}
?>