Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smart way of resizing images in PHP [closed]

I was wondering if someone can help me with an image resize function written in PHP but one wich has to resize an image, but in a manner like PHPThumb does. So, if I set the width and height of the new image, the function must fit the uploaded image (and mantain aspect ratio) in the new width and height.

Any help is appreciated.

Thank you.

like image 730
Psyche Avatar asked Nov 18 '25 20:11

Psyche


2 Answers

I wrote this a couple years ago and it does exactly what you're looking for. Keep in mind this only calculates the width and height, you must call Imagick yourself to actually apply these calculations.

/**
* ImageIntelligentResize()
* 
* @global Intelligently resizes images using a providid max width and height
* @param mixed $imagePath
* @param mixed $maxWidth
* @param mixed $maxHeight
* @param mixed $alwaysUpscale
* @return
*/
function ImageIntelligentResize( $imagePath, $maxWidth, $maxHeight, $alwaysUpscale )
{
    // garbage in, garbage out
    if ( IsNullOrEmpty($imagePath) || !is_file($imagePath) || IsNullOrEmpty($maxWidth) || IsNullOrEmpty($maxHeight) )
    {
        return array("width"=>"", "height"=>"");
    }

    // if our thumbnail size is too big, adjust it via HTML
    $size = getimagesize($imagePath);
    $origWidth = $size[0];
    $origHeight = $size[1];

    // Check if the image we're grabbing is larger than the max width or height or if we always want it resized
    if ( $alwaysUpscale || $origWidth > $maxWidth || $origHeight > $maxHeight )
    {   
        // it is so let's resize the image intelligently
        // check if our image is landscape or portrait
        if ( $origWidth > $origHeight )
        {
            // target image is landscape/wide (ex: 4x3)
            $newWidth = $maxWidth;
            $ratio = $maxWidth / $origWidth;
            $newHeight = floor($origHeight * $ratio);
            // make sure the image wasn't heigher than expected
            if ($newHeight > $maxHeight)
            {
                // it is so limit by the height
                $newHeight = $maxHeight;
                $ratio = $maxHeight / $origHeight;
                $newWidth = floor($origWidth * $ratio);
            }
        }
        else
        {
            // target image is portrait/tall (ex: 3x4)
            $newHeight = $maxHeight;
            $ratio = $maxHeight / $origHeight;
            $newWidth = floor($origWidth * $ratio);
            // make sure the image wasn't wider than expected
            if ($newWidth > $maxWidth)
            {
                // it is so limit by the width
                $newWidth = $maxWidth;
                $ratio = $maxWidth / $origWidth;
                $newHeight = floor($origHeight * $ratio);
            }
        }
    }
    // it's not, so just use the current height and width
    else
    {
        $newWidth = $origWidth;
        $newHeight = $origHeight;
    }   

    return array("width"=>$newWidth, "height"=>$newHeight);
}
like image 88
TravisO Avatar answered Nov 20 '25 12:11

TravisO


When i started learning OOP, i wroted this image class to practice myself.

It uses GD, ans surely can be improved, hope it helps.

edit: after 2 downvotes for a issue where i dont have any control, i put the class on pastie.

The class

The Example

Remember, the class works fine, but it was a practice in order to learn basics OOP.

I hope this will be ok, you lazy users ;)

at Christmas we are all better people

like image 33
Strae Avatar answered Nov 20 '25 10:11

Strae



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!