调整图像大小并用颜色填充比例间隙

发布于 2024-09-06 00:09:28 字数 316 浏览 3 评论 0原文

我正在将徽标上传到我的系统,它们需要修复在 60x60 像素框中。我有所有代码可以按比例调整它的大小,这不是问题。

我的 454x292px 图像变成了 60x38。问题是,我需要图片为 60x60,这意味着我想用白色填充顶部和底部(我可以用颜色填充矩形)。

理论上是我创建一个白色矩形,60x60,然后复制图像并将其大小调整为 60x38,并将其放入我的白色矩形中,从顶部开始 11 像素(这加起来就是我需要的总填充的 22 像素。

我会发布我的代码,但它很长,但如果需要的话我可以。

有谁知道如何做到这一点,或者你能给我指出执行此操作的代码/教程吗?

I am uploading logos to my system, and they need to fix in a 60x60 pixel box. I have all the code to resize it proportionately, and that's not a problem.

My 454x292px image becomes 60x38. The thing is, I need the picture to be 60x60, meaning I want to pad the top and bottom with white each (I can fill the rectangle with the color).

The theory is I create a white rectangle, 60x60, then I copy the image and resize it to 60x38 and put it in my white rectangle, starting 11px from the top (which adds up to the 22px of total padding that I need.

I would post my code but it's decently long, though I can if requested.

Does anyone know how to do this or can you point me to code/tutorial that does this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怕倦 2024-09-13 00:09:28

与GD:

$newWidth = 60;
$newHeight = 60;
$img = getimagesize($filename);
$width = $img[0];
$height = $img[1];
$old = imagecreatefromjpeg($filename); // change according to your source type
$new = imagecreatetruecolor($newWidth, $newHeight)
$white = imagecolorallocate($new, 255, 255, 255);
imagefill($new, 0, 0, $white);

if (($width / $height) >= ($newWidth / $newHeight)) {
    // by width
    $nw = $newWidth;
    $nh = $height * ($newWidth / $width);
    $nx = 0;
    $ny = round(fabs($newHeight - $nh) / 2);
} else {
    // by height
    $nw = $width * ($newHeight / $height);
    $nh = $newHeight;
    $nx = round(fabs($newWidth - $nw) / 2);
    $ny = 0;
}

imagecopyresized($new, $old, $nx, $ny, 0, 0, $nw, $nh, $width, $height);
// do something with new: like imagepng($new, ...);
imagedestroy($new);
imagedestroy($old);

With GD:

$newWidth = 60;
$newHeight = 60;
$img = getimagesize($filename);
$width = $img[0];
$height = $img[1];
$old = imagecreatefromjpeg($filename); // change according to your source type
$new = imagecreatetruecolor($newWidth, $newHeight)
$white = imagecolorallocate($new, 255, 255, 255);
imagefill($new, 0, 0, $white);

if (($width / $height) >= ($newWidth / $newHeight)) {
    // by width
    $nw = $newWidth;
    $nh = $height * ($newWidth / $width);
    $nx = 0;
    $ny = round(fabs($newHeight - $nh) / 2);
} else {
    // by height
    $nw = $width * ($newHeight / $height);
    $nh = $newHeight;
    $nx = round(fabs($newWidth - $nw) / 2);
    $ny = 0;
}

imagecopyresized($new, $old, $nx, $ny, 0, 0, $nw, $nh, $width, $height);
// do something with new: like imagepng($new, ...);
imagedestroy($new);
imagedestroy($old);
手长情犹 2024-09-13 00:09:28

http://php.net/manual/en/function.imagecopyresampled.php

这基本上就是您想要平滑复制并调整其大小的功能。

http://www.php.net/manual/en/function.imagecreatetruecolor。 php

用它创建一个新的黑色图像。

http://www.php.net/manual/en/function.imagefill。 php

那部分解释了如何将其填充为白色。

其余的如下。

http://php.net/manual/en/function.imagecopyresampled.php

That's basically the function you want to copy and resize it smoothly.

http://www.php.net/manual/en/function.imagecreatetruecolor.php

With that one you create a new black image.

http://www.php.net/manual/en/function.imagefill.php

That part explains how to fill it white.

The rest follows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文