【问题标题】:Finding height of an image from given width keeping proportionality in PHP在PHP中从给定宽度保持比例查找图像的高度
【发布时间】:2014-12-18 21:20:44
【问题描述】:

我正在尝试将图像大小调整为 500 像素的宽度。我想保持图像的纵横比。

我的原图是

当我将它调整为宽度为 500 的大小时,它就像

我正在执行以下代码来调整图像大小。

$target_dir="uploads/reference/".$data['CP_Image'];
move_uploaded_file($tmp_file, $target_dir);
list($w, $h) = getimagesize($target_dir);
$width=500;
$ratio = $width / $h;
$x = ($w - $width / $ratio) / 2;
$height = 500 / $ratio;exit;

$path = "uploads/reference/".$data['CP_Image'];
$thumb = imagecreatetruecolor($width, $height);
$source = imagecreatefromjpeg($target_dir);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $width, $height, $w, $h);

// Output and free memory
imagejpeg($thumb,$path);
imagedestroy($thumb);

使用此代码,宽度更改为 500 像素,但高度不成比例。我做错了什么?谁能帮我解决它?

提前致谢

【问题讨论】:

    标签: php image resize aspect-ratio


    【解决方案1】:

    您计算的比率不正确。您不需要将新宽度除以原始高度,而是需要这样的东西:

    list($w, $h) = getimagesize($target_dir);    
    $width = 500;
    $ratio = $w / $h;
    $height = $width / $ratio;
    

    【讨论】:

      猜你喜欢
      • 2013-04-07
      • 2022-01-11
      • 2019-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多