PHP GD 库,在图像周围放置白色区域

发布于 2024-10-09 01:02:54 字数 290 浏览 3 评论 0原文

我正在从 flash 导出一些图像,然后在 php 中调整它们的大小。我正在使用 GD 库来执行此操作。然而,我在获得正确的尺寸方面遇到了一些困难......有些是横向的,有些是纵向的,我什至有需要将它们放入的尺寸的 div。

因此,当我将它们的大小调整到一定高度时,有些是(例如)150px x 30px,有些是 30px x 150px。问题在于 css 中的垂直对齐。

我认为解决方案是在图像周围放置空白,以便它们都具有相同的宽度和高度,但图像在垂直和水平方向上居中。

使用 GD 库执行此操作的最佳方法是什么?

I am exporting some images from flash and then resizing them in php. I am using the GD library to do this. However, I am having some difficulty getting the sizes right... some are landscape, some are portrait and I have even sized divs that they need to be put into.

So, when I resize them to a certain height there are some that are (for example) 150px x 30px and some that are 30px x 150px. The problem is with vertical alignment in the css.

I figure the solution is to just put whitespace around the images so that they all measure the same width and height but with the image centered in the middle vertically and horizontally.

What is the best way to do this with the GD library?

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

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

发布评论

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

评论(3

转角预定愛 2024-10-16 01:02:54

在这种情况下,只需创建一个 150x150 图像,用白色填充它,然后将图像粘贴到新图像上的适当位置。

$src = imagecreatefromjpeg(...); // your flash exported image
$dst = imagecreatetruecolor(150,150); // new blank 150x150 image
imagefill($dst, 0, 0, 0xFFFFFF); // flood fill with white

$new_x = ...
$new_y = ... // figure out resizing parameters for the $src image

imagecopyresampled($dst, $src, ...);

imagejpeg($dst, 'resized.jpg');

有关 copyresampled 参数的确切详细信息此处

In this case, just create a 150x150 image, fill it with white, and then paste your image into the appropriate spot on that new image.

$src = imagecreatefromjpeg(...); // your flash exported image
$dst = imagecreatetruecolor(150,150); // new blank 150x150 image
imagefill($dst, 0, 0, 0xFFFFFF); // flood fill with white

$new_x = ...
$new_y = ... // figure out resizing parameters for the $src image

imagecopyresampled($dst, $src, ...);

imagejpeg($dst, 'resized.jpg');

Exact details on the copyresampled parameters here.

开始看清了 2024-10-16 01:02:54

这是我编写的函数的链接,它将帮助您将任何大小的图像调整为任意大小。该功能还允许您对图像进行裁剪或加宽,以使其适合所需的宽高比。

https://www.spotlesswebdesign.com/blog.php?id=1

如果这有帮助,请选中此答案旁边的复选标记。谢谢!

Here is a link to a function I wrote, that will help you resize any sized image to any arbitrary size. The function also allows you to either crop-to-fit, or letterbox your image in order to make it fit the desired aspect ratio.

https://www.spotlesswebdesign.com/blog.php?id=1

If this helps, please select the check mark next to this answer. Thanks!

做个少女永远怀春 2024-10-16 01:02:54

仔细看看这个 stackoverflow 问题,您需要您的尺寸新图像,原始图像的尺寸和公式基本上是,

$startx=($newx_size/2)-($oldx_size/2),     
$starty=($newy_size/2)-($oldy_size/2)

起始x和y是新图像的中点(newX和newY除以2)减去调整大小图像的高度/宽度($oldx和oldy每个除以二。)首先调整它们的大小,获取它们的新尺寸并相应地放置它们。

take a close look at this stackoverflow question you need the dimensions of your new image, your dimensions of your original image and the formula is basically,

$startx=($newx_size/2)-($oldx_size/2),     
$starty=($newy_size/2)-($oldy_size/2)

your start x and y are the middle points of your new image (newX and newY divided by 2) minus the height/width of the resized image ($oldx and oldy each divided by two.) resize them first, get their new dimensions and place them accordingly.

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