php批量缩放图片的代码[ini参数控制]


Posted in PHP onFebruary 11, 2011

首先使用一个ini文件来设置要缩放的大小,其中为宽或高0的则为图片放大或缩小,都为0则还是原大小,都不为0都拉抻成指定的大小。

注意:ini文件使用php解释时为注释文件,什么也没有输出,这是为了安全起见而故意为之。而;则是ini文件的注释。

我设置的ini文件例子如下:

<?php 
/* 
;Translate the image format using the original image size 
[Translation] 
width=0 
height=0 ;Stretch the image to the specified size 
[Stretch] 
width=800 
height=600 
;Zoom the image to the specified Width with height auto size 
[AutoHeight] 
width=740 
height=0 
;Zoom the image to the specified Height with width auto size 
[AutoWidth] 
width=0 
height=380 
*/ 
?>

下面是编写的缩放图片的php代码,其中变量classes是一个数组,可以选择任意多个ini文件中指定的设置:
<?php 
$oimg = "test.jpg";//Original image name 
$classes = array('Translation','AutoHeight','AutoWidth','Stretch');//Give classes for the new creating images' size which are defined in the specified ini file 
$suffix = 'jpg';//The new image's suffix 
$inifile = 'image.ini.php'; $size = getimagesize($oimg); 
$x = $size[0]/$size[1]; 
$name = explode('.',$oimg); 
if(!file_exists($inifile)) die('Ini file does not exist!'); 
$cn = parse_ini_file($inifile,true);//Parse the class style image size from ini file 
foreach($classes as $class){ 
foreach($cn as $k=>$v){ 
if($k==$class){ 
if($v['width'] && $v['height']){ 
$thumbWidth = $v['width']; 
$thumbHeight = $v['height']; 
}elseif($v['width']){ 
$thumbWidth = $v['width']; 
$thumbHeight = round($thumbWidth/$x); 
}elseif($v['height']){ 
$thumbHeight = $v['height']; 
$thumbWidth = round($thumbHeight*$x); 
}else{ 
$thumbWidth = $size[0]; 
$thumbHeight = $size[1]; 
} 
break; 
} 
} 
if(!isset($thumbHeight) && !isset($thumbWidth)) die('Ini file Settings error!'); 
$nimg = $name[0].'_'.$class.'.'.$suffix;//New image file name 
$source = imagecreatefromjpeg($oimg); 
$thumb = imagecreatetruecolor($thumbWidth, $thumbHeight); 
imagecopyresampled($thumb,$source,0,0,0,0,$thumbWidth,$thumbHeight,$size[0],$size[1]); 
if($suffix=='jpg') $method = 'imagejpeg'; 
else $method='image'.$suffix; 
$method($thumb, $nimg); 
imagedestroy($thumb);//Release the image source 
imagedestroy($source); 
} 
?>
PHP 相关文章推荐
PHP4实际应用经验篇(8)
Oct 09 PHP
最新的php 文件上传模型,支持多文件上传
Aug 13 PHP
PHP 柱状图实现代码
Dec 04 PHP
php preg_filter执行一个正则表达式搜索和替换
Feb 27 PHP
PHP imagegrabscreen和imagegrabwindow(截取网站缩略图)的实例代码
Nov 07 PHP
PHP闭包函数传参及使用外部变量的方法
Mar 15 PHP
PHP+Apache+Mysql环境搭建教程
Aug 01 PHP
php计算给定日期所在周的开始日期和结束日期示例
Feb 06 PHP
PHP ADODB实现事务处理功能示例
May 25 PHP
PHP实现正则匹配所有括号中的内容
Jun 22 PHP
PHP判断是否微信访问的方法示例
Mar 27 PHP
PHP 结合 Boostrap 结合 js 实现学生列表删除编辑及搜索功能
May 21 PHP
让PHP以ROOT权限执行系统命令的方法
Feb 10 #PHP
PHP开发中常用的字符串操作函数
Feb 08 #PHP
php提交表单时判断 if($_POST[submit])与 if(isset($_POST[submit])) 的区别
Feb 08 #PHP
php 数组的指针操作实现代码
Feb 08 #PHP
PHP游戏编程25个脚本代码
Feb 08 #PHP
PHP通用检测函数集合
Feb 08 #PHP
.htaccess文件保护实例讲解
Feb 06 #PHP
You might like
ThinkPHP3.1新特性之G方法的使用
2014/06/19 PHP
浅谈PHP正则表达式中修饰符/i, /is, /s, /isU
2014/10/21 PHP
弹出模态框modal的实现方法及实例
2017/09/19 PHP
PHP操作Redis常用技巧总结
2018/04/24 PHP
纯js分页代码(简洁实用)
2013/11/05 Javascript
JS中的数组的sort方法使用示例
2014/01/22 Javascript
Jquery动态替换div内容及动态展示的方法
2015/01/23 Javascript
jquery使用animate方法实现控制元素移动
2015/03/27 Javascript
jquery读写cookie操作实例分析
2015/12/24 Javascript
详解javascript高级定时器
2015/12/31 Javascript
jQuery中常用动画效果函数(日常整理)
2016/09/17 Javascript
JS验证全角与半角及相互转化的介绍
2017/05/18 Javascript
vue 进阶之实现父子组件间的传值
2019/04/26 Javascript
js 递归json树实现根据子id查父id的方法分析
2019/11/08 Javascript
JavaScript实现多球运动效果
2020/09/07 Javascript
在nuxt中使用路由重定向的实例
2020/11/06 Javascript
Vue+Spring Boot简单用户登录(附Demo)
2020/11/12 Javascript
[03:15]DOTA2-DPC中国联赛1月22日Recap集锦
2021/03/11 DOTA
python备份文件以及mysql数据库的脚本代码
2013/06/10 Python
Python中的jquery PyQuery库使用小结
2014/05/13 Python
Python正则替换字符串函数re.sub用法示例
2017/01/19 Python
Diango + uwsgi + nginx项目部署的全过程(可外网访问)
2018/04/22 Python
详解用TensorFlow实现逻辑回归算法
2018/05/02 Python
Python3.5以上版本lxml导入etree报错的解决方案
2019/06/26 Python
用Python识别人脸,人种等各种信息
2019/07/15 Python
解决python执行较大excel文件openpyxl慢问题
2020/05/15 Python
Python大批量搜索引擎图像爬虫工具详解
2020/11/16 Python
纯CSS3实现圆角效果(含IE兼容解决方法)
2014/05/07 HTML / CSS
html5新特性与用法大全
2018/09/13 HTML / CSS
墨尔本复古时尚品牌:Dangerfield
2018/12/12 全球购物
在C++ 程序中调用被C 编译器编译后的函数,为什么要加extern "C"
2014/08/09 面试题
采购部部门职责
2013/12/15 职场文书
写给女生的道歉信
2014/01/14 职场文书
城市规划应届毕业生自荐信
2014/07/04 职场文书
劳模事迹材料范文
2014/12/24 职场文书
2016年教师师德师风承诺书
2016/03/25 职场文书