Scale an image proportionally

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
jalz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 340
Joined: Fri Sep 12, 2008 11:04 pm

Scale an image proportionally

Post by jalz » Sun Jul 27, 2014 3:50 pm

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
   


jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Scale an image proportionally

Post by jmburnod » Sun Jul 27, 2014 4:35 pm

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
https://alternatic.ch

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Scale an image proportionally

Post by [-hh] » Sun Jul 27, 2014 4:49 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 3:52 pm, edited 3 times in total.
shiftLock happens

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Scale an image proportionally

Post by jmburnod » Sun Jul 27, 2014 5:06 pm

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
https://alternatic.ch

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Scale an image proportionally

Post by [-hh] » Sun Jul 27, 2014 5:16 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 3:52 pm, edited 1 time in total.
shiftLock happens

jalz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 340
Joined: Fri Sep 12, 2008 11:04 pm

Re: Scale an image proportionally

Post by jalz » Sun Jul 27, 2014 5:50 pm

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

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Scale an image proportionally

Post by [-hh] » Sun Jul 27, 2014 6:27 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 3:53 pm, edited 2 times in total.
shiftLock happens

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10102
Joined: Fri Feb 19, 2010 10:17 am

Re: Scale an image proportionally

Post by richmond62 » Sun Jul 27, 2014 7:36 pm

Here's my take:
imager.livecode.zip
(8.19 KiB) Downloaded 291 times

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10102
Joined: Fri Feb 19, 2010 10:17 am

Re: Scale an image proportionally

Post by richmond62 » Sun Jul 27, 2014 7:49 pm

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

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Scale an image proportionally

Post by [-hh] » Sun Jul 27, 2014 8:53 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 3:53 pm, edited 1 time in total.
shiftLock happens

William Jamieson
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 212
Joined: Fri Feb 01, 2013 1:31 am
Contact:

Re: Scale an image proportionally

Post by William Jamieson » Mon Jul 28, 2014 5:07 am

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?

William Jamieson
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 212
Joined: Fri Feb 01, 2013 1:31 am
Contact:

Re: Scale an image proportionally

Post by William Jamieson » Mon Jul 28, 2014 5:24 am

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

Post Reply