REBOL [ title: "Calculate Image Aspect Ratio" date: 20-Mar-2008 author: ["James Irwin" "The Untitled One"] email: rebolyte--gmail--com license: ['MIT http://www.opensource.org/licenses/mit-license.php] ] calc-ratio: calculate-image-aspect-ratio: func [ "Calculate a new aspect ratio from given dimensions." image-size [pair!] "Size of original image." desired [integer!] "New desired width." /height "Instead of width, specify new desired height." ][ orig-width: first image-size orig-height: second image-size either height [ ; Use height variable x: orig-width * desired desired-opposite: round/half-ceiling x / orig-height res: to pair! rejoin [desired-opposite "x" desired] ][ ; Use width variable x: orig-height * desired desired-opposite: round/half-ceiling x / orig-width res: to pair! rejoin [desired "x" desired-opposite] ] ] text-is-number?: func [text [string!] /local dig=] [ dig=: charset "0123456789" parse text [some dig=] ] main-lay: layout [ style lab label 50x22 right style field field 40 style btn btn 60 white h3 "Original" across lab "Width:" orig-width-fld: field lab "Height:" orig-height-fld: field return h3 "New" return lab "Width:" new-width-fld: field lab "Height:" new-height-fld: field return btn "Width" [ if any [empty? orig-width-fld/text empty? orig-height-fld/text] [ alert "You must enter the original dimensions of your image!" exit ] either text-is-number? rejoin [orig-width-fld/text orig-height-fld/text new-width-fld/text] [ orig-width-val: to integer! orig-width-fld/text orig-height-val: to integer! orig-height-fld/text new-width-val: to integer! new-width-fld/text ][ alert "All fields must contain valid integers!" exit ] either empty? new-width-fld/text [ alert "You must enter a new desired width!" exit ][ ;request/ok "All fields contain valid integers. Continuing." orig-dimensions: to pair! rejoin [orig-width-val "x" orig-height-val] new-height-fld/text: second calc-ratio orig-dimensions new-width-val show main-lay ] ] btn "Height" [ if any [empty? orig-width-fld/text empty? orig-height-fld/text] [ alert "You must enter the original dimensions of your image!" exit ] either text-is-number? rejoin [orig-width-fld/text orig-height-fld/text new-height-fld/text] [ orig-width-val: to integer! orig-width-fld/text orig-height-val: to integer! orig-height-fld/text new-height-val: to integer! new-height-fld/text ][ alert "All fields must contain valid integers!" exit ] either empty? new-height-fld/text [ alert "You must enter a new desired height!" exit ][ ;request/ok "All fields contain valid integers. Continuing." orig-dimensions: to pair! rejoin [orig-width-val "x" orig-height-val] new-width-fld/text: first calc-ratio/height orig-dimensions new-height-val show main-lay ] ] ] view main-lay