php批量缩放图片的代码[ini参数控制]
作者:bea
首先使用一个ini文件来设置要缩放的大小,其中为宽或高0的则为图片放大或缩小,都为0则还是原大小,都不为0都拉抻成指定的大小。 注意:ini文件使用php解释时为注释文件,什么也没有输出,这是为了安全起见而故意为之。而;则是ini文件的注释。 我设置的ini文件例子如下: 代码如下: <?php /* ;Translate the image format using the original image size [Translation] width=
首先使用一个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);
}
?>
有用 | 无用
注意: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下安装配置fckeditor编辑器的方法
- PHP如何抛出异常处理错误
- php中实现记住密码自动登录的代码
- php Smarty 字符比较代码
- php下批量挂马和批量清马代码
- php SQL Injection with MySQL
- PHP的SQL注入实现(测试代码安全不错)
- php通用防注入程序 推荐
- 8个出色的WordPress SEO插件收集
- zend framework多模块多布局配置
- 让你成为更出色的PHP开发者的10个技巧
- 理解php Hash函数,增强密码安全
- php中根据某年第几天计算出日期年月日的代码
- PHP中防止SQL注入实现代码
- 简单的移动设备检测PHP脚本代码
- PHP下通过file_get_contents的代理使用方法
- php文件上传表单摘自drupal的代码
- php 连接mysql连接被重置的解决方法
- php中获取关键词及所属来源搜索引擎名称的代码