php图片等比例放大与缩小的方法举例
时间:2021-07-01 10:21:17
帮助过:10人阅读
/** - * 图片等比例放大与缩小
- * @resizeimage
- * @param $srcfile 源文件尺寸
- * @param $mySize 要转换的尺寸
- * edit bbs.it-home.org
- * at 2013/6/7
- */
- function resizeimage($srcfile,$mySize){
- $size=getimagesize($srcfile);
- switch($size[2]){
- case 1:
- $img=imagecreatefromgif($srcfile);
- break;
- case 2:
- $img=imagecreatefromjpeg($srcfile);
- break;
- case 3:
- $img=imagecreatefrompng($srcfile);
- break;
- }
- //源图片的宽度和高度
- $oldImg['w']=imagesx($img);
- $oldImg['h']=imagesy($img);
- if ($oldImg['w']<=$mySize['w'] && $oldImg['h']<156){
- $rate=1;
- }elseif ($oldImg['w']>$mySize['w'] && $oldImg['h']<$mySize['h']){
- $rate=$mySize['w']/$oldImg['w'];
- }elseif ($oldImg['w']<$mySize['w'] && $oldImg['h']>$mySize['h']){
- $rate=$mySize['h']/$oldImg['h'];
- }elseif ($oldImg['w']>$mySize['w'] && $oldImg['h']>$mySize['h']){
- $rate1=$mySize['w']/$oldImg['w'];
- $rate2=$mySize['h']/$oldImg['h'];
- if ($rate1>$rate2){$rate=$rate2;}else{$rate=$rate1;}
- }
- $newImg['w']=$oldImg['w']*$rate;
- $newImg['h']=$oldImg['h']*$rate;
- return "width=".$newImg['w']." height=".$newImg['h'];
- }
//调用示例: - $mySize=array('w'=>143,'h'=>156);
- $imgSize=resizeimage("22.jpg",$mySize);
- echo "";
- ?>
|