使用 PHP 调整图像大小,检测最长边,然后根据?

发布于 2024-11-05 17:50:53 字数 670 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

并安 2024-11-12 17:50:53

您可能会误会:

$width > 时$height 表示它是风景。将 maxwidth 设置为 800 意味着(高度/宽度)*800 = 新高度。另一方面 $height > $width 表示将 maxheight 设置为 800,因此 (width/height)*800 是新宽度。

现在您同时使用高度/宽度比,而不是相反。示例:

Image: 1600 (w) x 1200 (h)
Type: Landscape
New Width: 800
New Height: (1200 (h) / 1600(w) * 800 (nw) = 600

Image 1200 (w) x 1600 (h)
Type: Portrait
New Height: 800
New Width: (1200 (w) / 1600(h) * 800 (nh) = 600

希望您明白我的意思,您只是切换了它们:) 另请注意,您乘以 $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:

Image: 1600 (w) x 1200 (h)
Type: Landscape
New Width: 800
New Height: (1200 (h) / 1600(w) * 800 (nw) = 600

Image 1200 (w) x 1600 (h)
Type: Portrait
New Height: 800
New Width: (1200 (w) / 1600(h) * 800 (nh) = 600

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

北座城市 2024-11-12 17:50:53

您可以看一下我在 Image 类中使用的这个函数:

public function ResizeProportional($MaxWidth, $MaxHeight)
{

    $rate = $this->width / $this->height;

    if ( $this->width / $MaxWidth > $this->height / $MaxHeight )
        return $this->Resize($MaxWidth, $MaxWidth / $rate);
    else
        return $this->Resize($MaxHeight * $rate, $MaxHeight);
}

基本上,它首先根据宽度/高度计算图像的 $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:

public function ResizeProportional($MaxWidth, $MaxHeight)
{

    $rate = $this->width / $this->height;

    if ( $this->width / $MaxWidth > $this->height / $MaxHeight )
        return $this->Resize($MaxWidth, $MaxWidth / $rate);
    else
        return $this->Resize($MaxHeight * $rate, $MaxHeight);
}

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.

隱形的亼 2024-11-12 17:50:53

您应该在第二部分中切换高度和宽度,请注意 ($width/$height) 部分:

} else {


    $newheight=800;
    $newwidth=($width/$height)*$newheight;


    $newheight1=150;
    $newwidth1=($width/$height)*$newheight;

}

You should switch height and width in the second part, note the ($width/$height) part:

} else {


    $newheight=800;
    $newwidth=($width/$height)*$newheight;


    $newheight1=150;
    $newwidth1=($width/$height)*$newheight;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文