PHP批量生成缩略图的代码


Posted in PHP onJuly 19, 2008

缺点:长宽不一的图片会被拉伸变形,不能智能裁切,需要智能裁切的,请自行研究。
<?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; 
?>

PHP 相关文章推荐
PHP 和 MySQL 开发的 8 个技巧
Jan 02 PHP
解析Win7 XAMPP apache无法启动的问题
Jun 26 PHP
Thinkphp中Create方法深入探究
Jun 16 PHP
CodeIgniter配置之routes.php用法实例分析
Jan 19 PHP
php使用timthumb生成缩略图的方法
Jan 22 PHP
Yii统计不同类型邮箱数量的方法
Oct 18 PHP
php将html转为图片的实现方法
May 19 PHP
PHP中OpenSSL加密问题整理
Dec 14 PHP
php使用imagecopymerge()函数创建半透明水印
Jan 25 PHP
php实现生成带二维码图片并强制下载功能
Feb 24 PHP
PHP基于PDO扩展操作mysql数据库示例
Dec 24 PHP
PHP+ajax实现上传、删除、修改单张图片及后台处理逻辑操作详解
Feb 12 PHP
Discuz 模板引擎的封装类代码
Jul 18 #PHP
PHP令牌 Token改进版
Jul 18 #PHP
php下intval()和(int)转换使用与区别
Jul 18 #PHP
PHP入门学习的几个不错的实例代码
Jul 13 #PHP
php header()函数使用说明
Jul 10 #PHP
php下实现一个阿拉伯数字转中文数字的函数
Jul 10 #PHP
兼容PHP5的PHP目录管理函数库
Jul 10 #PHP
You might like
adodb与adodb_lite之比较
2006/12/31 PHP
Mysql的GROUP_CONCAT()函数使用方法
2008/03/28 PHP
关于IIS php调用com组件的权限问题
2012/01/11 PHP
Php连接及读取和写入mysql数据库的常用代码
2014/08/11 PHP
PHP设计模式之策略模式原理与用法实例分析
2019/04/04 PHP
php使用event扩展的io复用测试的示例
2020/10/20 PHP
document.all与WEB标准
2020/05/13 Javascript
JS 排序输出实现table行号自增前端动态生成的tr
2014/08/13 Javascript
jQuery验证插件validation使用指南
2015/04/21 Javascript
JavaScript的函数式编程基础指南
2016/03/19 Javascript
微信小程序 自己制作小组件实例详解
2016/12/22 Javascript
Vue.js 60分钟快速入门教程
2017/03/28 Javascript
基于AngularJS的简单使用详解
2017/09/10 Javascript
nuxt+axios解决前后端分离SSR的示例代码
2017/10/24 Javascript
微信小程序switch组件使用详解
2018/01/31 Javascript
JS/HTML5游戏常用算法之碰撞检测 像素检测算法实例详解
2018/12/12 Javascript
nodejs实现聊天机器人功能
2019/09/19 NodeJs
vue 实现input表单元素的disabled示例
2019/10/28 Javascript
[01:39]2014DOTA2国际邀请赛 Newbee经理CU专访队伍火力全开
2014/07/15 DOTA
python实现在每个独立进程中运行一个函数的方法
2015/04/23 Python
Python的Bottle框架中实现最基本的get和post的方法的教程
2015/04/30 Python
Python中逗号的三种作用实例分析
2015/06/08 Python
Python实现发送QQ邮件的封装
2017/07/14 Python
Python函数装饰器实现方法详解
2018/12/22 Python
Python函数装饰器常见使用方法实例详解
2019/03/30 Python
python Opencv计算图像相似度过程解析
2019/12/03 Python
PyTorch里面的torch.nn.Parameter()详解
2020/01/03 Python
手把手教你从PyCharm安装到激活(最新激活码),亲测有效可激活至2089年
2020/11/25 Python
Scrapy-Redis之RedisSpider与RedisCrawlSpider详解
2020/11/18 Python
HTML5 embed 标签使用方法介绍
2013/08/13 HTML / CSS
澳大利亚汽车零部件、音响及配件超市:Automotive Superstore
2018/06/19 全球购物
alice McCALL官网:澳大利亚时尚品牌
2020/11/16 全球购物
小学英语教师2015年度个人工作总结
2015/10/14 职场文书
2016年领导干部廉政承诺书
2016/03/24 职场文书
golang 语言中错误处理机制
2021/08/30 Golang
一小时学会TensorFlow2之基本操作2实例代码
2021/09/04 Python