Scaling an Image to a Rect

Geoff Canyon gcanyon at
Sat Oct 19 10:24:31 EDT 2013

Late to the party, but this command takes the long id of an image and a
width and height and resizes the image appropriately. The default is (I
think) what you're looking for, but you can also pass in true for bFit and
get the reverse: the image is still proportional, but instead of filling
the width and height (and potentially overflowing one of them in order to
maintain proportions) it fits within them. It doesn't do any math it
doesn't have to do, and it resizes the image in one statement (faster I
think than setting the height and width separately).

on resizeImage pID,H,W,bFit
   -- pID: long id of the image to resize
   -- H: the height of the rect to use
   -- W: the width of the rect to use
   -- bFit: if true, fit the image in the rect
   --        if false or empty (default), fill the rect with the image

   put the formattedWidth of pID into fW
   put the formattedHeight of pID into fH
   put H / fH into hRatio
   put W / fW into wRatio
   put the rect of pID into R

   if (hRatio > wRatio) is (bFit is true) then
      put round(item 1 of R + W) into item 3 of R
      put round(item 2 of R + fH * wRatio) into item 4 of R
      put round(item 1 of R + fW * hRatio) into item 3 of R
      put round(item 2 of R + H) into item 4 of R
   end if
   set the rect of pID to R
end resizeImage

On Tue, Oct 15, 2013 at 3:10 PM, Dan Friedman <dan at>wrote:

> Greetings!
> I have been piddling around with this for weeks and still can't get it
> right.  I kinda got it working... but it's still not always 100% correct.
>  So, I thought I would ask this list for assistance since most of the
> people here are far smarter than I!
> You have a rect and an image.  The goal is to completely fill the rect
> with the image.  But, we don't want to distort the image.  So, you need to
> proportionally scale the image up (if the image is smaller than the rect)
> or down (if the image is larger than the rect).   Remember that we want to
> completely fill rect.  It's ok (and expected) that some of the image will
> be cropped.
> Think of this as setting the background of a stack with an image.  You
> want to fill the entire background of the stack (regardless of the size of
> the stack), with the image (regardless of the rect of the image).  But we
> don't want to distort the image.
> Any thoughts?
> -Dan
> _______________________________________________
> use-livecode mailing list
> use-livecode at
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:

More information about the Use-livecode mailing list