image.cljs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. (ns frontend.image
  2. "Image related utility fns"
  3. (:require ["/frontend/exif" :as exif]
  4. [clojure.string :as string]
  5. [frontend.date :as date]
  6. [goog.object :as gobj]))
  7. (defn reverse?
  8. [exif-orientation]
  9. (contains? #{5 6 7 8} exif-orientation))
  10. (defn re-scale
  11. [exif-orientation width height max-width max-height]
  12. (let [[width height]
  13. (if (reverse? exif-orientation)
  14. [height width]
  15. [width height])
  16. ratio (/ width height)
  17. to-width (if (> width max-width) max-width width)
  18. to-height (if (> height max-height) max-height height)
  19. new-ratio (/ to-width to-height)
  20. [w h] (cond
  21. (> new-ratio ratio)
  22. [(* ratio to-height) to-height]
  23. (< new-ratio ratio)
  24. [to-width (/ to-width ratio)]
  25. :else
  26. [to-width to-height])]
  27. [(int w) (int h)]))
  28. (defn fix-orientation
  29. "Given image and exif orientation, ensure the photo is displayed
  30. rightside up"
  31. [img exif-orientation cb max-width max-height]
  32. (let [off-canvas (js/document.createElement "canvas")
  33. ctx ^js (.getContext off-canvas "2d")
  34. width (gobj/get img "width")
  35. height (gobj/get img "height")
  36. [to-width to-height] (re-scale exif-orientation width height max-width max-height)]
  37. (gobj/set ctx "imageSmoothingEnabled" false)
  38. (set! (.-width off-canvas) to-width)
  39. (set! (.-height off-canvas) to-height)
  40. ;; rotate
  41. (let [[width height] (if (reverse? exif-orientation)
  42. [to-height to-width]
  43. [to-width to-height])]
  44. (case exif-orientation
  45. 2 (.transform ctx -1 0 0 1 width 0)
  46. 3 (.transform ctx -1 0 0 -1 width height)
  47. 4 (.transform ctx 1 0 0 -1 0 height)
  48. 5 (.transform ctx 0 1 1 0 0 0)
  49. 6 (.transform ctx 0 1 -1 0 height 0)
  50. 7 (.transform ctx 0 -1 -1 0 height width)
  51. 8 (.transform ctx 0 -1 1 0 0 width)
  52. (.transform ctx 1 0 0 1 0 0))
  53. (.drawImage ctx img 0 0 width height))
  54. (cb off-canvas)))
  55. (defn get-orientation
  56. [img cb max-width max-height]
  57. (exif/getEXIFOrientation
  58. img
  59. (fn [orientation]
  60. (fix-orientation img orientation cb max-width max-height))))
  61. (defn create-object-url
  62. [file]
  63. (.createObjectURL (or (.-URL js/window)
  64. (.-webkitURL js/window))
  65. file))
  66. ;; (defn build-image
  67. ;; []
  68. ;; (let [img (js/Image.)]
  69. ;; ))
  70. (defn upload
  71. [files file-handler & {:keys [files-limit]
  72. :or {files-limit 1}}]
  73. (doseq [file (take files-limit (array-seq files))]
  74. (let [file-type (gobj/get file "type")
  75. ymd (->> (vals (date/year-month-day-padded))
  76. (string/join "_"))
  77. file-name (str ymd "_" (gobj/get file "name"))]
  78. (when (= 0 (.indexOf file-type "image/"))
  79. (file-handler file file-name file-type)
  80. ;; (let [img (js/Image.)]
  81. ;; (set! (.-onload img)
  82. ;; (fn []
  83. ;; (get-orientation img
  84. ;; (fn [^js off-canvas]
  85. ;; (let [file-form-data ^js (js/FormData.)
  86. ;; data-url (.toDataURL off-canvas)
  87. ;; blob (blob/blob data-url)]
  88. ;; (.append file-form-data "file" blob)
  89. ;; (file-cb file file-form-data file-name file-type)))
  90. ;; max-width
  91. ;; max-height)))
  92. ;; (set! (.-src img)
  93. ;; (create-object-url file)))
  94. ))))