【发布时间】: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