Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @autor: kid_goth
- * @Description: Herramienta en el conocido lenguage php para la generacion de thumbnails
- * sobre las imagenes contenidas en una carpeta.
- * @Creacion: 13/04/2013
- * @Actualizacion: 04/07/2013 (Se agrego salida general a un unico formato)
- */
- @error_reporting(0);
- class clsImgThumb
- {
- public $rewrite = false; // sobreescribir imagenes (al ponerse en true se ignoran $sufijo y $carpeta)
- public $sufijo = "_thumb"; // Sufijo que se les pondra al nombre de los thumbnails generados
- public $carpeta = false; // guardar las thumbnails en una subcarpeta (true por defecto)
- public $carp = "./"; // ruta de la carpeta (./ pordefecto)
- public $width = 100; // ancho por defecto de los thumbnails
- public $height = 100; // alto por defecto de los thumbnails
- public $formato = null; // formato de salida general
- public $help = false;
- function __construct($argumentos)
- {
- $this->head();
- if (is_array($argumentos))
- {
- $this->argv2var($argumentos);
- if(!$this->help)
- {
- $this->iniciar();
- }
- $this->despedida();
- }
- }
- function iniciar()
- {
- $imgs = $this->listImg();
- if($this->carpeta)
- {
- if(!file_exists('./thumbnails'))
- mkdir('./thumbnails');
- $this->carp = './thumbnails';
- }
- if($this->rewrite)
- {
- $this->carp = './';
- $this->sufijo = "";
- }
- print "\r\nProcesando imagenes...\r\n";
- foreach ($imgs as $img)
- {
- $this->resize($img['i'], $img['e']);
- }
- }
- function head()
- {
- print " ";
- print " MM NMMMMM MMMMMMMMMN MMM MMM MMM MMM MMMMN SMMMM MMMMMMMM ";
- print " IMM NMMO MMM MMMMMMMMMN MMM MMM MMM MMM MMMMM MMMMM MMM SMMM ";
- print " ZMMMMM, MMMM MMM MMMMMMMMMM MMM MMM MMMMM:,MMMMM MMM8DMMMD ";
- print " MMMM MMM MMM MMMMMMMMMM MMM MMM MMM~MMMM?MMM MMMMMMMMM ";
- print " DMMMMM MMM MMM MMM MMM$ MMM MMM MMMM MMM MMM MMM8 ";
- print " ,MM 7MM MMM MMM MMM MMMMMMMM MMM NMMM MMM MMMMMMMMM ";
- print " M :: ,:: ::: ::: :MM7 ::, :: ::: :::::: ";
- print " ";
- print " by kid_goth ";
- }
- function help()
- {
- print "\r\n";
- 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.";
- print "\r\n\r\nSe ejecuta de la siguiente manera: \r\n";
- print "\r\nshell> php " . basename(__FILE__) . "-arg1=valor -arg2=valor...\r\n\r\nArgumentos validos:\r\n\r\n";
- print "[*] -r con este argumento las imagenes se sobreescribiran asi que ten cuidado\r\n";
- print " si las necesitas en tamaƱo original. (se ignoran -c y -s)\r\n\r\n";
- print "[*] -s sufijo que se pondra al final del nombre de las img (default '_thumb')\r\n\r\n";
- print "[*] -w ancho de los thumb en pixeles (default 100), solo poner los numeros\r\n\r\n";
- print "[*] -h alto de los thumb en pixeles (default 100), solo poner los numeros\r\n\r\n";
- print "[*] -c usela (sin valor) si quiere que los thumbnails se guarden en una\r\n";
- print " subcarpeta\r\n\r\n";
- print "[*] -f Este parametro indica el formato de dalida por defecto que desea\r\n";
- print " formatos permitidos jpg, gif y/o png. (Al usar, se guardaran las\r\nimagenes en una subcarpeta)\r\n\r\n";
- print "[*] -? Imprime esta ayuda\r\n";
- print "\r\nEjemplos de uso:\r\n";
- 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";
- print "[*] shell> php " . basename(__FILE__) . " -r\r\n(sobreescribira las imagenes existentes dejandolas de 100x100)\r\n\r\n";
- 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";
- }
- /*
- * Funcion para realizar el redimencionado de las imagenes
- */
- function resize($file, $ext)
- {
- switch ($ext) {
- case 'jpg':
- case 'jpeg':
- $im = @imagecreatefromjpeg($file);
- break;
- case 'gif':
- $im = @imagecreatefromgif($file);
- break;
- case 'png':
- $im = @imagecreatefrompng($file);
- break;
- }
- if(isset($this->formato) && $this->formato != null)
- {
- $ext = $this->formato;
- }
- switch ($ext) {
- case 'jpg':
- case 'jpeg':
- if($im)
- {
- $thumb = imagecreatetruecolor($this->width, $this->height);
- imagecopyresampled($thumb, $im, 0, 0, 0, 0, $this->width, $this->height, imagesx($im), imagesy($im));
- imagejpeg($thumb, $this->carp ."/". substr($file, 0, -4) . $this->sufijo . "." . $ext, 95);
- print "\r\n[-] Exito: " . $file . " ($this->width x $this->height)";
- }else { print "\r\n[X] Error: " . $file; }
- break;
- case 'gif':
- if($im)
- {
- $thumb = imagecreatetruecolor($this->width, $this->height);
- imagecopyresampled($thumb, $im, 0, 0, 0, 0, $this->width, $this->height, imagesx($im), imagesy($im));
- imagegif($thumb, $this->carp ."/". substr($file, 0, -4) . $this->sufijo . "." . $ext, 95);
- print "\r\n[-] Exito: " . $file . " ($this->width x $this->height)";
- }else { print "\r\n[X] Error: " . $file; }
- break;
- case 'png':
- if($im)
- {
- $thumb = imagecreatetruecolor($this->width, $this->height);
- imagecopyresampled($thumb, $im, 0, 0, 0, 0, $this->width, $this->height, imagesx($im), imagesy($im));
- imagepng($thumb, $this->carp ."/". substr($file, 0, -4) . $this->sufijo . "." . $ext, 0);
- print "\r\n[-] Exito: " . $file . " ($this->width x $this->height)";
- }else { print "\r\n[X] Error: " . $file; }
- break;
- }
- imagedestroy($im);
- }
- /*
- * Funcion para realizar el listado de las imagenes que se encuentran en la carpeta
- */
- function listImg()
- {
- $dir = opendir(".");
- $imgs = array();
- $ext = array('png', 'jpg', 'jpeg', 'gif');
- while ($arc = readdir($dir)) {
- if(is_file($arc))
- {
- $ft = strtolower(substr($arc, -3));
- if(in_array($ft, $ext))
- {
- $imgs[] = array('i' => $arc, 'e' => $ft);
- }
- }
- }
- return $imgs;
- }
- /*
- * Funcion para el proceso de los argumentos
- */
- function argv2var($arr_argv)
- {
- //print_r($arr_argv);die();
- foreach ($arr_argv as $value)
- {
- $flag = substr($value, 0, 3);
- if(strpos("-?", $flag) === 0)
- {
- $this->help();
- $this->help = true;
- }
- if(strpos("-r", $flag) === 0)
- {
- $this->rewrite = true;
- }
- if(strpos("-s=", $flag) === 0)
- {
- $this->sufijo = substr($value, 3);
- }
- if(strpos("-c", $flag) === 0)
- {
- $this->carpeta = true;
- }
- if(strpos("-f=", $flag) === 0)
- {
- $this->formato = substr($value, 3);
- $this->carpeta = true;
- }
- if(strpos("-w=", $flag) === 0)
- {
- $this->width = substr($value, 3);
- }
- if(strpos("-h=", $flag) === 0)
- {
- $this->height = substr($value, 3);
- }
- }
- }
- function despedida()
- {
- print "\r\n\r\nEspero que la tool te haya servido, recuerda que puedes";
- print "hacer uso del codigo como lo desees, prueba modifica, destruye y vuelve a";
- print "reparar pero que nunca se te olvide COMPARTE..!!.\r\n\r\nSaludos kid_goth";
- }
- }
- $objImgThumb = new clsImgThumb($argv);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment