Scaling images on the fly?

Jeanne A. E. DeVoto revolution at jaedworks.com
Sun Jan 2 20:19:19 EST 2005


At 5:09 PM -0600 1/2/05, docmann wrote:
>Is there a way to constrain or scale the display image while
>maintaining the proper size perspective of the original image, to
>avoid distortion?

Here's a quick way, assuming you want the image to be 100 pixels x 
100 pixels or so:

   create image "Holder" -- optional - if the image object doesn't exist yet
   set the filename of image "Holder" to myURL
   -- (myURL contains the URL of the image you want to show)
   -- get the ratio of the natural width/height of the image:
   put the width of image "Holder"/the height of image "Holder" into 
naturalRatio
   -- scale the image to respect its aspect ratio:
   if naturalRatio = 1 then -- square image
     set the rect of image "Holder" to (0,0,100,100)
   else if naturalRatio < 1 then -- image height is proportionally greater
     set the width of image "Holder" to 100
     set the height of image "Holder" to trunc(the width of image 
"Holder"/naturalRatio)
   else -- naturalRatio > 1, image width is proportionally greater
     set the height of image "Holder" to 100
     set the width of image "Holder" to trunc(the height of image 
"Holder" * naturalRatio)
   end if
   set the lockLoc of image "Holder" to true -- maintain current size
   set the loc of image "Holder" to 100,100 -- or wherever you want to put it

The above sets the smaller dimension of the image to 100 pixels. You 
can vary this to set the larger dimension to 100 pixels by setting 
the height to 100 first if naturalRatio < 1, and the width if 
naturalRatio > 1.
-- 
jeanne a. e. devoto ~ revolution at jaedworks.com
http://www.jaedworks.com


More information about the use-livecode mailing list