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面向对象编程快速入门
Oct 09 PHP
Ha0k 0.3 PHP 网页木马修改版
Oct 11 PHP
PHP统计目录下的文件总数及代码行数(去除注释及空行)
Jan 17 PHP
分享一个PHP数据流应用的简单例子
Jun 01 PHP
PHP输出缓存ob系列函数详解
Mar 11 PHP
ThinkPHP使用心得分享-分页类Page的用法
May 15 PHP
Codeigniter整合Tank Auth权限类库详解
Jun 12 PHP
php使用pear_smtp发送邮件
Apr 15 PHP
学习PHP session的传递方式
Jun 15 PHP
Laravel框架中Blade模板的用法示例
Aug 30 PHP
Laravel Validator 实现两个或多个字段联合索引唯一
May 08 PHP
Laravel框架中队列和工作(Queues、Jobs)操作实例详解
Apr 06 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
分享微信扫码支付开发遇到问题及解决方案-附Ecshop微信支付插件
2015/08/23 PHP
ThinkPHP和UCenter接口冲突的解决方法
2016/07/25 PHP
Windows 下安装 swoole 图文教程(php)
2017/06/05 PHP
PHP PDOStatement::nextRowset讲解
2019/02/01 PHP
一些Javascript的IE和Firefox(火狐)兼容性的问题总结及常用例子
2009/05/21 Javascript
js 代码集(学习js的朋友可以看下)
2009/07/22 Javascript
用js做一个小游戏平台 (一)
2009/12/29 Javascript
JS 密码强度验证(兼容IE,火狐,谷歌)
2010/03/15 Javascript
web开发人员学习jQuery的6大理由及jQuery的优势介绍
2013/01/03 Javascript
JavaScript对象创建及继承原理实例解剖
2013/02/28 Javascript
JQuery实现展开关闭层的方法
2015/02/17 Javascript
jQuery实现菜单式图片滑动切换
2015/03/14 Javascript
JavaScript判断用户是否对表单进行了修改的方法
2015/03/18 Javascript
JavaScript+html5 canvas实现本地截图教程
2020/04/16 Javascript
扩展bootstrap的modal模态框-动态添加modal框-弹出多个modal框
2017/02/21 Javascript
深入理解 JavaScript 中的 JSON
2017/04/06 Javascript
webpack3+React 的配置全解
2017/08/21 Javascript
微信小程序如何刷新当前界面的实现方法
2019/06/07 Javascript
nodejs环境使用Typeorm连接查询Oracle数据
2019/12/05 NodeJs
jQuery实现的图片点击放大缩小功能案例
2020/01/02 jQuery
Vue中实现回车键切换焦点的方法
2020/02/19 Javascript
微信小程序用户盒子、宫格列表的实现
2020/07/01 Javascript
Python 和 JS 有哪些相同之处
2017/11/23 Python
python3 遍历删除特定后缀名文件的方法
2018/04/23 Python
python3 enum模块的应用实例详解
2019/08/12 Python
pytorch 实现将自己的图片数据处理成可以训练的图片类型
2020/01/08 Python
python神经网络编程实现手写数字识别
2020/05/27 Python
Tensorflow之MNIST CNN实现并保存、加载模型
2020/06/17 Python
国税会议欢迎词
2014/01/16 职场文书
采购部经理岗位职责
2014/02/10 职场文书
社区戒毒工作方案
2014/06/04 职场文书
反邪教标语
2014/06/23 职场文书
2015廉洁自律个人总结
2015/02/14 职场文书
小学思想品德教学反思
2016/02/24 职场文书
深入理解Vue的数据响应式
2021/05/15 Vue.js
Redis 操作多个数据库的配置的方法实现
2022/03/23 Redis