Con las funciones de imagen de la libreria GD en esta simple funcion
Código PHP:
function thumbnail($file,$dir,$ancho=100,$alto=100,$gif_support=false) { if(!file_exists($file)){ return false; } if(!is_dir($dir)){ return false; } $final = (int) strlen($file) - 4; $nombre = strtolower(substr($file,0,$final)); $ext = strtolower(substr($file,-4,4)); $nombre = $nombre.$ext; $mini = $dir."/mini_".$nombre; if($ext==".jpg" || $ext==".jpeg"){ $origen = imageCreateFromJPEG($file); $imgAncho= imageSx($origen); $imgAlto = imageSy($origen); $imagen = imageCreate($ancho,$alto); ImageCopyResized($imagen,$origen,0,0,0,0,$ancho,$alto,$imgAncho,$imgAlto); imageJPEG($imagen,$mini); }else if($ext==".png"){ $origen = imageCreateFromPNG($file); $imgAncho= imageSx($origen); $imgAlto = imageSy($origen); $imagen = imageCreate($ancho,$alto); ImageCopyResized($imagen,$origen,0,0,0,0,$ancho,$alto,$imgAncho,$imgAlto); imagePNG($imagen,$mini); }else if(($gif_support==true) && ($ext==".gif")){ $origen = imageCreateFromGIF($file); $imgAncho= imageSx($origen); $imgAlto = imageSy($origen); $imagen = imageCreate($ancho,$alto); ImageCopyResized($imagen,$origen,0,0,0,0,$ancho,$alto,$imgAncho,$imgAlto); imageGIF($imagen,$mini); }else{ return false; } return true; }
Como usarla?
Sencillo, le pasas a la funcion el nombre del archivo y el directorio en que quieres que queden creadas las thumbnails.
En forma de opcion esta que le pases el alto y ancho que por defecto es 100 de ancho y de alto.
Tambien si tu libreria GD soporta gifs le pasas true a la opcion de gif.
Código PHP:
thumbnail( $file , $dir , [$ancho = 100] , [$alto = 100] , [$gif_support=false] );
Ejemplo de uso
Código PHP:
if(!thumbnail("imagen.png","./thumbnails",80,80)){ echo "error en creacion de thumbnails"; }else{ echo "Creacion exitosa";