Dênis Montone
Dênis Montone

Reputation: 621

Redefining image height in PHP

I have dynamic loaded images wich cannot be larger than 115px. I get them from an external source so I have no control over it.

I could get the image sizes with:

$list = getimagesize($imagePath);
$width = $list[0];
$height = $list[1];

But now I need to resize if they are larger than 115px. Tried this but it didn't keep the proportion:

$height = round(($height * $width) / $width);

Can someone help me with this? Thanks in advance.

Upvotes: 0

Views: 92

Answers (5)

Gavin
Gavin

Reputation: 2183

Depending on your minimum browser requirement, and that this is indeed intended for HTML output you could handle this in html, and save yourself some processing.

<img alt='image alt' src='imagesrc' style='max-width:115px; max-height:115px; height:auto; width:auto;' />

Upvotes: 0

svlzx
svlzx

Reputation: 192

Your proportion is lets say height/width ratio, so put it to a variable:

$ratio = $width/$height;

If you have a image like this: 100x200 px, your ratio is 1/2. If you would like to have an image that has 20px height, the width sould be 10 px.

$newHeight = 20;

$newWidth = round( $newHeight * $ratio ); //gives you 10

Also lets say you would like to make the width 10px, just use this for to find new width:

$newWidth = 10;

$newHeight = round( $newWidth / $ratio );

So you should specify at least one edge length, then ratio will help you to find other edge.

Upvotes: 0

Mr. Llama
Mr. Llama

Reputation: 20899

You should consider using imagecopyresampled or imagecopyresized.

If you're looking to keep the image scaled properly, you'll have to throw in a bit more arithmetic, but it shouldn't be too bad.

Here's a bit of pseudocode for scaling the image:

$max_height = 115;
if ($height > $max_height)
{
    $scale = $max_height / $height;
    $height = intval($height * $scale);
    $width  = intval($width  * $scale);
}

Here's a more generic form for if height and width must be less than 115:

$max_size = 115;
if (max($height, $width) > $max_size)
{
    $scale = $max_size / max($height, $width);
    $height = intval($height * $scale);
    $width  = intval($width  * $scale);
}

This guarantees that the largest dimension of the image (be it height or width) will be no greater than 115.

Upvotes: 1

Baba
Baba

Reputation: 95121

You should just look at http://www.php.net/manual/en/function.getimagesize.php#97564 it very direct

$max_width = 115; 
$max_height = 115;

list($width, $height) = getimagesize($imagePath);
$ratioh = $max_height/$height;
$ratiow = $max_width/$width;
$ratio = min($ratioh, $ratiow);

// New dimensions
$width = intval($ratio * $width);
$height = intval($ratio * $height);

Upvotes: 0

Adam Casey
Adam Casey

Reputation: 1620

If you mean they can't be larger than 115x115. The process you'd have to go by is something like:

If wider than 115, set width to be 115px and height = height/original_width * 115;

I have some code from an image uploader script which does this

    list($Width, $Height) = getimagesize($row["img_file"]);

    if(($Width > $Height) && ($Width > 115))
    {
        $Height = ceil(($Height / $Width) * 115) ;
        $Width = 115;
    }
    elseif(($Height > $Width) && ($Height > 115))
    {
        $Width = ceil(($Width / $Height)* 115);
        $Height = 115;
    }
    elseif(($Height > 115) && ($Height == $Width))
    {
        $Height = 115;
        $Width = 115;
    }

After you have the correct dimensions you can resize the image using imagecreatefromjpeg/png/gif and then imagecopyresampled.

Another section of code from the image upload script which resized images from URLs using the calculated dimensions

        if ($extension == "png")
        {
            $image = imagecreatefrompng($URLofImage)or die("Width Of Image: ".$widthofImage." Height Of Image: ".$heightofImage." Height: ".$height." Width: ".$width);
            $image_p = imagecreatetruecolor($widthofImage, $heightofImage)or die("Width Of Image: ".$widthofImage." Height Of Image: ".$heightofImage." Height: ".$height." Width: ".$width);
            imagealphablending($image_p, false);
            $color = imagecolorallocatealpha($image_p, 0, 0, 0, 0);
            imagesavealpha($image_p, true);
            list($width, $height) = getimagesize($URLofImage);

            imagecopyresampled($image_p, $image, 0, 0, 0, 0, $widthofImage, $heightofImage, $width, $height);
            imagepng($image_p, "./images/thumbs/".$name, 9);

            return ("./images/thumbs/".$name);
        }

Upvotes: 0

Related Questions