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 相关文章推荐
php继承的一个应用
Sep 06 PHP
PHP系列学习之日期函数使用介绍
Aug 18 PHP
基于PHP异步执行的常用方式详解
Jun 03 PHP
获取php页面执行时间,数据库读写次数,函数调用次数等(THINKphp)
Jun 03 PHP
php selectradio和checkbox默认选择的实现方法详解
Jun 29 PHP
php获取字段名示例分享
Mar 03 PHP
php实现斐波那契数列的简单写法
Jul 19 PHP
php自定文件保存session的方法
Dec 10 PHP
php+mysqli使用面向对象方式更新数据库实例
Jan 29 PHP
Yii+upload实现AJAX上传图片的方法
Jul 13 PHP
php中目录操作opendir()、readdir()及scandir()用法示例
Jun 08 PHP
php设计模式之组合模式实例详解【星际争霸游戏案例】
Mar 27 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生成数字字母的验证码图片
2015/07/14 PHP
PHP使用mkdir创建多级目录的方法
2015/12/22 PHP
thinkphp5.0整合phpsocketio完整攻略(绕坑)
2018/10/12 PHP
Laravel5.4框架使用socialite实现github登录的方法
2019/03/20 PHP
客户端脚本中常常出现的一些问题和调试技巧
2007/01/09 Javascript
分享一个我自己写的ToolTip提示插件(附源码)
2013/01/20 Javascript
图片延迟加载的实现代码(模仿懒惰)
2013/03/29 Javascript
js中substring和substr的详细介绍与用法
2013/08/29 Javascript
JavaScript设计模式之单例模式实例
2014/09/24 Javascript
AngularJS入门教程(二):AngularJS模板
2014/12/06 Javascript
Eclipse引入jquery报错如何解决
2015/12/01 Javascript
jquery遍历table的tr获取td的值实现方法
2016/05/19 Javascript
原JS实现banner图的常用功能
2017/06/12 Javascript
vue.js 上传图片实例代码
2017/06/22 Javascript
js Date()日期函数浏览器兼容问题解决方法
2017/09/12 Javascript
JS正则表达式完美实现身份证校验功能
2017/10/18 Javascript
利用pm2部署多个node.js项目的配置教程
2017/10/22 Javascript
jquery ajax异步提交表单数据的方法
2017/10/27 jQuery
深入浅析Vue中的slots/scoped slots
2018/04/03 Javascript
JavaScript实现网页跨年倒计时
2020/12/02 Javascript
[04:09]2014DOTA2国际邀请赛Ti西雅图 历届冠军相继出局 BBC综述今日比赛
2014/07/20 DOTA
[01:21:07]EG vs Liquid 2018国际邀请赛淘汰赛BO3 第一场 8.25
2018/08/29 DOTA
python3简单实现微信爬虫
2015/04/09 Python
Python首次安装后运行报错(0xc000007b)的解决方法
2016/10/18 Python
pycharm安装图文教程
2017/05/02 Python
使用Python爬取最好大学网大学排名
2018/02/24 Python
Python爬虫爬取杭州24时温度并展示操作示例
2020/03/27 Python
Python word文本自动化操作实现方法解析
2020/11/05 Python
CSS3动画animation实现云彩向左滚动
2014/05/09 HTML / CSS
关于运动会的稿件
2014/02/02 职场文书
某集团股份有限公司委托书样本
2014/09/24 职场文书
售房协议书范本2014
2014/10/23 职场文书
2015教师年度思想工作总结
2015/04/30 职场文书
银行资信证明
2015/06/17 职场文书
先进教师个人主要事迹材料
2015/11/03 职场文书
Python实战实现爬取天气数据并完成可视化分析详解
2022/06/16 Python