圖片縮放(不裁剪,按原來比例計算寬高)

King_JW發表於2020-07-23
//比例計算圖片寬高
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);
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結

周嘉偉

相關文章