Serving dynamically generated images?

65 views
Skip to first unread message

blackwre

unread,
Dec 28, 2009, 1:43:11 PM12/28/09
to Compojure
Assuming that I have a clojure function that generates Images (pie
charts, diagrams etc) dynamically via BufferedImage, is there a way to
serve these image files as PNG via a route in compojure without having
to save them to disk and then serve-file them?

Sorry if this is a naive newbie question, any pointers to libraries or
sample code gratefully accepted - thanks.

Alen Ribic

unread,
Dec 28, 2009, 1:50:24 PM12/28/09
to comp...@googlegroups.com
Hope this example helps:

https://gist.github.com/01e4022f6fde08fc5f4b

-Alen

> --
>
> You received this message because you are subscribed to the Google Groups "Compojure" group.
> To post to this group, send email to comp...@googlegroups.com.
> To unsubscribe from this group, send email to compojure+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/compojure?hl=en.
>
>
>

blackwre

unread,
Dec 28, 2009, 5:49:52 PM12/28/09
to Compojure

Thanks very much!

Distilling that down gives the following handler which works perfectly
with my BufferedImage

(defn serve-image [request image]
(let [out-stream (java.io.ByteArrayOutputStream.)
in-stream (do
(javax.imageio.ImageIO/write image "png" out-stream)
(java.io.ByteArrayInputStream.
(.toByteArray out-stream)))
header {:status 200
:headers {"Content-Type" "image/png"}}]
(update-response request
header
in-stream)))

Reply all
Reply to author
Forward
0 new messages