Scale images to FIT/FILL bounding box in PHP using GD

Quite often I was in need a function to scale an image to the size right to fit into bounding box.

For example, displaying images of real estate or product image in the shop site. Images can be different sizes, less or bigger of target size, portrait or landscape, etc..

I wrote little helper function in PHP that does that tasks using two modes: FIT and FILL. It uses standard PHP-GD extension.

Why did I re-invent the wheel?

Solution seems easy and should be widely deployed, however I haven’t found a working one on the Internet.  All my findings didn’t do the job right and the way they work was not clear and I was not able to fix them.

My function accepts source image, destination image and operation (FIT or FILL). It scales image UP and DOWN and uses a very simple algorithm. See examples below:

Image FIT/FILL example using PHP GD

Here is an example to give the idea how it works in two different modes

Fit mode

In fit mode it scales image to FIT image into bounding BOX leaving free space on the edges.


Fill mode

In FILL mod it scales image proportionally to fill entire bounding box with image, so no white space is left on the borders.


Like the pictures, ehh? :)

Source code

Examples below are generated in real time using code below, feel free to use it and improve.


  1. Thanks for your code.

    Asking for permission to use it on my project in VB 🙂

    Enum ResizeMethod
    End Enum

    Function Resize(ByVal source As Size, ByVal destination As Size, ByVal method As ResizeMethod) As Rectangle
    Dim resized As RectangleF
    resized.Width = destination.Width
    resized.Height = destination.Width * source.Height / source.Width
    resized.X = 0
    resized.Y = (destination.Height – resized.Height) / 2
    If IIf(method = ResizeMethod.Fill, resized.Height destination.Height) Then
    resized.Width = destination.Height * source.Width / source.Height
    resized.Height = destination.Height
    resized.X = (destination.Width – resized.Width) / 2
    resized.Y = 0
    End If
    Return Rectangle.Round(resized)
    End Function

  2. 874841 850960I agree with most of your points, but a couple of require to be discussed further, I will hold a small talk with my partners and perhaps I will look for you some suggestion soon. 362393

  3. 792653 671451This post is dedicated to all people who know what is billiard table; to all people who do not know what is pool table; to all people who want to know what is billiards; 392901

  4. 968706 567200An interesting discussion is worth comment. I think that you should write more on this topic, it might not be a taboo subject but generally people are not enough to speak on such topics. To the next. Cheers 876863

  5. this is great solution for real estate websites, as different branches within the same company use different cameras hence the sizes are all over the place

  6. Hi, thank you for your code, will this work if i change source image to array(), like $src = array();
    I’m trying to ‘fit’ all thumbnails at once, and merge them into sprite at once. My sprite generator works but thumbnails are different sizez,and aligned to center. Is using python is better for generatin about 40 thumbnails at once? Sorry for my poor English

Leave a Reply

Your email address will not be published.