Resize image using PHP, determine the longest side and resize according to?

I am working on uploading a script image with PHP, I found that someone was suggesting and trying to modify it, however I ran into several problems.

I want to do the following: Detect the longest side of the image (for example, portrait or landscape) And then resize the image, with the longest side being 800 pixels and keep the aspect ratio.

Here is the code that I still have. For landscape images, it works great, but with portrait images it distorts them like crazy. PS. I am making an enlarged 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);
+3
source share
3 answers

You are probably mistaken:

$width > $height , . 800 (/) * 800 = . , $height > $width 800 , , (/) * 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

+3

, 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 ), - .

$this->width / $MaxWidth - . , $this->width / $MaxWidth , $this->height / $MaxHeight, ​​ maxwidth, . - , maxheight .

0

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

} else {


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


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

}
0
source

All Articles