Skip to content

Instantly share code, notes, and snippets.

@remyrd
Forked from oliyh/image-url-to-data-uri.js
Last active October 19, 2021 15:14
Show Gist options
  • Save remyrd/0612779748c4cdb0e9fee68698d9691a to your computer and use it in GitHub Desktop.
Save remyrd/0612779748c4cdb0e9fee68698d9691a to your computer and use it in GitHub Desktop.
Convert an image url to a data URI without canvas
(require '[ajax.protocols :as pr])
;; effecting with re-frame-http-fx
{:http-xhrio {,,,
:response-format {:content-type "image/png"
:description "PNG image"
:read pr/-body
:type :arraybuffer}
:on-success [::my-handler]}
(rf/reg-event-db
::my-handler
(fn [db [_ response]]
(assoc db :thing (->> response
(js/Uint8Array.)
(js/String.fromCharCode.apply nil)
(js/btoa)
(str "data:image/png;base64,")))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment