自动批量生成缩略图

sucn| 阅读:4056 发表时间:2004-12-18 经验技巧

这是生成缩略图的源程序,把文件放目录下,如果目录下有图片会自动生成小图片。适合批量生成小图,做图片站的能用的着吧。

[code]$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't create sour image pointer." . ENDL;
continue;
}

if(file_exists($config['prefix'] . $file) || substr($file, 0, strlen($config['prefix'])) == $config['prefix']) {
$op .= "note -> this file has already got a thumbnail." . ENDL;
continue;
}

$to = imagecreatetruecolor($config['t_width'],$config['t_height']);
if(!$to) {
$op .= "fail -> couldn't create dest image pointer." . ENDL;
continue;
}

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

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

echo "

"; 
echo $op;
echo "
";

exit;

?> [/code]
*文章为原作者独立观点,不代表网站的立场
本文由网站发表并编辑,转载此文章须经作者同意,并请附上出处及本页链接。如有侵权,请联系本站删除。

友情连接