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实现的功能是显示8条基色色带
Oct 09 PHP
php 设计模式之 单例模式
Dec 19 PHP
PHP导出EXCEL快速开发指南--PHPEXCEL的使用详解
Jun 03 PHP
PHP 只允许指定IP访问(允许*号通配符过滤IP)
Jul 08 PHP
thinkphp数据查询和遍历数组实例
Nov 28 PHP
php随机取mysql记录方法小结
Dec 27 PHP
PHP图像处理类库及演示分享
May 17 PHP
PHP生成条形码大揭秘
Sep 24 PHP
php连接mysql数据库
Mar 21 PHP
PHP操作MongoDB实现增删改查功能【附php7操作MongoDB方法】
Apr 24 PHP
PHP单元测试配置与使用方法详解
Dec 27 PHP
Docker 安装 PHP并与Nginx的部署实例讲解
Feb 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仿盗链代码
2012/06/03 PHP
学习php设计模式 php实现合成模式(composite)
2015/12/08 PHP
php is_executable判断给定文件名是否可执行实例
2016/09/26 PHP
ThinkPHP框架分布式数据库连接方法详解
2017/03/14 PHP
老生常谈PHP面向对象之注册表模式
2017/05/26 PHP
JS特权方法定义作用以及与公有方法的区别
2013/03/18 Javascript
JS读取XML文件示例代码
2013/11/15 Javascript
dul无法加载bootstrap实现unload table/user恢复
2016/09/29 Javascript
AngularJS压缩JS技巧分析
2016/11/08 Javascript
微信小程序教程之本地图片上传(leancloud)实例详解
2016/11/16 Javascript
原生js实现无限循环轮播图效果
2017/01/20 Javascript
微信小程序 引用其他js文件实现代码
2017/02/22 Javascript
Vue2几种常见开局方式详解
2017/09/09 Javascript
使用express搭建一个简单的查询服务器的方法
2018/02/09 Javascript
angular4笔记系列之内置指令小结
2018/11/09 Javascript
微信域名检测接口调用演示步骤(含PHP、Python)
2019/12/08 Javascript
JavaScript函数Call、Apply原理实例解析
2020/02/17 Javascript
[44:39]2014 DOTA2国际邀请赛中国区预选赛 NE VS CNB
2014/05/21 DOTA
MAC中PyCharm设置python3解释器
2017/12/15 Python
python 多维切片之冒号和三个点的用法介绍
2018/04/19 Python
浅谈python中requests模块导入的问题
2018/05/18 Python
python用户评论标签匹配的解决方法
2018/05/31 Python
Python使用__new__()方法为对象分配内存及返回对象的引用示例
2019/09/20 Python
浅谈PyTorch的可重复性问题(如何使实验结果可复现)
2020/02/20 Python
pyspark给dataframe增加新的一列的实现示例
2020/04/24 Python
Python 数据分析之逐块读取文本的实现
2020/12/14 Python
Tirendo比利时:在线购买轮胎
2018/10/22 全球购物
初中科学教学反思
2014/01/21 职场文书
超市端午节活动方案
2014/01/23 职场文书
软件工程毕业生自荐信
2014/07/04 职场文书
2014年教研组工作总结
2014/11/26 职场文书
维稳工作承诺书
2015/01/20 职场文书
南京导游词
2015/02/03 职场文书
教育读书笔记
2015/07/02 职场文书
React 高阶组件HOC用法归纳
2021/06/13 Javascript
Flask使用SQLAlchemy实现持久化数据
2021/07/16 Python