本文介绍了将图片调整大小/裁剪/填充为固定大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将图片调整为固定大小.但是它必须将宽度和高度之间的因素保持在一起.

I need to resize a picture to a fixed size. But it has to keep the factors between the width and height.

说我想将图片的尺寸从238 (w) X 182 (h)调整为210 / 150

Say I want to resize a picture from 238 (w) X 182 (h) to 210 / 150

我现在要做的是:

Original width / target width = 1.333333
Original Height / target Height = 1.213333

现在我考虑最小的因素.

Now I take the smallest factor.

现在,从238 / 1.333333 = 210开始,我总是有正确的宽度.但是高度仍然是160.

Now I always have the right width since 238 / 1.333333 = 210.But the height is still 160.

如何在不破坏图片的情况下将高度降低到160?

How do I get the height down to 160 without ruining the pic?

我需要修剪吗?如果可以,怎么办?

Do I need to crop? If so how?

推荐答案

该解决方案与Can BerkGüder的解决方案基本相同,但是花了一些时间写和发表评论后,我才喜欢发帖.

This solution is basically the same as Can Berk Güder's, but after having spent some time writing and commenting, I felt like posting.

此功能创建的缩略图与您提供的缩略图大小完全一样.调整图像大小以使其最适合缩略图的大小.如果在两个方向上都不完全适合,则将其放在缩略图的中心.大量评论解释了这一过程.

This function creates a thumbnail that is exactly as big as the size you give it.The image is resized to best fit the size of the thumbnail. If it does not fit exactly in both directions, it's centered in the thumnail. Extensive comments explain the goings-on.

function thumbnail_box($img, $box_w, $box_h) {
    //create the image, of the required size
    $new = imagecreatetruecolor($box_w, $box_h);
    if($new === false) {
        //creation failed -- probably not enough memory
        return null;
    }


    //Fill the image with a light grey color
    //(this will be visible in the padding around the image,
    //if the aspect ratios of the image and the thumbnail do not match)
    //Replace this with any color you want, or comment it out for black.
    //I used grey for testing =)
    $fill = imagecolorallocate($new, 200, 200, 205);
    imagefill($new, 0, 0, $fill);

    //compute resize ratio
    $hratio = $box_h / imagesy($img);
    $wratio = $box_w / imagesx($img);
    $ratio = min($hratio, $wratio);

    //if the source is smaller than the thumbnail size,
    //don't resize -- add a margin instead
    //(that is, dont magnify images)
    if($ratio > 1.0)
        $ratio = 1.0;

    //compute sizes
    $sy = floor(imagesy($img) * $ratio);
    $sx = floor(imagesx($img) * $ratio);

    //compute margins
    //Using these margins centers the image in the thumbnail.
    //If you always want the image to the top left,
    //set both of these to 0
    $m_y = floor(($box_h - $sy) / 2);
    $m_x = floor(($box_w - $sx) / 2);

    //Copy the image data, and resample
    //
    //If you want a fast and ugly thumbnail,
    //replace imagecopyresampled with imagecopyresized
    if(!imagecopyresampled($new, $img,
        $m_x, $m_y, //dest x, y (margins)
        0, 0, //src x, y (0,0 means top left)
        $sx, $sy,//dest w, h (resample to this size (computed above)
        imagesx($img), imagesy($img)) //src w, h (the full size of the original)
    ) {
        //copy failed
        imagedestroy($new);
        return null;
    }
    //copy successful
    return $new;
}

示例用法:

$i = imagecreatefromjpeg("img.jpg");
$thumb = thumbnail_box($i, 210, 150);
imagedestroy($i);

if(is_null($thumb)) {
    /* image creation or copying failed */
    header('HTTP/1.1 500 Internal Server Error');
    exit();
}
header('Content-Type: image/jpeg');
imagejpeg($thumb);

这篇关于将图片调整大小/裁剪/填充为固定大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 04:37