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.ini中的php-5.2.0配置指令详解
Mar 27 PHP
phpmyadmin 访问被拒绝的真实原因
Jun 15 PHP
php xml 入门学习资料
Jan 01 PHP
PHP 通过Socket收发十六进制数据的实现代码
Aug 16 PHP
PHP json_encode中文乱码问题的解决办法
Sep 09 PHP
ThinkPHP之M方法实例详解
Jun 20 PHP
几个实用的PHP内置函数使用指南
Nov 27 PHP
使用GDB调试PHP代码,解决PHP代码死循环问题
Mar 02 PHP
php无序树实现方法
Jul 28 PHP
thinkPHP订单数字提醒功能的实现方法
Dec 01 PHP
PHP实现表单提交数据的验证处理功能【防SQL注入和XSS攻击等】
Jul 21 PHP
PHP如何开启Opcache功能提升程序处理效率
Apr 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 UTF8 文件的签名问题
2009/10/30 PHP
使用PHP生成图片的缩略图的方法
2015/08/18 PHP
php下的原生ajax请求用法实例分析
2020/02/28 PHP
Get中文乱码IE浏览器Get中文乱码解决方案
2013/12/26 Javascript
jQuery1.9.1针对checkbox的调整方法(prop)
2014/05/01 Javascript
对 jQuery 中 data 方法的误解分析
2014/06/18 Javascript
JavaScript实现从数组中选出和等于固定值的n个数
2014/09/03 Javascript
JS实现浏览器状态栏文字闪烁效果的方法
2015/10/27 Javascript
JavaScript引用类型和基本类型详解
2016/01/06 Javascript
浅谈Cookie的生命周期问题
2016/08/02 Javascript
javascript学习之json入门
2016/12/22 Javascript
JQuery和HTML5 Canvas实现弹幕效果
2017/01/04 Javascript
jQuery实现鼠标点击处心形漂浮的炫酷效果示例
2018/04/12 jQuery
详解JavaScript的BUG和错误
2018/05/07 Javascript
vue引入js数字小键盘的实现代码
2018/05/14 Javascript
JS实现HTML页面中动态显示当前时间完整示例
2018/07/30 Javascript
通过JS运行机制的角度说说作用域
2019/03/12 Javascript
原生js实现简单轮播图
2020/10/26 Javascript
Nuxt.js nuxt-link与router-link的区别说明
2020/11/06 Javascript
python查找第k小元素代码分享
2013/12/18 Python
Python对小数进行除法运算的正确方法示例
2014/08/25 Python
Python 常用string函数详解
2016/05/30 Python
浅析python中的分片与截断序列
2016/08/09 Python
python使用邻接矩阵构造图代码示例
2017/11/10 Python
几种实用的pythonic语法实例代码
2018/02/24 Python
Pyqt QImage 与 np array 转换方法
2019/06/27 Python
Pycharm 2020.1 版配置优化的详细教程
2020/08/07 Python
口腔医学技术应届生求职信
2013/11/09 职场文书
财务人员个人求职信范文
2013/12/04 职场文书
清扬洗发水广告词
2014/03/14 职场文书
南京市纪委监察局整改方案
2014/09/16 职场文书
2015新员工工作总结范文
2015/10/15 职场文书
2016年心理学教育培训学习心得体会
2016/01/12 职场文书
《搭石》教学反思
2016/02/18 职场文书
python实现进度条的多种实现
2021/04/29 Python
mysq启动失败问题及场景分析
2021/07/15 MySQL