使用 PHP 调整图像大小,检测最长边,然后根据?
我正在使用 PHP 编写一个图像上传脚本,我发现有人提供了一个并尝试对其进行修改,但是,我遇到了一些问题。
我想做以下事情: 检测图像的最长边(即纵向或横向) 然后调整图像大小,最长边为 800px 并保持比例。
这是我到目前为止的代码。对于横向图像,它工作得很好,但对于纵向图像,它会疯狂地扭曲它们。 附言。我正在制作更大的图像和缩略图。
list($width,$height)=getimagesize($uploadedfile);
if($width > $height){
$newwidth=800;
$newheight=($height/$width)*$newwidth;
$newwidth1=150;
$newheight1=($height/$width)*$newwidth1;
} else {
$newheight=800;
$newwidth=($height/$width)*$newheight;
$newheight1=150;
$newwidth1=($height/$width)*$newheight;
}
$tmp=imagecreatetruecolor($newwidth,$newheight);
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
I'm working on an image upload script with PHP, I found one someone was offering and tried modifying it, however, i'm running into a few problems.
I want to do the following:
Detect the longest side of the image (ie. portrait or landscape)
And then resize the image, with the longest side being 800px AND keep proportions.
Here is the code I have so far.. For landscape images it works fine, but with portrait ones it distorts them like crazy.
PS. I'm making a larger image as well as a thumbnail.
list($width,$height)=getimagesize($uploadedfile);
if($width > $height){
$newwidth=800;
$newheight=($height/$width)*$newwidth;
$newwidth1=150;
$newheight1=($height/$width)*$newwidth1;
} else {
$newheight=800;
$newwidth=($height/$width)*$newheight;
$newheight1=150;
$newwidth1=($height/$width)*$newheight;
}
$tmp=imagecreatetruecolor($newwidth,$newheight);
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能会误会:
当
$width > 时$height
表示它是风景。将 maxwidth 设置为 800 意味着(高度/宽度)*800 = 新高度。另一方面$height > $width
表示将 maxheight 设置为 800,因此 (width/height)*800 是新宽度。现在您同时使用高度/宽度比,而不是相反。示例:
希望您明白我的意思,您只是切换了它们:) 另请注意,您乘以 $newheight 而不是 $newheight1 作为肖像缩略图
You're probably mistaking:
When
$width > $height
that means it's landscape. Setting maxwidth to 800 means (height/width)*800 = new height. On the other hand$height > $width
means setting maxheight to 800 and thus having (width/height)*800 is new width.Right now your using both the height/width ratio instead of the other way around. Example:
Hope you get what I'm saying, you just switched them :) Also notice that you multiply with $newheight instead of $newheight1 for the portrait thumbnail
您可以看一下我在 Image 类中使用的这个函数:
基本上,它首先根据宽度/高度计算图像的 $rate 比例。然后,它检查调整大小时宽度是否会超出范围
( $this->width / $MaxWidth > $this->height / $MaxHeight )
,如果是 - 设置width 到所需的最大宽度并相应地计算高度。$this->width / $MaxWidth
是图像宽度相对于最大宽度的百分比。因此,如果$this->width / $MaxWidth
大于$this->height / $MaxHeight
,则宽度应设置为 maxwidth,并应计算高度基于它。如果比较是相反的,只需将高度设置为 maxheight 并计算新的宽度。You can take a look in this function I use in my Image class:
Basically it first calculates the image's proportions in $rate based on width/height. Then it checks if the width is going to get out of bounds when resized
( $this->width / $MaxWidth > $this->height / $MaxHeight )
and if it is - sets width to the desired maximum width and calculates the height accordingly.$this->width / $MaxWidth
is the percentage of the image's width based on the maximum one. So if$this->width / $MaxWidth
is larger than$this->height / $MaxHeight
the width should be set to maxwidth and the height should be calculated based on it. If the comparison is the other way around just set height to maxheight and calculate the new width.您应该在第二部分中切换高度和宽度,请注意
($width/$height)
部分:You should switch height and width in the second part, note the
($width/$height)
part: