Constrain image size to container

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


Skip,

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! :•)

Paul


On 2013-12-11, at 10:12 AM, Magicgate Software - Skip Kimpel <skip at magicgate.com> 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 "http://www.mysite.com/imgch.jpg"
> 
> Any help is greatly appreciated!
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode





More information about the use-livecode mailing list