Scaling an Image to a Rect

Dan Friedman dan at clearvisiontech.com
Tue Oct 15 16:57:34 EDT 2013


Craig,

Thanks for replying.  I am aware of the width and height properties.  But, I guess I can't manage it.  Perhaps you might take a look at this snippet and see what I'm doing wrong.  This works - but not always.

on resizeImage pImageID,fRectH,fRectW
  put the formattedWidth of pImageID into fImageW
  put the formattedHeight of pImageID into fImageH
  
  try
    if fImageW > fImageH then
      if fRectW < fRectH then
        --it's taller than wide
        put fImageH / fRectH into tRatio
        put fImageW / tRatio into tWidth
        set the width of pImageID to tWidth
        set the height of pImageID to fRectH
      else
        put fImageW / fRectW into tRatio
        put fImageH / tRatio into tHeight
        set the width of pImageID to fRectW
        set the height of pImageID to tHeight
      end if
    else
      --the image is higher than tall
      if fRectW < fRectH then
        --the area is taller then wide
        put fImageH / fRectH into tRatio
        put fImageW / tRatio into tWidth
        set the width of pImageID to tWidth
        set the height of pImageID to fRectH
      else
        --the area is wider then tall
        put fImageW / fRectW into tRatio
        put fImageH / tRatio into tHeight
        set the width of pImageID to fRectW
        set the height of pImageID to tHeight
      end if
    end if
  end try
  
  set the loc of pImageID to fRectW/2,fRectH/2
end resizeImage




> So the aspect ratio is to remain constant, that is, the ratio of width to height.
> 
> If you want to scale the image, this ratio must be maintained. If the new rect, for example is to be 1,5 times as wide as the old, but the new height is only 1,4 times as tall as the old, then the larger (width) value has to be used for both, and the excess height cropped. Similarly, if the image is scaled down, the larger value is to be "filled", and the other cropped. In all cases, the ratio is maintained.
> 
> Using the several properties, "width", "height", "rect", "topLeft", etc, can you manage this? Write back if not, but it will be a terrific exercise for learning, and you should try first.
> 
> Craig Newman
> 


>> 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
>> 





More information about the use-livecode mailing list