70 lines
3 KiB
Clojure
70 lines
3 KiB
Clojure
(ns luduverse.images
|
|
(:require [image-resizer.core :refer :all]
|
|
[image-resizer.util :as utils]
|
|
[image-resizer.format :refer :all]
|
|
[image-resizer.crop :refer :all]
|
|
[clojure.java.io :as io]
|
|
[noir.io :as noir-io]
|
|
[clj-http.client :as http]
|
|
[me.raynes.fs :as fs]))
|
|
|
|
;; NOTE: Parts of this file is grabbed from the open source lib
|
|
;; https://github.com/arg-games/ldview
|
|
|
|
(defn base-path
|
|
;([] (str (noir-io/resource-path) "img"))
|
|
([] "resources/ld-images")
|
|
([competition-id] (str (base-path) "/ld" competition-id "/")))
|
|
|
|
(defn create-file-structure [competition-id]
|
|
(let [path (base-path competition-id)]
|
|
(fs/mkdirs (str path "/thumbs/"))
|
|
(fs/mkdirs (str path "/fullscreen/"))
|
|
(fs/mkdirs (str path "/raw/"))))
|
|
|
|
(defn image-name [competition-id folder entry-id number]
|
|
;; FIXME: JPEGImageWriter is not thread save? Whats wrong here?
|
|
;; IIOException Invalid argument to native writeImage com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeImage (JPEGImageWriter.java:-2)
|
|
(str (base-path competition-id) folder "/" entry-id "_" number ".png"))
|
|
|
|
(defn to-square [file new-size]
|
|
(let [[width height] (dimensions (utils/buffered-image file))]
|
|
(if (< width height)
|
|
(let [resized-img (resize-to-width file new-size)
|
|
[w h] (dimensions resized-img)
|
|
crop-margin (quot (- h new-size) 2)]
|
|
(crop-from resized-img 0 crop-margin new-size new-size))
|
|
(let [resized-img (resize-to-height file new-size)
|
|
[w h] (dimensions resized-img)
|
|
crop-margin (quot (- w new-size) 2)]
|
|
(crop-from resized-img crop-margin 0 new-size new-size)))))
|
|
|
|
(defn sourceimage->thumb [image-path new-image-path]
|
|
(with-redefs [image-resizer.fs/new-filename (fn [filepath dimensions] (str filepath))]
|
|
(as-file (to-square (io/file image-path) 200) new-image-path)))
|
|
|
|
(defn sourceimage->fullscreen [image-path new-image-path]
|
|
(with-redefs [image-resizer.fs/new-filename (fn [filepath dimensions] (str filepath))]
|
|
(as-file (resize (io/file image-path) 800 600) new-image-path)))
|
|
|
|
(defn save-image-from-url [url target-file]
|
|
(with-open [bodystream (:body (http/get url {:as :stream}))]
|
|
(io/copy bodystream (io/file target-file))))
|
|
|
|
(defn save-images-for-entry [competition-id entry]
|
|
(create-file-structure competition-id)
|
|
(doseq [image-url (:images entry)]
|
|
(let [id (:ld_uid entry)
|
|
number (last (first (re-seq #"shot([0-9]+)" image-url)))
|
|
raw-image-path (image-name competition-id "raw" id number)]
|
|
(if-not (fs/exists? raw-image-path)
|
|
(try
|
|
(do
|
|
(save-image-from-url image-url raw-image-path)
|
|
(sourceimage->fullscreen raw-image-path (image-name competition-id "fullscreen" id number))
|
|
(sourceimage->thumb raw-image-path (image-name competition-id "thumbs" id number)))
|
|
(catch Exception e
|
|
(do
|
|
(print (str competition-id ": " id))
|
|
(print (.getMessage e)))))))))
|