[cl-gd-devel] with-resized-image macro

Ryszard Szopa ryszard.szopa at gmail.com
Mon Apr 28 12:23:41 UTC 2008


Hi Edi,

I was needing to resize images quite often, so I rolled a
`with-resized-image' macro:

(defmacro with-resized-image ((name x &key y (image '*default-image*
img-provided)) &body body)
  "Rescale `image' so that its width is equal `x' pixels. If `y' is
provided, make its height equal `y' pixels. If `y' isn't provided, try
to keep the proportions of the image right."
  (let ((%image (gensym "image"))
	(%new-y (gensym "new-y"))
	(%y (gensym "y")))
    `(let* ((,%image ,image)
	    (,%y ,y))
       (multiple-value-bind (old-x old-y)
	   (image-size ,%image)
	 (let* ((new-to-old-proportion (/ ,x old-x))
		(,%new-y (or ,%y (round (* old-y new-to-old-proportion)))))
	   (with-image (,name ,x ,%new-y t)
	     (copy-image ,%image ,name 0 0 0 0 old-x old-y :resize t
:dest-width ,x :dest-height  ,%new-y)
	     , at body))))))

(defmacro with-resized-image* ((x &key y image) &body body)
  `(with-resized-image (*default-image* ,x
					 :y ,y
					 ,@(when image (list :image image)))
     , at body))

Do you think it does what is supposed to do properly? I mean, I know
it *does* resize images, but I am not so sure it does it in the
*right* way (right relative to cl-gd). :)
Anyway, if you were interested in including this into cl-gd, I can
provide a proper patch with unit tests and such stuff.

Cheers,

    -- Richard
-- 
http://szopa.tasak.gda.pl/



More information about the Cl-gd-devel mailing list