php批量缩放图片的代码[ini参数控制]
程序员文章站
2022-06-06 15:16:45
首先使用一个ini文件来设置要缩放的大小,其中为宽或高0的则为图片放大或缩小,都为0则还是原大小,都不为0都拉抻成指定的大小。 注意:ini文件使用php解释时为注释文件,...
首先使用一个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);
}
?>
上一篇: python 两个小练习理解递归函数
下一篇: wifi共享精灵无法启动的原因和解决方法