当前位置: 首页 > 图文教程 > 网络编程 > PHP > PHP批量生成缩略图的代码

PHP
一个自定义位数的php多用户计数器代码
php实现的MySQL通用查询程序
一个简单的php实现的MySQL数据浏览器
php桌面中心(一) 创建数据库
php桌面中心(二) 数据库写入
php桌面中心(三) 修改数据库
php桌面中心(四) 数据显示
织梦模板标记简介
DedeCms模板安装/制作概述
PHP中的CMS的涵义
手把手教你使用DedeCms的采集的图文教程
利用PHP和AJAX创建RSS聚合器的代码
mysql4.1以上版本连接时出现Client does not support authentication protocol问题解决办法
Linux下进行MYSQL编程时插入中文乱码的解决方案
推荐Discuz!5的PHP代码高亮显示与实现可运行代码
不错的一篇面向对象的PHP开发模式(简写版)
用PHP实现多服务器共享SESSION数据的方法
用header 发送cookie的php代码
PHP的开发框架的现状和展望
使用 eAccelerator加速PHP代码的目的

PHP批量生成缩略图的代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 121 ::
收藏到网摘: n/a

本文件可以用于自动创建目录下所有JPG图片的缩略图,放到图片目录下运行即可。 缺点:长宽不一的图片会被拉伸变形,不能智能裁切,需要智能裁切的,请自行研究。
<?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 -> <a href='{$file}'>$file</a>" . 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: <a href='{$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;
?>