<?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代碼,其中變量classes是一個數組,可以選擇任意多個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
*/ ?>
注意:ini文件使用php解釋時為注釋文件,什麼也沒有輸出,這是為了安全起見而故意為之。而;則是ini文件的注釋。