function imageresize2($width, $height, $targetw, $targeth) { $percentage = 1; if (($width > $targetw) || ($height > $targeth)) { $width_diff = $width - $targetw; $height_diff = $height - $targeth; if ($width_diff >= $height_diff) { $percentage = ($targetw / $width); } else { $percentage = ($targeth / $height); } } //gets the new value and applies the percentage, then rounds the value $width = round($width * $percentage); $height = round($height * $percentage); $resize[0] = $width; $resize[1] = $height; return $resize; } //方法二 if (!$max_width) $max_width = 240; if (!$max_height) $max_height = 200; $size = getimagesize($image); $width = $size[0]; $height = $size[1]; $x_ratio = $max_width / $width; $y_ratio = $max_height / $height; if ( ($width <= $max_width) && ($height <= $max_height) ) { $tn_width = $width; $tn_height = $height; } else if (($x_ratio * $height) < $max_height) { $tn_height = ceil($x_ratio * $height); $tn_width = $max_width; } else { $tn_width = ceil($y_ratio * $width); $tn_height = $max_height; } $src = imagecreatefrompng($image); $dst = imagecreate($tn_width,$tn_height); imagecopyresized($dst, $src, 0, 0, 0, 0, $tn_width,$tn_height,$width,$height); header("content-type: image/png"); imagepng($dst, null, -1); imagedestroy($src); imagedestroy($dst); //方法三 /* 函數原型如下: 參數說明: $oldwidth:原圖片寬度 $oldheight:原圖片高度 $imgwidth:縮小或放大的圖片寬度 $imgheight:縮小或放大的圖片高度 返回:wwww.111cn.net 數組:arraysize ,其中索引為:width 和height 即:arraysize['width']、arraysize['height'] */ function getimgsize($oldwidth,$oldheight,$imgwidth,$imgheight) { //$oldwidth設定的寬度,$oldheight設定的高度,$imgwidth圖片的寬度,$imgheight圖片的高度; //儲存格裝得能裝得進圖片,則按圖片的真實大小顯示; if($imgwidth<=$oldwidth&&$imgheight<=$oldheight) { $arraysize=array('width'=>$imgwidth,'height'=>$imgheight); return $arraysize; } else { $suoxiaowidth=$imgwidth-$oldwidth; $suoxiaoheight=$imgheight-$oldheight; $suoxiaoheightper=$suoxiaoheight/$imgheight; $suoxiaowidthper=$suoxiaowidth/$imgwidth; if($suoxiaoheightper>=$suoxiaowidthper) { //儲存格高度為準; $aftersuoxiaowidth=$imgwidth*(1-$suoxiaoheightper); $arraysize=array('width'=>$aftersuoxiaowidth,'height'=>$oldheight); return $arraysize; } else { //儲存格寬度為準; $aftersuoxiaoheight=$imgheight*(1-$suoxiaowidthper); $arraysize=array('width'=>$oldwidth,'height'=>$aftersuoxiaoheight); return $arraysize; } } } |