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 相关文章推荐
最小化数据传输――在客户端存储数据
Oct 09 PHP
PHP 上传文件的方法(类)
Jul 30 PHP
PHP imagecreatefrombmp 从BMP文件或URL新建一图像
Jul 16 PHP
利用php递归实现无限分类 格式化数组的详解
Jun 08 PHP
PHP删除HTMl标签的三种解决方法
Jun 30 PHP
PHP调用MsSQL Server 2012存储过程获取多结果集(包含output参数)的详解
Jul 03 PHP
php猴子选大王问题解决方法
May 12 PHP
ThinkPHP模板循环输出Volist标签用法实例详解
Mar 23 PHP
php set_include_path函数设置 include_path 配置选项
Oct 30 PHP
浅谈PHP中的错误处理和异常处理
Feb 04 PHP
php简单生成一组与多组随机字符串的方法
May 09 PHP
PHP文件类型检查及fileinfo模块安装使用详解
May 09 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:风雨欲来 路在何方?
2006/10/09 PHP
MySQL相关说明
2007/01/15 PHP
php表单敏感字符过滤类
2014/12/08 PHP
PHP获取毫秒级时间戳的方法
2015/04/15 PHP
WIN8.1下搭建PHP5.6环境
2015/04/29 PHP
Yii rules常用规则示例
2016/03/15 PHP
php版微信发红包接口用法示例
2016/09/23 PHP
JS类中定义原型方法的两种实现的区别
2007/03/08 Javascript
借用Google的Javascript API Loader来加速你的网站
2009/01/28 Javascript
textarea 控制输入字符字节数(示例代码)
2013/12/27 Javascript
javascript中的__defineGetter__和__defineSetter__介绍
2014/08/15 Javascript
Javascript中this关键字的一些小知识
2015/03/15 Javascript
js淡入淡出的图片轮播效果代码分享
2015/08/24 Javascript
jquery实现隐藏在左侧的弹性弹出菜单效果
2015/09/18 Javascript
AngularJS基础 ng-keyup 指令简单示例
2016/08/02 Javascript
自定义vue全局组件use使用、vuex的使用详解
2017/06/14 Javascript
JS设计模式之状态模式概念与用法分析
2018/02/05 Javascript
JavaScript中常见内置函数用法示例
2018/05/14 Javascript
Puppeteer环境搭建的详细步骤
2018/09/21 Javascript
JavaScript创建防篡改对象的方法分析
2018/12/30 Javascript
从vue源码看props的用法
2019/01/09 Javascript
详解微信小程序开发聊天室—实时聊天,支持图片预览
2019/05/20 Javascript
微信小程序 WXML节点信息查询详解
2019/07/29 Javascript
vue element upload组件 file-list的动态绑定实现
2019/10/11 Javascript
浅谈JavaScript中this的指向更改
2020/07/28 Javascript
Python正则表达式匹配中文用法示例
2017/01/17 Python
python基础教程项目二之画幅好画
2018/04/02 Python
python实现智能语音天气预报
2019/12/02 Python
美国指甲油品牌:Deco Miami
2017/01/30 全球购物
iKRIX意大利网上商店:男女豪华服装和配件
2019/10/09 全球购物
迎八一活动主题
2014/01/31 职场文书
根叔历年演讲稿
2014/05/20 职场文书
汉语言文学毕业求职信
2014/07/17 职场文书
党员作风建设自查报告
2014/10/23 职场文书
2015年学校关工委工作总结
2015/04/03 职场文书
监理中标通知书
2015/04/16 职场文书