图片缩放(不裁剪,按原来比例计算宽高)


//比例计算图片宽高
public function calculateDimensions($width, $height, $maxWidth, $maxHeight)
{
  if ($width != $height) {
  if ($width > $height) {
  $tWidth = $maxWidth;
  $tHeight = (($tWidth * $height)/$width);
  //fix height
  if ($tHeight > $maxHeight) {
  $tHeight = $maxHeight;
  $tWidth = (($width * $tHeight)/$height);
  }
 } else {
  $tHeight = $maxHeight;
  $tWidth = (($width * $tHeight)/$height);
  //fix width
  if ($tWidth > $maxWidth) {
  $tWidth = $maxWidth;
  $tHeight = (($tWidth * $height)/$width);
  }
 } } else {
  $tWidth = $tHeight = min($maxHeight, $maxWidth);
  }
  return array('height'=>(int)$tHeight,'width'=>(int)$tWidth);
}

你可能感兴趣的:(图片缩放(不裁剪,按原来比例计算宽高))