bssanchez93

IMG Thumb Generator v2

Jun 9th, 2013
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.67 KB | None | 0 0
  1. <?php
  2.     /**
  3.     * @autor: kid_goth
  4.     * @Description: Herramienta en el conocido lenguage php para la generacion de thumbnails
  5.     *       sobre las imagenes contenidas en una carpeta.
  6.     * @Creacion: 13/04/2013
  7.     * @Actualizacion: 04/07/2013 (Se agrego salida general a un unico formato)
  8.     */
  9.     @error_reporting(0);
  10.     class clsImgThumb
  11.     {
  12.         public $rewrite = false;    // sobreescribir imagenes (al ponerse en true se ignoran $sufijo y $carpeta)
  13.         public $sufijo = "_thumb";  // Sufijo que se les pondra al nombre de los thumbnails generados
  14.         public $carpeta = false;    // guardar las thumbnails en una subcarpeta (true por defecto)
  15.         public $carp = "./";        // ruta de la carpeta (./ pordefecto)
  16.         public $width = 100;        // ancho por defecto de los thumbnails
  17.         public $height = 100;       // alto por defecto de los thumbnails
  18.         public $formato = null;     // formato de salida general
  19.  
  20.         public $help = false;
  21.  
  22.         function __construct($argumentos)
  23.         {
  24.             $this->head();
  25.             if (is_array($argumentos))
  26.             {
  27.                 $this->argv2var($argumentos);
  28.                 if(!$this->help)
  29.                 {
  30.                     $this->iniciar();
  31.                 }
  32.                 $this->despedida();
  33.             }
  34.         }
  35.  
  36.         function iniciar()
  37.         {
  38.             $imgs = $this->listImg();
  39.             if($this->carpeta)
  40.             {
  41.                 if(!file_exists('./thumbnails'))
  42.                     mkdir('./thumbnails');
  43.  
  44.                 $this->carp = './thumbnails';
  45.             }
  46.  
  47.             if($this->rewrite)
  48.             {
  49.                 $this->carp = './';
  50.                 $this->sufijo = "";
  51.             }
  52.  
  53.             print "\r\nProcesando imagenes...\r\n";
  54.             foreach ($imgs as $img)
  55.             {
  56.                 $this->resize($img['i'], $img['e']);
  57.             }
  58.         }
  59.  
  60.         function head()
  61.         {
  62.             print "                                                                                ";
  63.             print "        MM NMMMMM  MMMMMMMMMN MMM    MMM  MMM    MMM  MMMMN  SMMMM  MMMMMMMM    ";
  64.             print "     IMM  NMMO MMM MMMMMMMMMN MMM    MMM  MMM    MMM  MMMMM  MMMMM  MMM  SMMM   ";
  65.             print " ZMMMMM,      MMMM    MMM     MMMMMMMMMM  MMM    MMM  MMMMM:,MMMMM  MMM8DMMMD   ";  
  66.             print " MMMM        MMM      MMM     MMMMMMMMMM  MMM    MMM  MMM~MMMM?MMM  MMMMMMMMM   ";
  67.             print "   DMMMMM             MMM     MMM    MMM  MMM$   MMM  MMM MMMM MMM  MMM   MMM8  ";  
  68.             print "      ,MM   7MM       MMM     MMM    MMM   MMMMMMMM   MMM NMMM MMM  MMMMMMMMM   ";  
  69.             print "         M   ::       ,::     :::    :::     :MM7     ::,  ::  :::  ::::::      ";
  70.             print "                                                                                ";
  71.             print "                                                                   by kid_goth  ";
  72.         }
  73.  
  74.         function help()
  75.         {
  76.             print "\r\n";
  77.             print "\r\nPara hacer uso de la herramienta debe tener en cuenta que el script debe encontrarse dentro de la carpeta de imagenes que se van a procesar.";
  78.  
  79.             print "\r\n\r\nSe ejecuta de la siguiente manera: \r\n";
  80.             print "\r\nshell> php " . basename(__FILE__) . "-arg1=valor -arg2=valor...\r\n\r\nArgumentos validos:\r\n\r\n";
  81.  
  82.             print "[*] -r   con este argumento las imagenes se sobreescribiran asi que ten cuidado\r\n";
  83.             print "         si las necesitas en tamaƱo original. (se ignoran -c y -s)\r\n\r\n";
  84.             print "[*] -s   sufijo que se pondra al final del nombre de las img (default '_thumb')\r\n\r\n";
  85.             print "[*] -w   ancho de los thumb en pixeles (default 100), solo poner los numeros\r\n\r\n";
  86.             print "[*] -h   alto de los thumb en pixeles (default 100), solo poner los numeros\r\n\r\n";
  87.             print "[*] -c   usela (sin valor) si quiere que los thumbnails se guarden en una\r\n";
  88.             print "         subcarpeta\r\n\r\n";
  89.             print "[*] -f   Este parametro indica el formato de dalida por defecto que desea\r\n";
  90.             print "         formatos permitidos jpg, gif y/o png. (Al usar, se guardaran las\r\nimagenes en una subcarpeta)\r\n\r\n";
  91.             print "[*] -?   Imprime esta ayuda\r\n";
  92.  
  93.             print "\r\nEjemplos de uso:\r\n";
  94.             print "[*] shell> php " . basename(__FILE__) . " -w=200 -h=250 -c\r\n(Generara los thumbs en una subcarpeta con el sufijo _thumb en las imagenes de 200x250)\r\n\r\n";
  95.             print "[*] shell> php " . basename(__FILE__) . " -r\r\n(sobreescribira las imagenes existentes dejandolas de 100x100)\r\n\r\n";
  96.             print "[*] shell> php " . basename(__FILE__) . " -t=_min -w=200\r\n(Creara los thumbnails en la misma carpeta de 200x100 con el sufijo _min)\r\n";
  97.         }
  98.  
  99.         /*
  100.         * Funcion para realizar el redimencionado de las imagenes
  101.         */
  102.  
  103.         function resize($file, $ext)
  104.         {
  105.             switch ($ext) {
  106.                 case 'jpg':
  107.                 case 'jpeg':
  108.                     $im = @imagecreatefromjpeg($file);
  109.                     break;
  110.                 case 'gif':
  111.                     $im = @imagecreatefromgif($file);
  112.                     break;
  113.                 case 'png':
  114.                     $im = @imagecreatefrompng($file);
  115.                     break;
  116.             }
  117.  
  118.  
  119.             if(isset($this->formato) && $this->formato != null)
  120.             {
  121.                 $ext = $this->formato;
  122.             }
  123.  
  124.             switch ($ext) {
  125.                 case 'jpg':
  126.                 case 'jpeg':
  127.                     if($im)
  128.                     {
  129.                         $thumb = imagecreatetruecolor($this->width, $this->height);
  130.                         imagecopyresampled($thumb, $im, 0, 0, 0, 0, $this->width, $this->height, imagesx($im), imagesy($im));
  131.  
  132.                         imagejpeg($thumb, $this->carp ."/". substr($file, 0, -4) . $this->sufijo . "." . $ext, 95);
  133.                         print "\r\n[-] Exito: " . $file . " ($this->width x $this->height)";
  134.                     }else { print "\r\n[X] Error: " . $file; }
  135.                     break;
  136.                 case 'gif':
  137.                     if($im)
  138.                     {
  139.                         $thumb = imagecreatetruecolor($this->width, $this->height);
  140.                         imagecopyresampled($thumb, $im, 0, 0, 0, 0, $this->width, $this->height, imagesx($im), imagesy($im));
  141.  
  142.                         imagegif($thumb, $this->carp ."/". substr($file, 0, -4) . $this->sufijo . "." . $ext, 95);
  143.                         print "\r\n[-] Exito: " . $file . " ($this->width x $this->height)";
  144.                     }else { print "\r\n[X] Error: " . $file; }
  145.                     break;
  146.                 case 'png':
  147.                     if($im)
  148.                     {
  149.                         $thumb = imagecreatetruecolor($this->width, $this->height);
  150.                         imagecopyresampled($thumb, $im, 0, 0, 0, 0, $this->width, $this->height, imagesx($im), imagesy($im));
  151.  
  152.                         imagepng($thumb, $this->carp ."/". substr($file, 0, -4) . $this->sufijo . "." . $ext, 0);
  153.                         print "\r\n[-] Exito: " . $file . " ($this->width x $this->height)";
  154.                     }else { print "\r\n[X] Error: " . $file; }
  155.                     break;
  156.             }
  157.             imagedestroy($im);
  158.         }
  159.  
  160.         /*
  161.         * Funcion para realizar el listado de las imagenes que se encuentran en la carpeta
  162.         */
  163.  
  164.         function listImg()
  165.         {
  166.             $dir = opendir(".");
  167.             $imgs = array();
  168.             $ext = array('png', 'jpg', 'jpeg', 'gif');
  169.  
  170.             while ($arc = readdir($dir)) {
  171.                 if(is_file($arc))
  172.                 {
  173.                     $ft = strtolower(substr($arc, -3));
  174.                     if(in_array($ft, $ext))
  175.                     {
  176.                         $imgs[] = array('i' => $arc, 'e' => $ft);
  177.                     }
  178.                 }
  179.             }
  180.  
  181.             return $imgs;
  182.         }
  183.  
  184.         /*
  185.         * Funcion para el proceso de los argumentos
  186.         */
  187.  
  188.         function argv2var($arr_argv)
  189.         {
  190.             //print_r($arr_argv);die();
  191.             foreach ($arr_argv as $value)
  192.             {
  193.                 $flag = substr($value, 0, 3);
  194.                 if(strpos("-?", $flag) === 0)
  195.                 {
  196.                     $this->help();
  197.                     $this->help = true;
  198.                 }
  199.  
  200.                 if(strpos("-r", $flag) === 0)
  201.                 {
  202.                     $this->rewrite = true;
  203.                 }
  204.  
  205.                 if(strpos("-s=", $flag) === 0)
  206.                 {
  207.                     $this->sufijo = substr($value, 3);
  208.                 }
  209.  
  210.                 if(strpos("-c", $flag) === 0)
  211.                 {
  212.                     $this->carpeta = true;
  213.                 }
  214.  
  215.                 if(strpos("-f=", $flag) === 0)
  216.                 {
  217.                     $this->formato = substr($value, 3);
  218.                     $this->carpeta = true;
  219.                 }
  220.  
  221.                 if(strpos("-w=", $flag) === 0)
  222.                 {
  223.                     $this->width = substr($value, 3);
  224.                 }
  225.  
  226.                 if(strpos("-h=", $flag) === 0)
  227.                 {
  228.                     $this->height = substr($value, 3);
  229.                 }
  230.             }
  231.         }
  232.  
  233.         function despedida()
  234.         {
  235.             print "\r\n\r\nEspero que la tool te haya servido, recuerda que puedes";
  236.             print "hacer uso del codigo como lo desees, prueba modifica, destruye y vuelve a";
  237.             print "reparar pero que nunca se te olvide COMPARTE..!!.\r\n\r\nSaludos kid_goth";
  238.         }
  239.     }
  240.    
  241.     $objImgThumb = new clsImgThumb($argv);
  242. ?>
Advertisement
Add Comment
Please, Sign In to add comment