PHP获取图片等比例高度

/**
 * 获取等比高宽
 * @param string $image_path 最大宽度
 * @param int $max_width 最大宽度
 * @param int $max_height 最大高度
 */
function getRatioSize($image_path, $max_width='', $max_height=''){
	list($width, $height) = getimagesize($image_path);
	$max_width = abs($max_width)<=0 ? 1 : abs($max_width);
	$max_height = abs($max_height)<=0 ? 1 : abs($max_height);
	if(!($max_width>$width || $max_height>$height)){
		$ratioh = $max_height/$height;
		$ratiow = $max_width/$width;
		$ratio = max($ratioh, $ratiow);
		// New dimensions
		$width = intval($ratio*$width);
		$height = intval($ratio*$height);
	}
	return array('width'=>$width, 'height'=>$height);
}


$image_path = "test2.jpg";

$size = getRatioSize($image_path, 300, 100);
echo '<img src="'.$image_path.'" height="'.$size['height'].'" width="'.$size['width'].'">';

你可能感兴趣的:(PHP,图片等比计算,宽高等比计算)