image scaling keeping width/hight proportion (imlib2) pasted by mario-goulart on Sat Jan 26 14:07:17 2013

(define (flonum->fixnum num)
  (inexact->exact (round num)))

(define (image-scale/proportional image max-dimension)
  ;; Scale the given image keeping width/height proportion and
  ;; limiting the new values to `max-dimension'.
  (let* ((w (image-width image))
         (h (image-height image))
         (w-proportion (/ w max-dimension))
         (h-proportion (/ h max-dimension))
         (scale-factor (if (> h-proportion w-proportion)
                           h-proportion
                           w-proportion)))
    (image-scale image
                 (flonum->fixnum (/ w scale-factor))
                 (flonum->fixnum (/ h scale-factor)))))

Improved proportional image scaling (untested) added by DerGuteMoritz on Sat Jan 26 16:50:56 2013

(define (image-scale/proportional image #!key max-dimension (max-width max-dimension) (max-height max-dimension))
  (let* ((w (image-width image))
         (h (image-height image))
         (w-proportion (and max-width (/ w max-width)))
         (h-proportion (and max-height (/ h max-height)))
         (scale-factor (cond ((and (not h-proportion) (not w-proportion))
                              (error "Must specify either max-dimension or both max-width and max-height."))
                             ((not h-proportion) w-proportion)
                             ((not w-proportion) h-proportion)
                             (else (if (> h-proportion w-proportion)
                                       h-proportion
                                       w-proportion)))))
    (image-scale image
                 (flonum->fixnum (/ w scale-factor))
                 (flonum->fixnum (/ h scale-factor)))))