Constrain image size to container

Paul Hibbert paulhibbert at
Wed Dec 11 22:08:09 EST 2013


I'm working on a small image utility right now, this is working for me, is it any use to you?

on scaleImage pImage
   local tScale_W,tScale_H,tFactor ="1"
   ## Check for a valid image extension ##
   if char -4 to -1 of pImage is not among the items of ".jpg,.png,.gif" then exit scaleImage
   lock screen
   ## Clear any previous image reference & cache ##
   set the filename of image "imageViewer" to empty
   ## Load the new image ##
   set the filename of image "imageViewer" to pImage
   ## Calculate the scale factors ## - I'm using a resizeable graphic canvas but these values could be hard coded
   put (the width of grc "canvas")/(the width of image "imageViewer") into tScale_W
   put (the height of grc "canvas")/(the height of image "imageViewer") into tScale_H
   ## Decide whether to use width or height scale factors ##
   if tScale_W > tScale_H then -- It's a portrait image
      put tScale_H into tFactor
   else -- It's a landscape or square image
      put tScale_W into tFactor
   end if
   ## Comment out the next line to allow small images to enlarge to fit the "canvas" area
   put min(1,tFactor) into tFactor -- Limit enlargement to 100% -- e.g. Change 1 to 2 for max 200% enlargement
   ## Scale the image to fit ##
   set the width of image "imageViewer" to round((the width of image "imageViewer") * tFactor)
   set the height of image "imageViewer" to round((the height of image "imageViewer") * tFactor)
   ## Keep the image in the correct place on screen ##
   set the loc of image "imageViewer" to the loc of grc "canvas"
   unlock screen
end scaleImage

You may be able to delete line 10 [ set the filename of image "imageViewer" to empty ], but I found better consistency with using it.

No restrictions on use! :•)


On 2013-12-11, at 10:12 AM, Magicgate Software - Skip Kimpel <skip at> wrote:

> I am pulling images from an online database but need the images to
> constrain to proportion and also stay within a certain size framework.  I
> have no control over the size of the images I am pulling otherwise I would
> just re-size everything.
> I am currently using this to accomplish the image retrieval:
> set the filename of image "imagelink" to ""
> Any help is greatly appreciated!
> _______________________________________________
> 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