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服务器
Oct 09 PHP
纯真IP数据库的应用 IP地址转化成十进制
Jun 14 PHP
PHP is_dir() 判断给定文件名是否是一个目录
May 10 PHP
php站内搜索并高亮显示关键字的实现代码
Dec 29 PHP
深入解析php之sphinx
May 15 PHP
PHP框架Swoole定时器Timer特性分析
Aug 19 PHP
ThinkPHP中RBAC类的四种用法分析
Nov 24 PHP
php备份数据库类分享
Apr 14 PHP
php线性表的入栈与出栈实例分析
Jun 12 PHP
PHP使用数组实现矩阵数学运算的方法示例
May 29 PHP
CakePHP框架Model函数定义方法示例
Aug 04 PHP
php设计模式之适配器模式实例分析【星际争霸游戏案例】
Apr 07 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
php获取服务器信息的实现代码
2013/02/04 PHP
Symfony生成二维码的方法
2016/02/04 PHP
PHP实现的登录,注册及密码修改功能分析
2016/11/25 PHP
php获取POST数据的三种方法实例详解
2016/12/20 PHP
PHP数组中头部和尾部添加元素的方法(array_unshift,array_push)
2017/04/10 PHP
js模拟弹出效果代码修正版
2008/08/07 Javascript
Web 前端设计模式--Dom重构 提高显示性能
2010/10/22 Javascript
用JS判别浏览器种类以及IE版本的几种方法小结
2011/08/02 Javascript
查看源码的工具 学习jQuery源码不错的工具
2011/12/26 Javascript
extjs两个tbar问题探讨
2013/08/08 Javascript
JS获取html对象的几种方式介绍
2013/12/05 Javascript
jQuery focus和blur事件的应用详解
2014/01/26 Javascript
jQuery+CSS3实现树叶飘落特效
2015/02/01 Javascript
js实现文本框选中的方法
2015/05/26 Javascript
基于jquery实现下拉框美化特效
2016/02/02 Javascript
在使用JSON格式处理数据时应该注意的问题小结
2017/05/20 Javascript
vue中的scope使用详解
2017/10/29 Javascript
javascript移动端 电子书 翻页效果实现代码
2019/09/07 Javascript
Python语言技巧之三元运算符使用介绍
2013/03/04 Python
Python实现合并两个列表的方法分析
2018/05/28 Python
在IPython中进行Python程序执行时间的测量方法
2018/11/01 Python
numpy库与pandas库axis=0,axis= 1轴的用法详解
2019/05/27 Python
Python 动态导入对象,importlib.import_module()的使用方法
2019/08/28 Python
selenium+python配置chrome浏览器的选项的实现
2020/03/18 Python
python中adb有什么功能
2020/06/07 Python
python怎么调用自己的函数
2020/07/01 Python
HTML5+CSS3实现无插件拖拽上传图片(支持预览与批量)
2017/01/05 HTML / CSS
Spartoo美国:欧洲排名第一的在线时装零售商
2019/12/12 全球购物
求职简历的自我评价
2014/01/31 职场文书
自荐信的格式
2014/03/10 职场文书
蟋蟀的住宅教学反思
2014/04/26 职场文书
个人四风问题对照检查材料
2014/09/26 职场文书
学校查摆问题整改措施
2014/09/28 职场文书
先进个人主要事迹怎么写
2015/11/04 职场文书
go语言中fallthrough的用法说明
2021/05/06 Golang
Spring Cloud OpenFeign模版化客户端
2022/06/25 Java/Android