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 相关文章推荐
Discuz! 5.0.0论坛程序中加入一段js代码,让会员点击下载附件前自动弹出提示窗口
Apr 18 PHP
PHP禁止页面缓存的代码
Oct 23 PHP
PHP setTime 设置当前时间的代码
Aug 27 PHP
php中使用临时表查询数据的一个例子
Feb 03 PHP
四种php中webservice实现的简单架构方法及实例
Feb 03 PHP
PHP文件系统管理(实例讲解)
Sep 19 PHP
Yii2.0使用阿里云OSS的SDK上传图片、下载、删除图片示例
Sep 20 PHP
PHP实现的链式队列结构示例
Sep 15 PHP
PHP环形链表实现方法示例
Sep 15 PHP
phpinfo无法显示的原因及解决办法
Feb 15 PHP
PHP小程序支付功能完整版【基于thinkPHP】
Mar 26 PHP
phpQuery解析HTML乱码问题(补充官网未列出的乱码解决方案)
Apr 01 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
CodeIgniter php mvc框架 中国网站
2008/05/26 PHP
PHP开源开发框架ZendFramework使用中常见问题说明及解决方案
2014/06/12 PHP
php插入排序法实现数组排序实例
2015/02/16 PHP
php实现json编码的方法
2015/07/30 PHP
PHP简单读取PDF页数的实现方法
2016/07/21 PHP
Laravel5.5新特性之友好报错以及展示详解
2017/08/13 PHP
小议Function.apply() 之一------(函数的劫持与对象的复制)
2006/11/30 Javascript
网上应用的一个不错common.js脚本
2007/08/08 Javascript
Javascript的一种模块模式
2008/03/22 Javascript
初窥JQuery(二)事件机制(2)
2010/12/06 Javascript
javascipt匹配单行和多行注释的正则表达式
2013/11/20 Javascript
jquery实现倒计时效果
2015/12/14 Javascript
Jqprint实现页面打印
2017/01/06 Javascript
微信小程序动态显示项目倒计时效果
2017/06/13 Javascript
[js高手之路]HTML标签解释成DOM节点的实现方法
2017/08/31 Javascript
JavaScript实现左侧菜单效果
2017/12/14 Javascript
node+koa2+mysql+bootstrap搭建一个前端论坛
2018/05/06 Javascript
深入剖析Node.js cluster模块
2018/05/23 Javascript
js实现导航跟随效果
2018/11/17 Javascript
2019 年编写现代 JavaScript 代码的5个小技巧(小结)
2019/01/15 Javascript
JQuery实现折叠式菜单的详细代码
2020/06/03 jQuery
[50:05]VGJ.S vs OG 2018国际邀请赛淘汰赛BO3 第二场 8.22
2018/08/23 DOTA
[57:31]DOTA2-DPC中国联赛 正赛 SAG vs CDEC BO3 第一场 2月1日
2021/03/11 DOTA
Python中实现从目录中过滤出指定文件类型的文件
2015/02/02 Python
matplotlib绘制动画代码示例
2018/01/02 Python
python中map的基本用法示例
2018/09/10 Python
django 快速启动数据库客户端程序的方法示例
2019/08/16 Python
Python爬虫实现使用beautifulSoup4爬取名言网功能案例
2019/09/15 Python
详解Python中打乱列表顺序random.shuffle()的使用方法
2019/11/11 Python
Spring @Enable模块驱动原理及使用实例
2020/06/23 Python
佐卡伊官网:中国知名珠宝品牌
2017/02/05 全球购物
FC-Moto美国:欧洲最大的摩托车服装和头盔商店之一
2019/08/24 全球购物
保密工作实施方案
2014/02/24 职场文书
领导调研接待方案
2014/02/27 职场文书
超市商业计划书
2014/05/04 职场文书
2014小学语文教师个人工作总结
2014/12/03 职场文书