There is a few way to do this.
Have blank image with black background color and then just write your original image to it, basically merging your image to black blank template. That is where your script will come handy. This way you can always resize black template to desired size without changing main image.
In web page create div space with CSS with black background and center your image into that div with properly set padding/margins to top,bottom
Those are from the top of my head.