简体   繁体   English

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

[英]PHP GD library, putting white area around an image

I am exporting some images from flash and then resizing them in php. 我正在从Flash导出一些图像,然后在php中调整它们的大小。 I am using the GD library to do this. 我正在使用GD库来执行此操作。 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. 但是,我在调整尺寸时遇到了一些困难……有些是风景,有些是肖像,我甚至还有一些需要放入的div。

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. 因此,当我将它们调整为一定高度时,会有一些(例如)150px x 30px和一些30px x 150px。 The problem is with vertical alignment in the css. 问题在于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? GD库执行此操作的最佳方法是什么?

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. 在这种情况下,只需创建一个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');

Exact details on the copyresampled parameters here . 有关此处的copyresampled参数的确切详细信息。

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, 仔细研究这个stackoverflow问题,您需要新图像的尺寸,原始图像的尺寸和公式基本上是

$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. 您的起始x和y是新图像的中间点(newX和newY除以2)减去调整大小后的图像的高度/宽度($ oldx和oldy分别除以2。)首先调整它们的大小,获取其新尺寸,然后相应地放置它们。

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 https://www.spotlesswebdesign.com/blog.php?id=1

If this helps, please select the check mark next to this answer. 如果有帮助,请选择此答案旁边的复选标记。 Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM