欢迎来到福编程网,本站提供各种互联网专业知识!

PHP批量生成缩略图的代码

发布时间:2008-07-19 作者: 来源:转载
本文件可以用于自动创建目录下所有JPG图片的缩略图,放到图片目录下运行即可。
缺点:长宽不一的图片会被拉伸变形,不能智能裁切,需要智能裁切的,请自行研究。
$config=array();
$config['path']="./";
$config['t_width']=120;
$config['t_height']=98;
$config['ignore']=array("",".","..");
$config['prefix']="thumb_";
$done=0;
define("IMAGE_JPG",2);
define("ENDL","n");
if($handle=opendir($config['path'])){
while(false!==($file=readdir($handle))){
if(!array_search($file,$config['ignore'])){

list($im_width,$im_height,$type)=getimagesize($file);
if($type!=IMAGE_JPG){
continue;
}

$op.="found->$file".ENDL;
$im=@imagecreatefromjpeg($file);
if(!$im){
$op.="fail->couldn'tcreatesourimagepointer.".ENDL;
continue;
}

if(file_exists($config['prefix'].$file)||substr($file,0,strlen($config['prefix']))==$config['prefix']){
$op.="note->thisfilehasalreadygotathumbnail.".ENDL;
continue;
}
$to=imagecreatetruecolor($config['t_width'],$config['t_height']);
if(!$to){
$op.="fail->couldn'tcreatedestimagepointer.".ENDL;
continue;
}

if(!imagecopyresampled($to,$im,0,0,0,0,$config['t_width'],$config['t_height'],$im_width,$im_height)){
$op.="fail->couldn'tcreatethumbnail.phpfail.".ENDL;
continue;
}

//保存文件
imagejpeg($to,$config['prefix'].$file);
$op.="done->createdthumb:{$config['prefix']}{$file}".ENDL;
$done++;
}
}
}
closedir($handle);
$op.="fin->{$done}file(s)written".ENDL;
echo"

"; 
echo$op;
echo"
";
exit;
?>

相关推荐