From 199867225860371f2f06ffc5f35a13bb227d89fe Mon Sep 17 00:00:00 2001 From: gered Date: Thu, 3 Apr 2014 22:13:10 -0400 Subject: [PATCH] add initial routes / views for animated gif support (not animating yet) --- resources/views/home.html | 9 +++++++ resources/views/methods/gif.html | 44 ++++++++++++++++++++++++++++++++ src/toascii/models/image.clj | 31 ++++++++++++++++++---- src/toascii/routes/api/image.clj | 30 ++++++++++++++++++++-- 4 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 resources/views/methods/gif.html diff --git a/resources/views/home.html b/resources/views/home.html index 14b1126..eda1980 100644 --- a/resources/views/home.html +++ b/resources/views/home.html @@ -32,6 +32,15 @@ +
+
+

Animated GIF   –>   ASCII

+
+
+ {% include 'methods/gif.html' %} +
+
+ {% endblock %} \ No newline at end of file diff --git a/resources/views/methods/gif.html b/resources/views/methods/gif.html new file mode 100644 index 0000000..e6c1faf --- /dev/null +++ b/resources/views/methods/gif.html @@ -0,0 +1,44 @@ +
+
+ +
+ +
+ +
+
+ +
+ +
+
+ +
+
+
+ +
+ +
+ +
+
+ +
+
+ +
+
+ +
+
+ + + + + \ No newline at end of file diff --git a/src/toascii/models/image.clj b/src/toascii/models/image.clj index cb23ab8..1c1e744 100644 --- a/src/toascii/models/image.clj +++ b/src/toascii/models/image.clj @@ -1,15 +1,26 @@ (ns toascii.models.image - (:import (java.awt.image BufferedImage)) - (:require [toascii.util :refer [query-param-url->java-url]] - [clj-image2ascii.core :as i2a]) + (:import (java.awt.image BufferedImage) + (javax.imageio.stream ImageInputStream)) + (:require [clojure.string :as str] + [clj-image2ascii.core :as i2a] + [toascii.util :refer [query-param-url->java-url]]) (:use hiccup.core)) +(def ascii-pre-css "font-size:6pt; letter-spacing:1px; line-height:5pt; font-weight:bold;") + (defn get-image [^String url] (let [java-url (query-param-url->java-url url)] (i2a/get-image-by-url java-url))) -(defn wrap-pre-tag [s] - (str "
" s "
")) +(defn get-image-stream [^String url] + (let [java-url (query-param-url->java-url url)] + (i2a/get-image-stream-by-url java-url))) + +(defn wrap-pre-tag + ([s] + (str "
" s "
")) + ([s delay] + (str "
" s "
"))) (defn image->ascii [^BufferedImage image scale-to-width color? html?] (let [converted (i2a/convert-image image scale-to-width color?) @@ -17,3 +28,13 @@ (if html? (wrap-pre-tag ascii) ascii))) + +(defn gif->ascii [^ImageInputStream image-stream scale-to-width color?] + (as-> image-stream x + (i2a/convert-animated-gif-frames x scale-to-width color?) + (:frames x) + (map + (fn [frame] + (wrap-pre-tag (:image frame) (:delay frame))) + x) + (str/join x))) \ No newline at end of file diff --git a/src/toascii/routes/api/image.clj b/src/toascii/routes/api/image.clj index c984cda..8e9de62 100644 --- a/src/toascii/routes/api/image.clj +++ b/src/toascii/routes/api/image.clj @@ -4,7 +4,7 @@ [liberator.core :refer [defresource]] [compojure.core :refer [ANY]] [toascii.route-utils :refer [register-routes]] - [toascii.models.image :refer [image->ascii get-image]] + [toascii.models.image :refer [image->ascii gif->ascii get-image get-image-stream]] [toascii.util :refer [parse-int parse-boolean]])) (defresource render-image [{:keys [url width color format] :as params}] @@ -42,5 +42,31 @@ (fn [ctx] (:error ctx))) +(defresource render-animated-image [{:keys [url width color] :as params}] + :available-media-types ["text/html"] + :malformed? + (fn [ctx] + (cond + (str/blank? url) {:error "Missing image url"} + (and width + (nil? (parse-int width))) {:error "Invalid image width."})) + :exists? + (fn [_] + (if-let [stream (get-image-stream url)] + {:image stream} + [false {:error "Image could not be loaded."}])) + :handle-ok + (fn [ctx] + (let [color? (or (nil? color) + (parse-boolean color))] + (gif->ascii (:image ctx) (parse-int width) color?))) + :handle-malformed + (fn [ctx] + (:error ctx)) + :handle-not-found + (fn [ctx] + (:error ctx))) + (register-routes api-image-routes - (ANY "/api/image" {params :params} (render-image params))) \ No newline at end of file + (ANY "/api/image" {params :params} (render-image params)) + (ANY "/api/animated" {params :params} (render-animated-image params))) \ No newline at end of file