add initial routes / views for animated gif support (not animating yet)

This commit is contained in:
Gered 2014-04-03 22:13:10 -04:00
parent 4aa70fdb40
commit 1998672258
4 changed files with 107 additions and 7 deletions

View file

@ -32,6 +32,15 @@
</div>
</div>
<div class="panel panel-info">
<div class="panel-heading">
<h4 class="panel-title">Animated GIF &nbsp; &ndash;&gt; &nbsp; ASCII</h4>
</div>
<div class="panel-body">
{% include 'methods/gif.html' %}
</div>
</div>
</div>
{% endblock %}

View file

@ -0,0 +1,44 @@
<form class="form-horizontal api-form" role="form" id="gifForm" data-api-endpoint="/animated">
<fieldset>
<div class="form-group">
<label for="gifUrl" class="col-sm-2 control-label">GIF URL</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="gifUrl" placeholder="http://url/to/image" data-fieldname="url" />
</div>
</div>
<div class="form-group">
<label for="gifMonochrome" class="col-sm-2 control-label">Color</label>
<div class="col-sm-3">
<div class="checkbox">
<label>
<input type="checkbox" id="gifMonochrome" data-fieldname="color" value="false" />
Black &amp; White
</label>
</div>
</div>
</div>
<div class="form-group">
<label for="gifWidth" class="col-sm-2 control-label">Scale to Width</label>
<div class="col-sm-3">
<input type="text" class="form-control" id="gifWidth" placeholder="Width (in pixels) to scale to" data-fieldname="width" />
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">To ASCII</button>
</div>
</div>
</fieldset>
</form>
<div class="methodCallDisplay well well-sm" style="display: none;">
<strong>API request:</strong> &nbsp; <a class="url text-muted"></a>
</div>
<pre class="textOutputContainer" style="display: none;"></pre>
<div class="htmlOutputContainer" style="display: none;"></div>
<div class="methodErrorContainer alert alert-danger" style="display: none;"></div>

View file

@ -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 "<pre style=\"font-size:6pt; letter-spacing:1px; line-height:5pt; font-weight:bold;\">" s "</pre>"))
(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 "<pre style=\"" ascii-pre-css "\">" s "</pre>"))
([s delay]
(str "<pre style=\"" ascii-pre-css "\" data-delay=\"" delay "\">" s "</pre>")))
(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)))

View file

@ -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)))
(ANY "/api/image" {params :params} (render-image params))
(ANY "/api/animated" {params :params} (render-animated-image params)))