Skalar is a humble attempt to build a fast GraphicsMagick based image tranformation library. There are already plenty of this kind of libraries all over the world and each one is special (at least in their authors' minds ;)
So, why Skalar?
Skalar achieves its robustness via underlaying pool of opened connections to GM sessions. Instead of creating new session with gm executable each time a request comes in, Skalar redirects request to first least-busy GM session (or more technically - the session with smallest number of assigned requests). Once the number of assigned request on each opened sessions exceeds given capacity, Skalar adds one more session trying to balance incoming requests.
(require '[clojure.java.io :as io])
(require '[skalar.core :as sk])
(require '[skalar.gm :as gm])
(def input-file (io/file "photo.jpg"))
(sk/convert input-file
:output "photo.png"
:processing [(gm/crop 10 10 100 100)
(gm/resize "200x200" :exact :no-profiles)
(gm/options {:auto-orient true})])
where :processing
is a vector of convert options. Two of them (-crop
and -resize
) got a little helpers in
skalar.gm
namespace to save people from remembering crazy GM syntax. All other options can be provided via skalar.gm/options
function in keywordized form (without
prefixing -), so for example {:auto-orient true}
gets translated to -auto-orient
.
So far default GraphicsMagick pool of processes (sessions) was used, but sometimes it's better to create a customized one. Each pool is defined by 4 parameters:
- base : is a number of minimum sessions that need to be kept opened and ready for incoming requests.
- capacity : is a number of requests each session can handle. Once the number of session exceeds capacity a new session is being added.
- max : maximum number of sessions that can be created.
Default pool is defined with base=3
, capacity=6
, max=8
, and that simply translates to:
keep 3 opened sessions by default, consequently add new ones if number of requests for each existing session exceeds 6, but do not create more than 8 sessions in total
Each additionally created session will be immediately closed and removed from pool when number of request drops down to 0 at some point in time.
Let's leave theory behind and create a custom pool:
(require '[skalar.pool :as sp])
(def my-pool
(sp/create-pool 2 3 4))
and provide it via :pool
option:
(sk/convert (sk/file-from-url "https://images-assets.nasa.gov/image/PIA20912/PIA20912~orig.jpg")
:output "kręcioł.png"
:processing [(gm/resize "200x200")]
:pool my-pool)
As mentioned Skalar bases on gm executable which must be already installed to get this library running correctly. To install gm on Mac, simply use brew:
brew install gm
GraphicsMagick is also available on Alpine Linux, so docker is your friend if you don't want to pollute your OS.