在PHP中调整图像大小的智能方法 [英] Smart way of resizing images in PHP

查看:67
本文介绍了在PHP中调整图像大小的智能方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以用PHP编写的图像调整大小功能来帮助我,但是一个人必须像PHPThumb那样调整图像大小.因此,如果我设置了新图像的宽度和高度,则该功能必须以新的宽度和高度适合上传的图像(并保持宽高比).

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.

感谢您的帮助.

谢谢.

推荐答案

几年前,我写了这篇文章,它确实可以满足您的需求.请记住,这仅用于计算宽度和高度,您必须致电Imagick才能实际应用这些计算.

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);
}

这篇关于在PHP中调整图像大小的智能方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆