How can I resize an image then centralize it with a white background? Can it be done using php-Imagine library?

1.4k Views Asked by At

The effect I would like to generate is exactly like the example in this StackOverflow thread: (Related Question)

1.Resize image
2.Keep proportion
3.Add or Fill none-image areas with white background

Here are three examples of this process below:
1. If original is square no matter 640*640 or 1024*1024, just resize it to target dimension e.g. 480*480 will work.
enter image description here

  1. If the original input is vertical rectangular,the output should not be cropped and still be centerlized as below (the red dash edge marker is just make it easier to see the background is white and the proportion is kept after image resized to 480*480 px)
    enter image description here

  2. if the original input is horizontal rectangular, output like below, white background, keeps proportion and image uncroped , thus without losing anything after the image processing.
    enter image description here

So after I've clarified such a question, I would like to know:

  1. Is there a general name of such Custom Image Resize mentioned above?
  2. would like to know how to achieve it using php image library Imagine? is it doable using php-imagine, or have to switch to another library, e.g imagemagick?

P.S. If you would like to achieve the effect either in image-resizing or video resizing, you can use below FFMPEG single-line command. (thanks to @Mulvya, yes below code applies both to videos and image formats)

[run below built-in code in ffmpeg console to achieve the mentioned resize effect]

[video resize]

ffmpeg -i "[raw video url or videofile location.mp4]"  -vf "scale=iw*min(480/iw\,480/ih):ih*min(480/iw\,480/ih),pad=480:480:(480-iw)/2:(480-ih)/2:color=white" [save_path_and_filename].mp4  

[image resize]

ffmpeg -i "[raw image url or imagefile location.jpg]"  -vf "scale=iw*min(480/iw\,480/ih):ih*min(480/iw\,480/ih),pad=480:480:(480-iw)/2:(480-ih)/2:color=white" [save_path_and_filename].jpg
1

There are 1 best solutions below

0
On

Sorry i don'T have full answer for you and it's too long for a comment, but this can be done natively using some maths and php's http://php.net/manual/en/ref.image.php

Check in the area of imagecopyresize() imageecreate() imagescale and so on. It a pretty complex lib so you will have to do some try and mistake to get where you want to go.

From experience you can:

  • create an empty image, imagecreatetruecolor()
  • change background color to white
  • take a scetion of an image and paste it in the new created image using imagecopyresample() imagecopyresize()
  • export this image ans gif, png, jpg. imagepng(), imagejpg().
  • add watermark
  • and ALOT more.

Sorry i don'T have a more complete answer but depending on your need it can take a few mitues/hours to make it perfect. have fun.

On top of it, if you know ffmpeg, you can make both work together and make awesome media outputs. Cheers to you