首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将图片调整大小/裁剪/填充为固定大小

将图片调整大小/裁剪/填充为固定大小
EN

Stack Overflow用户
提问于 2009-04-14 11:15:08
回答 11查看 26.6K关注 0票数 8

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

假设我想要将图片从238 (w) X 182 (h)调整为210 / 150

我现在做的是:

代码语言:javascript
运行
复制
Original width / target width = 1.333333
Original Height / target Height = 1.213333

现在我取最小的因子。

现在,自从238 / 1.333333 = 210以来,我总是有正确的宽度。但是高度仍然是160

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

我需要裁剪吗?如果是这样的话,是怎么做的?

EN

回答 11

Stack Overflow用户

回答已采纳

发布于 2009-04-14 12:21:19

这个解决方案基本上与Can Berk Güder的相同,但在花了一些时间撰写和评论后,我想要发帖。

此函数创建的缩略图与您指定的大小完全相同。图像的大小将调整为最适合缩略图的大小。如果它在两个方向上都不完全适合,它将位于缩略图的中心。大量的评论解释了这一切。

代码语言:javascript
运行
复制
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;
}

示例用法:

代码语言:javascript
运行
复制
$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);
票数 26
EN

Stack Overflow用户

发布于 2009-04-14 11:35:02

这不会裁剪图片,但在必要时在新图像周围留出空间,我认为这是创建缩略图时更好的方法(比裁剪)。

代码语言:javascript
运行
复制
$w = 210;
$h = 150;

$orig_w = imagesx($original);
$orig_h = imagesy($original);

$w_ratio = $orig_w / $w;
$h_ratio = $orig_h / $h;

$ratio = $w_ratio > $h_ratio ? $w_ratio : $h_ratio;

$dst_w = $orig_w / $ratio;
$dst_h = $orig_h / $ratio;
$dst_x = ($w - $dst_w) / 2;
$dst_y = ($h - $dst_h) / 2;

$thumbnail = imagecreatetruecolor($w, $h);

imagecopyresampled($thumbnail, $original, $dst_x, $dst_y,
                   0, 0, $dst_w, $dst_h, $orig_w, $orig_h);
票数 11
EN

Stack Overflow用户

发布于 2009-04-14 11:22:54

你有Imagick吗?如果是这样的话,您可以用它加载图像并执行类似于thumbnailimage()的操作

在那里你可以跳过任何一个参数(高度或宽度),它将正确地调整大小。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/747101

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档