开发者

Resize image with PHP, detect longest side, and resize according?

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开发者_如何学Go: 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);


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


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.


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;

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜