名站网址导航为大家免费提供关于网站编程方面的知识。
使用XDebug调试及单元测试覆盖率分析-PhPphp技巧
- 本篇文章的具体介绍如下请大家仔细看
特别注意: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);
}
?>
关于网站编程方面的知识就说道这里了,希望能够对大家有作用。,