Advertisement
programu

Untitled

Sep 2nd, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.42 KB | None | 0 0
  1. NO LO HE PROBADO, ASI QUE ANTES HAZ BACKUP DE c.upload.php y de ajax.upload.php
  2.  
  3. 1) Abres el ajax.upload.php y buscas:
  4.             echo $tsCore->setJSON($tsUpload->newUpload(1));
  5. Y lo reemplazas por:
  6.             echo $tsCore->setJSON($tsUpload->newUpload(1, true));
  7.  
  8.  
  9. 2) Reemplaza tu c.upload.php por este:
  10. ¡¡(Debes modificar la linea 155, 161, 200 y la 209 para poner los nombres de la carpeta.)!!
  11.  
  12. <?php if ( ! defined('TS_HEADER')) exit('No se permite el acceso directo al script');
  13. /**
  14.  * Modelo para subir im�genes
  15.  *
  16.  * @name    c.upload.php
  17.  * @author  PHPost Team
  18.  */
  19. class tsUpload {
  20.     var $type = 1;  // TIPO DE SUBIDA
  21.     var $max_size = 1048576;    // 1MB
  22.     var $allow_types = array('png','gif','jpeg'); // ARCHIVOS PERMITIDOS
  23.     var $found = 0; // VARIABLE BANDERA
  24.     var $file_url = ''; // URL
  25.     var $file_size = array(); // TAMA�O DEL ARCHIVO REMOTO
  26.     var $image_size = array('w' => 570, 'h' => 450);
  27.     var $image_scale = false;
  28.     var $servers = array();
  29.     var $server = 'imgur';  // DEFAULT IMGUR
  30.  
  31.     // INSTANCIA DE LA CLASE
  32.     public static function &getInstance(){
  33.         static $instance;
  34.        
  35.         if( is_null($instance) ){
  36.             $instance = new tsUpload();
  37.         }
  38.         return $instance;
  39.     }
  40.     // CONSTRUCTOR
  41.     public function __construct(){
  42.        $this->servers = array(
  43.             'imgur' => 'http://api.imgur.com/2/upload.json?key=24bf6070f45ed716e8cf9324baebddbd',
  44.             'imgshack' => 'http://post.imageshack.us/transload.php',
  45.        );
  46.     }
  47.     /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*\
  48.                                 UPLOAD
  49.     /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  50.     /*
  51.         newUpload($type)
  52.         :: $type => URL o ARCHIVO
  53.     */
  54.     function newUpload($type = 1, $post = false){
  55.         $this->type = $type;
  56.         // ARCHIVOS
  57.         if($this->type == 1){
  58.             foreach($_FILES as $file)
  59.                 $fReturn[] = $this->uploadFile($file, 'file', $post);
  60.         // DESDE URL
  61.         }elseif($this->type == 2) {
  62.                $fReturn[] = $this->uploadUrl();
  63.         // CROP
  64.         } elseif($this->type == 3){
  65.             if(empty($this->file_url)) {
  66.                 foreach($_FILES as $file)
  67.                     $fReturn = $this->uploadFile($file);
  68.                     if(empty($fReturn['msg'])) return array('error' => $fReturn[1]);
  69.             } else {
  70.                 $file = array(
  71.                     'name' => substr($this->file_url, -4),
  72.                     'type' => 'image/url',
  73.                     'tmp_name' => $this->file_url,
  74.                     'error' => 0,
  75.                     'size' => 0
  76.                 );
  77.                 //
  78.                 $fReturn = $this->uploadFile($file, 'url');
  79.                 if(empty($fReturn['msg'])) return array('error' => $fReturn[1]);
  80.             }
  81.         }
  82.         //
  83.         if($this->found == 0) return array('error' => 'No se ha seleccionado archivo alguno.');
  84.         else return $fReturn;
  85.     }
  86.     /*
  87.         uploadFiles()
  88.     */
  89.     function uploadFile($file, $type = 'file'){
  90.         // VALIDAR
  91.         $error = $this->validFile($file, $type);
  92.         if(!empty($error)){
  93.             return array(0, $error);
  94.         }else{
  95.             $type = explode('/',$file['type']);
  96.             $ext = ($type[1] == 'jpeg' || $type[1] == 'url') ? 'jpg' : $type[1]; // EXTENCION
  97.             $key = rand(0,1000);
  98.             $newName = 'phpost_'.$key.'.'.$ext;
  99.             // IMAGEN
  100.             if($this->type == 1)
  101.                 return array(1, $this->sendFile($file,$newName), $type[1]);
  102.             // CROP
  103.             else
  104.                 return array('msg' => $this->createImage($file,$newName,$post), 'error' => '', 'key' => $key, 'ext' => $ext);
  105.             //
  106.         }
  107.     }
  108.     /*
  109.         uploadUrl()
  110.     */
  111.     function uploadUrl(){
  112.         $error = $this->validFile(null, 'url');
  113.         if(!empty($error)) return array(0, $error);
  114.         else return array(1, urldecode($this->file_url));
  115.     }
  116.     /*
  117.         validFile()
  118.     */
  119.     function validFile($file, $type = 'file'){
  120.         // ARCHIVO
  121.         if($type == 'file'){
  122.             // SE ENCONTRO EL ARCHIVO
  123.             if(empty($file['name'])) return 'No Found';
  124.             else $this->found = $this->found + 1;
  125.             //
  126.             $type = explode('/',$file['type']);
  127.             if($file['size'] > $this->max_size) return '#'.$this->found.' pesa mas de 1 MB.';
  128.             elseif(!in_array($type[1], $this->allow_types)) return '#'.$this->found.' no es una imagen.';
  129.         } elseif($type == 'url'){
  130.             $this->file_size = getimagesize($this->file_url);
  131.             // TAMA�O MINIMO
  132.             $min_w = 160;
  133.             $min_h = 120;
  134.             // MAX PARA EVITAR CARGA LENTA
  135.             $max_w = 1024;
  136.             $max_h = 1024;
  137.             $this->found = 1;
  138.             //
  139.             if(empty($this->file_size[0])) return 'La url ingresada no existe o no es una imagen v&aacute;lida.';
  140.             elseif($this->file_size[0] < $min_w || $this->file_size[1] < $min_h) return 'Tu foto debe tener un tama&ntilde;o superior a 160x120 pixeles.';
  141.             elseif($this->file_size[0] > $max_w || $this->file_size[1] > $max_h) return 'Tu foto debe tener un tama&ntilde;o menor a 1024x1024 pixeles.';
  142.         }
  143.         // TODO BIEN
  144.         return false;
  145.     }
  146.     /*
  147.       sendFile($file,$name)
  148.     */
  149.     function sendFile($file, $name){
  150.         //
  151.         $url = $this->createImage($file,$name);
  152.         // SUBIMOS...
  153.         $new_img = $this->getImagenUrl($this->uploadImagen($this->setParams($url)));
  154.         // BORRAR
  155.         $this->deleteFile($name);
  156.         // REGRESAMOS
  157.         return $new_img;
  158.     }
  159.     /*
  160.         copyFile($file, $name)
  161.     */
  162.     function copyFile($file,$name,$post){
  163.         global $tsCore;
  164.         // COPIAMOS
  165.         if($post == true)
  166.         $root = TS_FILES.'uploads/'.$name; //CARPETA POSTS
  167.         else
  168.         $root = TS_FILES.'uploads/'.$name;
  169.         copy($file['tmp_name'],$root);
  170.         // REGRESAMOS LA URL
  171.         if($post == true)
  172.         return $tsCore->settings['url'].'/files/uploads/'.$name; //URL IMAGENES POSTS
  173.         else
  174.         return $tsCore->settings['url'].'/files/uploads/'.$name;
  175.     }
  176.     /*
  177.         createImage()
  178.     */
  179.     function createImage($file, $name, $post){
  180.         global $tsCore;
  181.         // TAMA�O
  182.         $size = empty($this->file_size) ? getimagesize($file['tmp_name']) : $this->file_size;
  183.         if(empty($size)) die('0: Intentando subir un archivo que no es v�lido.');
  184.         $width = $size[0];
  185.         $height = $size[1];
  186.         // ESCALAR SOLO SI LA IMAGEN EXEDE EL TAMA�O Y SE DEBE ESCALAR
  187.         if($this->image_scale == true && ($width > $this->image_size['w'] || $height > $this->image_size['h'])){
  188.                 // OBTENEMOS ESCALA
  189.                 if($width > $height){
  190.                     $_height = ($height * $this->image_size['w']) / $width;
  191.                     $_width = $this->image_size['w'];
  192.                 } else {
  193.                     $_width = ($width * $this->image_size['h']) / $height;
  194.                     $_height = $this->image_size['h'];
  195.                 }
  196.                 // TIPO
  197.                 switch($file['type']){
  198.                     case 'image/url':
  199.                         $img = imagecreatefromstring($tsCore->getUrlContent($file['tmp_name']));
  200.                     break;
  201.                     case 'image/jpeg':
  202.                     case 'image/jpg':
  203.                         $img = imagecreatefromjpeg($file['tmp_name']);
  204.                         break;
  205.                     case 'image/gif':
  206.                         $img = imagecreatefromgif($file['tmp_name']);
  207.                         break;
  208.                     case 'image/png':
  209.                         $img = imagecreatefrompng($file['tmp_name']);
  210.                         break;
  211.                 }
  212.                 // ESCALAMOS NUEVA IMAGEN
  213.                 $newimg = imagecreatetruecolor($_width, $_height);
  214.                 imagecopyresampled($newimg, $img, 0, 0, 0, 0, $_width, $_height, $width, $height);
  215.                 // COPIAMOS
  216.                 if($post == true)
  217.                 $root = TS_FILES.'uploads/'.$name; //CARPETA POSTS
  218.                 else
  219.                 $root = TS_FILES.'uploads/'.$name;
  220.                 //
  221.                 imagejpeg($newimg,$root,100);
  222.                 imagedestroy($newimg);
  223.                 imagedestroy($img);
  224.                 // RETORNAMOS
  225.                 if($post == true)
  226.                 return $tsCore->settings['url'].'/files/uploads/'.$name; //RETORNTAR URL DE IMAGENES DE LOS POSTS
  227.                 else
  228.                 return $tsCore->settings['url'].'/files/uploads/'.$name;
  229.         } else {
  230.             // MANTENEMOS LAS DIMENCIONES Y SOLO COPIAMOS LA IMAGEN
  231.             return $this->copyFile($file, $name, $post);
  232.         }
  233.     }
  234.     /**
  235.      * @name cropAvatar()
  236.      * @uses Creamos el avatar a partir de las coordenadas resibidas
  237.      * @access public
  238.      * @param int
  239.      * @return array
  240.     */
  241.     public function cropAvatar($key){
  242.         $source = TS_FILES.'uploads/phpost_'.$_POST['key'].'.'.$_POST['ext'];
  243.         $size = getimagesize($source);
  244.         // COORDENADAS
  245.         $x = $_POST['x'];
  246.         $y = $_POST['y'];
  247.         $w = $_POST['w'];
  248.         $h = $_POST['h'];
  249.         // TAMA�OS
  250.         $_w = $_h = 120;
  251.         $_tw = $_th = 50;
  252.         // CREAMOS LA IMAGEN DEPENDIENDO EL TIPO
  253.         switch($size['mime']){
  254.             case 'image/jpeg':
  255.             case 'image/jpg':
  256.                 $img = imagecreatefromjpeg($source);
  257.                 break;
  258.             case 'image/gif':
  259.                 $img = imagecreatefromgif($source);
  260.                 break;
  261.             case 'image/png':
  262.                 $img = imagecreatefrompng($source);
  263.                 break;
  264.         }
  265.         if(!$img) return array('error' => 'No pudimos crear tu avatar...');
  266.         //
  267.         $width = imagesx($img);
  268.         $height = imagesy($img);
  269.         // AVATAR
  270.         $avatar = imagecreatetruecolor($_w, $_h);
  271.         imagecopyresampled($avatar, $img, 0, 0, $x, $y, $_w, $_h, $w, $h);
  272.         // AVATAR THUMB
  273.         $thumb = imagecreatetruecolor($_tw, $_th);
  274.         imagecopyresampled($thumb, $img, 0, 0, $x, $y, $_tw, $_th, $w, $h);
  275.         // GUARDAMOS...
  276.         $root = TS_FILES.'avatar/'.$key.'_';
  277.         imagejpeg($avatar,$root.'120.jpg',90);
  278.         imagejpeg($thumb,$root.'50.jpg',90);
  279.         // CLEAR
  280.         imagedestroy($img);
  281.         imagedestroy($avatar);
  282.         imagedestroy($thumb);
  283.         // BORRAMOS LA ORIGINAL
  284.         unlink($source);
  285.         //
  286.         return array('error' => 'success');
  287.     }
  288.     /*
  289.         deleteFile()
  290.     */
  291.     function deleteFile($file){
  292.         $root = TS_FILES.'uploads/'.$file;
  293.         unlink($root);
  294.         return true;
  295.     }
  296.     /*
  297.         uploadImagen()
  298.     */
  299.     function uploadImagen($params){
  300.         // User agent
  301.         $useragent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.9) Gecko/2008052906 Firefox/3.0";
  302.         // SERVIDOR
  303.         $servidor = $this->servers[$this->server];
  304.         //Abrir conexion  
  305.         $ch = curl_init();  
  306.         curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
  307.         curl_setopt($ch,CURLOPT_URL,$servidor);
  308.         curl_setopt($ch,CURLOPT_POST,1);
  309.         curl_setopt($ch,CURLOPT_POSTFIELDS,$params);
  310.         curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  311.         // ESTO ES PARA IMAGESHACK NO TODOS LOS SERVIDORES LO SOPORTAN
  312.         if($this->server == 'imgshack')
  313.             curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
  314.         // RESULTADO
  315.         $result = curl_exec($ch);
  316.         curl_close($ch);
  317.         return $result;
  318.     }
  319.     /*
  320.         setParams()
  321.     */
  322.     function setParams($url){
  323.         switch($this->server){
  324.             case 'imgur':
  325.                 return 'image='.$url;
  326.             break;
  327.             case 'imgshack':
  328.                 return 'MAX_FILE_SIZE=13145728&refer=http://imageshack.us/&brand=&optimage=resample&url='.$url;
  329.             break;
  330.         }
  331.     }
  332.     /**
  333.      * @name getImagenUrl($html)
  334.      * @access public
  335.      * @param string
  336.      * @return string
  337.      * @version 1.1
  338.     */
  339.     public function getImagenUrl($code){
  340.         //
  341.         switch($this->server){
  342.             case 'imgur':
  343.                 global $tsCore;
  344.                 //
  345.                 $image_data = $tsCore->setJSON($code, 'decode');
  346.                 $src = $image_data->upload->links->original;
  347.                 //$src = str_replace('//','//i.',$src); // PARA EVITAR REDIRECCIONES
  348.                 return $src;
  349.             break;
  350.             // IMAGESHACK
  351.             case 'imgshack':
  352.                 $links = explode('Please <',$code);
  353.                 $links = explode('" />',$links[1]);
  354.                 $link = explode('"',$links[0]);
  355.                 $total = count($link);
  356.                 return $link[$total-1];
  357.             break;
  358.         }
  359.     }
  360.      
  361. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement