Page 1 of 1

Scale an image proportionally

Posted: Sun Jul 27, 2014 3:50 pm
by jalz
Hi Guys,

Im trying to scale an image proportionally so it fits into 350x350 container. I found an algorithm online which I believe will do this, but the ceiling command is failing. Can someone identify what Im doing wrong please?

Thanks
Jalz

Code: Select all


   // Target dimensions
   put 350 into tMaxWidth
   put 350 into tMaxHeight
   
   // Calculate the scaling we need to do to fit the image inside our frame
   put min(tMaxWidth/tPaperWidth, tMaxHeight/tPaperHeight) into tScale
   
   // Get the new dimensions
   put ceiling(tScale*tPaperWidth) into tNewWidth 
   put ceiling(tScale*tPaperHeight) into tNewHeight
   
   set the height of image tImageName to tNewHeight
   set the width of image tImageName to tNewWidth
   


Re: Scale an image proportionally

Posted: Sun Jul 27, 2014 4:35 pm
by jmburnod
Hi Jalz,
I understand tPaperWidth and tPaperHeight are empty and I don't know "ceiling" function but you can use something like that
to do what you want.

Code: Select all

on doScaleImg  
   put "myImage" into tImageName
   put the width of img tImageName into tPaperWidth
   put the height of img tImageName into tPaperHeight
   // Target dimensions
      put 350 into tMaxWidth
      put 350 into tMaxHeight
      
      // Calculate the scaling we need to do to fit the image inside our frame
      put min(tMaxWidth/tPaperWidth, tMaxHeight/tPaperHeight) into tScale
      
      // Get the new dimensions
   --jmb I don't know ceiling function
   --   put ceiling(tScale*tPaperWidth) into tNewWidth 
   --   put ceiling(tScale*tPaperHeight) into tNewHeight
   
   -- but you can use this instead 
      put round(tScale*tPaperWidth) into tNewWidth 
      put round(tScale*tPaperHeight) into tNewHeight
      set the height of image tImageName to tNewHeight 
      set the width of image tImageName to tNewWidth
end doScaleImg
Best regards
Jean-Marc

Re: Scale an image proportionally

Posted: Sun Jul 27, 2014 4:49 pm
by [-hh]
..........

Re: Scale an image proportionally

Posted: Sun Jul 27, 2014 5:06 pm
by jmburnod
Hi Hermann,
It happens something strange with your link
"The requested topic does not exist."
And when I search "imageZoomAndFit.livecode" by search Tools I get a result but the link is bad. :shock:
Kind regards
Jean-Marc

Re: Scale an image proportionally

Posted: Sun Jul 27, 2014 5:16 pm
by [-hh]
..........

Re: Scale an image proportionally

Posted: Sun Jul 27, 2014 5:50 pm
by jalz
Hi Jean-Marc

Thanks for posting you solution, but the scaling don't seem to be working properly. I've got an image with the following proportions, 612hx678w. When I reduce it down manually using the shift key to fit my 350x350 image dimension I get the following size:350hx313w.

When I use your code, it gives me 350hx270w which is not a true. I seem to get this proportional resize when I followed the tutorial http://lessons.runrev.com/m/4071/l/1501 ... e-an-image

Thanks
Jalz

Re: Scale an image proportionally

Posted: Sun Jul 27, 2014 6:27 pm
by [-hh]
..........

Re: Scale an image proportionally

Posted: Sun Jul 27, 2014 7:36 pm
by richmond62
Here's my take:
imager.livecode.zip
(8.19 KiB) Downloaded 292 times

Re: Scale an image proportionally

Posted: Sun Jul 27, 2014 7:49 pm
by richmond62
on mouseUp
if exists(img "picture") then
delete img "picture"
end if
answer file "choose the image to import"
if the result = "cancel" then
exit mouseUp
else
import paint from file it
set the name of the last image to "picture"
end if
put the width of img "picture" into WIDD
put the height of img "picture" into HITE
if HITE > WIDD then
set the height of img "picture" to 350
put HITE/350 into DIVVER
put WIDD/DIVVER into WDIV
set the width of img "picture" to (WDIV div 1)
else
set the width of img "picture" to 350
put WIDD/350 into DIVVER
put HITE/DIVVER into HDIV
set the height of img "picture" to (HDIV div 1)
end if
set the moveSpeed to 65000
move img "picture" to 400,300
end mouseUp

Re: Scale an image proportionally

Posted: Sun Jul 27, 2014 8:53 pm
by [-hh]
..........

Re: Scale an image proportionally

Posted: Mon Jul 28, 2014 5:07 am
by William Jamieson
Side thought:

Would you guys happen to know if (Export image to size 350,350) would scale the image proportionally and crop the edges automatically? Anyone have any experience with this?

Re: Scale an image proportionally

Posted: Mon Jul 28, 2014 5:24 am
by William Jamieson
If you care to look through it, (its pretty long and I know its probably not the most efficient way of doing it), but this is the script i use to resize images proportionally. Not pretty to look at but works very well for me.

(I moved around a couple things so let me know if this works still after taking out some unnecessary parts)

Code: Select all

##NOTE JPEGQUALITY
   set the JPEGQuality to sJPEGQuality
   
   ##Copies the Resize Image into the local variable at the desired size
   set the resizeQuality of img "AndroidTempImage" of me to "best"
   
   ##LArger IMG
   if the width of img "AndroidTempImage" of me > 1000 and the height of img "AndroidTempImage" of me > 750 then
      
      put the width of img "AndroidTempImage" of me into tWidth
      put the height of img "AndroidTempImage" of me into tHeight
      
      if tWidth > tHeight * (4/3) then
         set the height of img "AndroidTempImage" of me to 750
         put 750 / tHeight into tRatio
         set the width of img "AndroidTempImage" of me to tWidth * tRatio
         
      else if tWidth <= tHeight then
         set the width of img "AndroidTempImage" of me to 1000
         put 1000 / tWidth into tRatio
         set the height of img "AndroidTempImage" of me to tHeight * tRatio
         
      end if
      
      ##Maintains aspect ratio
      put (the width of img "AndroidTempImage" of me) & comma & the height \
            of img "AndroidTempImage" of me into tImageDimensions
      export snapshot from img "AndroidTempImage" of me at size tImageDimensions \
            to pSavePost["Photo:Hyperblob"] as JPEG
      
      
      ##Smaller IMG
   else if the width of img "AndroidTempImage" of me <1000 or the height of img "AndroidTempImage" of me < 750 then
      put the width of img "AndroidTempImage" of me into tWidth
      put the height of img "AndroidTempImage" of me into tHeight
      
      if tWidth > tHeight * (4/3) then
         set the width of img "AndroidTempImage" of me to 1000
         put 1000 / tWidth into tRatio
         set the height of img "AndroidTempImage" of me to tHeight * tRatio
         
      else if tWidth <= tHeight then
         set the height of img "AndroidTempImage" of me to 750
         put 750 / tHeight into tRatio
         set the width of img "AndroidTempImage" of me to tWidth * tRatio
      end if
      
      ##Maintains aspect ratio
      put (the width of img "AndroidTempImage" of me) & comma & the height \
            of img "AndroidTempImage" of me into tImageDimensions
      export snapshot from img "AndroidTempImage" of me at size tImageDimensions \
            to pSavePost["Photo:Hyperblob"] as JPEG
   end if