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 相关文章推荐
phpize的深入理解
Jun 03 PHP
解析PHP中DIRECTORY_SEPARATOR,PATH_SEPARATOR两个常量的作用
Jun 21 PHP
Linux编译升级php的详细方法
Nov 04 PHP
php自定义函数截取汉字长度
May 15 PHP
让ThinkPHP支持大小写url地址访问的方法
Oct 31 PHP
基于PHP实现简单的随机抽奖小程序
Jan 05 PHP
PHP数组游标实现对数组的各种操作详解
Jan 26 PHP
ThinkPHP中limit()使用方法详解
Apr 19 PHP
PHP设计模式之迭代器模式
Jun 17 PHP
PHP使用栈解决约瑟夫环问题算法示例
Aug 27 PHP
php递归函数怎么用才有效
Feb 24 PHP
php实现微信支付之企业付款
May 30 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
php 什么是PEAR?
2009/03/19 PHP
php 无限级分类学习参考之对ecshop无限级分类的解析 带详细注释
2010/03/23 PHP
PHP header()函数常用方法总结
2014/04/11 PHP
PHP基于SPL实现的迭代器模式示例
2018/04/22 PHP
PHP实现普通hash分布式算法简单示例
2018/08/06 PHP
PHP框架实现WebSocket在线聊天通讯系统
2019/11/21 PHP
js prototype截取字符串函数
2010/04/01 Javascript
jquery延迟加载外部js实现代码
2013/01/11 Javascript
js类型转换与引用类型详解(Boolean_Number_String)
2014/03/07 Javascript
JavaScript onkeydown事件入门实例(键盘某个按键被按下)
2014/10/17 Javascript
基于jquery实现等比缩放图片
2014/12/03 Javascript
Javascript中实现trim()函数的两种方法
2015/02/04 Javascript
jQuery链式调用与show知识浅析
2016/05/11 Javascript
浅析angularJS中的ui-router和ng-grid模块
2016/05/20 Javascript
JQueryEasyUI之DataGrid数据显示
2016/11/23 Javascript
学习vue.js计算属性
2016/12/03 Javascript
bootstrap suggest下拉框使用详解
2017/04/10 Javascript
jQuery中.attr()和.data()的区别分析
2017/09/03 jQuery
详解JavaScript的变量
2019/04/04 Javascript
浅述python中argsort()函数的实例用法
2017/03/30 Python
python3中eval函数用法使用简介
2019/08/02 Python
Python turtle库绘制菱形的3种方式小结
2019/11/23 Python
django框架forms组件用法实例详解
2019/12/10 Python
python实现拼图小游戏
2020/02/22 Python
python 爬取英雄联盟皮肤并下载的示例
2020/12/04 Python
英国皇家邮政海外旗舰店:Royal Mail
2018/02/21 全球购物
法律专业自我鉴定
2013/10/03 职场文书
售房委托书
2014/08/30 职场文书
教育实践活动对照检查材料
2014/09/23 职场文书
未中标通知书
2015/04/17 职场文书
初中政教处工作总结
2015/08/12 职场文书
2019升学宴主持词范本5篇
2019/10/09 职场文书
基于Redis过期事件实现订单超时取消
2021/05/08 Redis
Python中requests做接口测试的方法
2021/05/30 Python
面试中canvas绘制图片模糊图片问题处理
2022/03/13 Javascript
鸿蒙3.0体验感怎么样? 鸿蒙3.0系统评测向
2022/08/14 数码科技