PHP GD 库,在图像周围放置白色区域
我正在从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这种情况下,只需创建一个 150x150 图像,用白色填充它,然后将图像粘贴到新图像上的适当位置。
有关 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.
Exact details on the copyresampled parameters here.
这是我编写的函数的链接,它将帮助您将任何大小的图像调整为任意大小。该功能还允许您对图像进行裁剪或加宽,以使其适合所需的宽高比。
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!
仔细看看这个 stackoverflow 问题,您需要您的尺寸新图像,原始图像的尺寸和公式基本上是,
起始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,
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.