手机
当前位置:查字典教程网 >编程开发 >php教程 >PHP批量生成缩略图的代码
PHP批量生成缩略图的代码
摘要:缺点:长宽不一的图片会被拉伸变形,不能智能裁切,需要智能裁切的,请自行研究。

缺点:长宽不一的图片会被拉伸变形,不能智能裁切,需要智能裁切的,请自行研究。

<?php

$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-><ahref='{$file}'>$file</a>".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:<ahref='{$config['prefix']}{$file}'>{$config['prefix']}{$file}</a>".ENDL;

$done++;

}

}

}

closedir($handle);

$op.="fin->{$done}file(s)written".ENDL;

echo"<pre>";

echo$op;

echo"</pre>";

exit;

?>

【PHP批量生成缩略图的代码】相关文章:

PHP系统流量分析的程序

PHP生成唯一订单号的方法汇总

PHP生成带有雪花背景的验证码

php生成图片验证码

PHP生成plist数据的方法

PHP中的类型约束介绍

用PHP编写PDF文档生成器

php生成随机密码的自定义函数

PHP超牛逼无限极分类生成树方法

生成缩略图

精品推荐
分类导航