First use an ini file to set the size to be scaled. If the width or height is 0, the image will be enlarged or reduced. If both are 0, it will remain the original size. If it is not 0, it will be stretched to the specified size.
Note: When the ini file is interpreted using PHP, it is a comment file and nothing is output. This is done intentionally for security reasons. And ; is a comment in the ini file.
The example of the ini file I set is as follows:
Copy the code The code is as follows:
< ;?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
*/
?>
The following is the php code written to zoom the image, where the variable classes is an array, and you can select any number of settings specified in the ini file:
Copy code The code is as follows:
$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) ;
}
?>
http://www.bkjia.com/PHPjc/322940.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322940.htmlTechArticleFirst use an ini file to set the size to be scaled, where 0 for width or height means the image is enlarged or Reduce the size. If both are 0, it will still be the original size. If both are 0, it will be stretched to the specified size. Note...