PHP 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?

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!

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.

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.