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网站安装程序制作的原理、步骤、注意事项和示例代码
Aug 01 PHP
PHP 字符串长度判断效率更高的方法
Mar 02 PHP
php实现redis数据库指定库号迁移的方法
Jan 14 PHP
PHP函数实现从一个文本字符串中提取关键字的方法
Jul 01 PHP
PHP实现无限级分类(不使用递归)
Oct 22 PHP
Yii2实现ajax上传图片插件用法
Apr 28 PHP
PHP会员找回密码功能的简单实现
Sep 05 PHP
php json中文编码为null的解决办法
Dec 14 PHP
PHP编程实现计算抽奖概率算法完整实例
Aug 09 PHP
PHP观察者模式示例【Laravel框架中有用到】
Jun 15 PHP
PHP swoole和redis异步任务实现方法分析
Aug 12 PHP
Laravel框架表单验证操作实例分析
Sep 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
日本十大惊悚动漫
2020/03/04 日漫
PHP判断手机是IOS还是Android
2015/12/09 PHP
PHP手机短信验证码实现流程详解
2018/05/17 PHP
跨域请求之jQuery的ajax jsonp的使用解惑
2011/10/09 Javascript
在页面加载完成后通过jquery给多个span赋值
2014/05/21 Javascript
使用jQuery实现星级评分代码分享
2014/12/09 Javascript
jQuery结合CSS制作漂亮的select下拉菜单
2015/05/03 Javascript
JS实现从顶部下拉显示的带动画QQ客服特效代码
2015/10/24 Javascript
IE8 内存泄露(内存一直增长 )的原因及解决办法
2016/04/06 Javascript
解决jquery无法找到其他父级子集问题的方法
2016/05/10 Javascript
JS实现搜索框文字可删除功能
2016/12/28 Javascript
详谈jQuery unbind 删除绑定事件 / 移除标签方法
2017/03/02 Javascript
Vue input控件通过value绑定动态属性及修饰符的方法
2017/05/03 Javascript
React数据传递之组件内部通信的方法
2017/12/31 Javascript
vue 监听某个div垂直滚动条下拉到底部的方法
2018/09/15 Javascript
vue.js 打包时出现空白页和路径错误问题及解决方法
2019/06/26 Javascript
CountUp.js实现数字滚动增值效果
2019/10/17 Javascript
使用vue实现一个电子签名组件的示例代码
2020/01/06 Javascript
vue实现购物车结算功能
2020/06/18 Javascript
Vue-resource安装过程及使用方法解析
2020/07/21 Javascript
如何使用three.js 制作一个三维的推箱子游戏
2020/07/29 Javascript
[01:06]DOTA2小知识课堂 Ep.01 TP出门不要忘记帮队友灌瓶哦
2019/12/05 DOTA
python引入导入自定义模块和外部文件的实例
2017/07/24 Python
基于Python的文件类型和字符串详解
2017/12/21 Python
把csv文件转化为数组及数组的切片方法
2018/07/04 Python
Python学习工具jupyter notebook安装及用法解析
2020/10/23 Python
收藏!10个免费高清视频素材网站!【设计、视频剪辑必备】
2021/03/18 杂记
html5开发三八女王节表白神器
2018/03/07 HTML / CSS
材料成型专业个人求职信范文
2013/09/25 职场文书
工业设计专业推荐信
2013/10/29 职场文书
师范生个人推荐信
2013/11/29 职场文书
缓刑人员的思想汇报
2014/01/11 职场文书
生物学学生自我评价
2014/01/17 职场文书
文科生自我鉴定
2014/02/15 职场文书
证劵公司反洗钱宣传活动总结
2015/05/08 职场文书
Redis集群新增、删除节点以及动态增加内存的方法
2021/09/04 Redis