在 php 中调整图像大小需要帮助
我有一些代码可以从上传的图像生成缩略图。唯一的问题是它会剪掉肖像图像,而不是调整肖像图像的大小以适应缩略图的高度,风景图像就很好。我想知道是否有人可以帮助我更改代码,以便它将肖像图像正确放置在缩略图内? (如果这是有道理的?)
这是代码:
$setSize = 150;
$setHSize = 113;
$jpeg_quality = 75;
list($width, $height, $type, $attr) = getimagesize($origPath);
$newW = $setSize;
$newH = round( $height * ( $setSize / $width ) );
$img_r = imagecreatefromjpeg($origPath) or notfound();
$dst_r = ImageCreateTrueColor( $setSize, $setHSize );
$heightOffset = round( ($setHSize-$newH)/2 );
$white = imagecolorallocate($dst_r, 255, 255, 255);
imagefilledrectangle($dst_r, 0, 0, $setSize, $setHSize, $white);
imagecopyresampled($dst_r, $img_r, 0, $heightOffset, 0, 0, $newW, $newH, $width, $height);
header("Content-type: image/jpeg");
imagejpeg($dst_r, $thbPath, $jpeg_quality);
我只是不完全理解 php 创建图像的方式,所以任何帮助将不胜感激:)
I have a bit of code that generates thumbnails from an uploaded image. The only problem is that it cuts portrait images off, rather than resizing the portrait image to fit the height of the thumbnail, landscape images are fine. I was wondering if anyone could help me out to change the code so it will place portrait images inside the thumbnail properly? (if that makes sense?)
Here's the code:
$setSize = 150;
$setHSize = 113;
$jpeg_quality = 75;
list($width, $height, $type, $attr) = getimagesize($origPath);
$newW = $setSize;
$newH = round( $height * ( $setSize / $width ) );
$img_r = imagecreatefromjpeg($origPath) or notfound();
$dst_r = ImageCreateTrueColor( $setSize, $setHSize );
$heightOffset = round( ($setHSize-$newH)/2 );
$white = imagecolorallocate($dst_r, 255, 255, 255);
imagefilledrectangle($dst_r, 0, 0, $setSize, $setHSize, $white);
imagecopyresampled($dst_r, $img_r, 0, $heightOffset, 0, 0, $newW, $newH, $width, $height);
header("Content-type: image/jpeg");
imagejpeg($dst_r, $thbPath, $jpeg_quality);
I just don't fully understand the way php creates images, so any help would be appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您计算 $newW 和 $newH 的方式不符合您的要求。您优先考虑宽度,因此大多数横向图像看起来都不错,但纵向图像会被剪掉。请尝试以下操作:
我希望这会有所帮助!
The way you compute $newW and $newH is incorrect for what you want. You are giving preference to the width, so most landscape images will look okay, but portrait will be cut off. Try the following instead:
I hope this helps!
希望这有帮助。
//*************< /em>*********|根据高度调整大小
//************< em>**********|从帖子中获取文件
//*********** ***********|检查权限
//************ **********|确保您有一个文件
//*********** ***********|创建新名称
//**********************|查看我们拥有的文件类型
//********** ************|获取高度和宽度
Hope this helps.
//**********************| Resize based on height
//**********************| Get the file from the post
//**********************| Check permission
//**********************| Make sure you have a file
//**********************| Make new name
//**********************| See the kind of file we have
//**********************| Get the height and width
或
可以帮助你
or
in Imagick can help you