计算用于调整大小的图像大小比率

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了计算用于调整大小的图像大小比率相关的知识,希望对你有一定的参考价值。

我有一个定义的固定宽度和高度来调整图像大小。但是,我有这个问题,因为图像可以有任何类型的尺寸比(它可以是垂直或水平)。在这种情况下,固定的宽度和高度会引起问题。我想以更智能的方式计算宽度和高度。

例如,假设我已经定义了宽度1024px和高度768px。我想调整一个垂直的图像(高度为1100px,宽度为200px)。所以在我的情况下它会调整到固定大小(1024x768),所以宽度将从100px增加到768px,这将是非常难看的。同样,如果图像的高度小于768px,则会将力度增加到768px。

因此,我想根据原始图像尺寸比率计算新的图像尺寸。让我们说如果上面的示例图像应该调整到最大高度768px,但那么宽度呢?它已经小于我的“最大宽度”,即200px,那么宽度应保持不变吗?还是应该进一步减少?

同样,如果图像的高度为200px,宽度为1100px。所以宽度应减少到1024px,但高度怎么样?

第三个问题是,假设高度和宽度都超过最大高度和最大宽度,让我们说宽度:1100px和高度:4000px。现在,由于宽度和高度都超过最大宽度和最大高度,但图像是垂直的,它将使其成为水平。那么如何在这种情况下检查是否应根据最大高度或根据最大宽度调整图像大小?

我很感激任何帮助。

答案

这是我个人抓取图像大小调整代码的代码。首先,您需要的数据:

list($originalWidth, $originalHeight) = getimagesize($imageFile);
$ratio = $originalWidth / $originalHeight;

然后,此算法尽可能地将图像拟合到目标大小,保持原始宽高比,而不是将图像拉伸大于原始图像:

$targetWidth = $targetHeight = min($size, max($originalWidth, $originalHeight));

if ($ratio < 1) {
    $targetWidth = $targetHeight * $ratio;
} else {
    $targetHeight = $targetWidth / $ratio;
}

$srcWidth = $originalWidth;
$srcHeight = $originalHeight;
$srcX = $srcY = 0;

这会使图像完全填满目标尺寸,而不是拉伸它:

$targetWidth = $targetHeight = min($originalWidth, $originalHeight, $size);

if ($ratio < 1) {
    $srcX = 0;
    $srcY = ($originalHeight / 2) - ($originalWidth / 2);
    $srcWidth = $srcHeight = $originalWidth;
} else {
    $srcY = 0;
    $srcX = ($originalWidth / 2) - ($originalHeight / 2);
    $srcWidth = $srcHeight = $originalHeight;
}

这会实际调整大小:

$targetImage = imagecreatetruecolor($targetWidth, $targetHeight);
imagecopyresampled($targetImage, $originalImage, 0, 0, $srcX, $srcY, $targetWidth, $targetHeight, $srcWidth, $srcHeight);

在这种情况下,$size只是宽度和高度(方形目标大小)的一个数字。我相信你可以修改它以使用非方形目标。它还应该为您提供可以使用的其他调整大小算法的灵感。

另一答案

如果给出或不给出最大高度或宽度,使用@(jilles de wit)逻辑

注意事项:这些应该已经定义了!

$mh = given height limit; //optional
$mw = given width limit; //optional

$height = $nh =[your original height];
$width = $nw =[your original width];

代码

if($mh || $mw){
if(is_numeric($mh)){$h_fact = $mh / $nh;}
if(is_numeric($mw)){$v_fact = $mw / $nw;}

if(is_numeric($v_fact) && is_numeric($h_fact)  ){$im_fact = min($v_fact, $h_fact);}else{$im_fact=is_numeric($v_fact)?$v_fact:$h_fact;}
$nw = $nw * $im_fact;
$nh = $nh * $im_fact;
}

重采样

$dst_img = imagecreatetruecolor($nw,$nh);
imagecopyresampled ($dst_img, $image, 0, 0, 0, 0, $nw, $nh, $width , $height);
另一答案
class Image_Aspect_Ratio_Resize {
var $image_to_resize;
var $new_width;
var $new_height;
var $ratio;
var $new_image_name;
var $save_folder;

function resize() {
    if (!file_exists($this->image_to_resize)) {
        exit("File " . $this->image_to_resize . " does not exist.");
    }

    $info = GetImageSize($this->image_to_resize);

    if (empty($info)) {
        exit("The file " . $this->image_to_resize . " doesn't seem to be an image.");
    }

    $width = $info[0];
    $height = $info[1];
    $mime = $info['mime'];

    /* Keep Aspect Ratio? */

    if ($this->ratio) {
        $thumb = ($this->new_width < $width && $this->new_height < $height) ? true : false; // Thumbnail
        $bigger_image = ($this->new_width > $width || $this->new_height > $height) ? true : false; // Bigger Image

        if ($thumb) {
            if ($this->new_width >= $this->new_height) {
                $x = ($width / $this->new_width);

                $this->new_height = ($height / $x);
            } else if ($this->new_height >= $this->new_width) {
                $x = ($height / $this->new_height);

                $this->new_width = ($width / $x);
            }
        } else if ($bigger_image) {
            if ($this->new_width >= $width) {
                $x = ($this->new_width / $width);

                $this->new_height = ($height * $x);
            } else if ($this->new_height >= $height) {
                $x = ($this->new_height / $height);

                $this->new_width = ($width * $x);
            }
        }
    }


    $type = substr(strrchr($mime, '/'), 1);

    switch ($type) {
        case 'jpeg':
            $image_create_func = 'ImageCreateFromJPEG';
            $image_save_func = 'ImageJPEG';
            $new_image_ext = 'jpg';
            break;

        case 'png':
            $image_create_func = 'ImageCreateFromPNG';
            $image_save_func = 'ImagePNG';
            $new_image_ext = 'png';
            break;

        case 'bmp':
            $image_create_func = 'ImageCreateFromBMP';
            $image_save_func = 'ImageBMP';
            $new_image_ext = 'bmp';
            break;

        case 'gif':
            $image_create_func = 'ImageCreateFromGIF';
            $image_save_func = 'ImageGIF';
            $new_image_ext = 'gif';
            break;

        case 'vnd.wap.wbmp':
            $image_create_func = 'ImageCreateFromWBMP';
            $image_save_func = 'ImageWBMP';
            $new_image_ext = 'bmp';
            break;

        case 'xbm':
            $image_create_func = 'ImageCreateFromXBM';
            $image_save_func = 'ImageXBM';
            $new_image_ext = 'xbm';
            break;

        default:
            $image_create_func = 'ImageCreateFromJPEG';
            $image_save_func = 'ImageJPEG';
            $new_image_ext = 'jpg';
    }

    // New Image
    $image_c = ImageCreateTrueColor($this->new_width, $this->new_height);

    $new_image = $image_create_func($this->image_to_resize);

    ImageCopyResampled($image_c, $new_image, 0, 0, 0, 0, $this->new_width, $this->new_height, $width, $height);

    if ($this->save_folder) {
        if ($this->new_image_name) {
            $new_name = $this->new_image_name . '.' . $new_image_ext;
        } else {
            $new_name = $this->new_thumb_name(basename($this->image_to_resize)) . '_resized.' . $new_image_ext;
        }

        $save_path = $this->save_folder . $new_name;
    } else {
        /* Show the image without saving it to a folder */
        header("Content-Type: " . $mime);

        $image_save_func($image_c);

        $save_path = '';
    }

    $process = $image_save_func($image_c, $save_path);

    return array('result' => $process, 'new_file_path' => $save_path);
}}

/ *函数调用* /

$resize_image = new Image_Aspect_Ratio_Resize;
$new_width = (int) $_POST['new_width'];
$new_height = (int) $_POST['new_height'];
$resize_image->new_width = $new_width;
$resize_image->new_height = $new_height;
$resize_image->image_to_resize = $image; // Full Path to the file
$resize_image->ratio = true; // Keep aspect ratio
// Name of the new image (optional) - If it's not set a new will be added automatically
$resize_image->new_image_name = 'water_lilies_thumbnail';
/* Path where the new image should be saved. If it's not set the script will output the image without saving it */
$resize_image->save_folder = 'thumbs/';
$process = $resize_image->resize(); // Output image
另一答案

这个怎么样:

double ratio = imageWidth/imageHeight;
int newHeight = Math.min(displayHeight, displayWidth / ratio); 
int newWidth =  Math.min(displayWidth, displayHeight * ratio); 
另一答案
$ratio = $originalWidth / $originalHeight

如果你想改变高度:

$targetWidth = $targetHeight * $ratio

如果要更改宽度:

$targetHeight = $targetWidth / $ratio
另一答案

您想要的是保持原始图像的纵横比。这是图像的宽度和高度之间的比率。因此,您需要计算在垂直和水平方向上调整图像大小的因子,然后保持两者中的较高者。在伪代码中:

target_height = 768
target_width = 1024
# v_fact and h_fact are the factor by which the original vertical / horizontal
# image sizes should be multiplied to get the image to your target size.
v_fact = target_height / im_height 
h_fact = target_width / im_width
# you want to resize the image by the same factor in both vertical 
# and horizontal direction, so you need to pick the correct factor from
# v_fact / h_fact so that the largest (relative to target) of the new height/width
# equals the target height/width and the smallest is lower than the target.
# this is the lowes

以上是关于计算用于调整大小的图像大小比率的主要内容,如果未能解决你的问题,请参考以下文章

调整图像大小后检测公差颜色

为纵向和横向模式调整 UIImage 的大小

上传前调整图像大小 - 将画布转换为文件对象

使用巨大(6000+ 宽)图像调整 GD 图像大小(带 CI)

使用 PHP 调整图像大小

Asp.net 图像大小调整质量