Skip to content

Commit 57b453f

Browse files
committed
Include pristine image if suitable
1 parent 4b896cd commit 57b453f

3 files changed

Lines changed: 40 additions & 32 deletions

File tree

771 Bytes
Loading

frog/enhance-body.rkt

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
;; Don't resize remote images. Or should we fetch it and resize it?
7171
'((div ((class "figure")) (img ((src "//somehost.com/img/file.jpg"))))))
7272
(when magick-available?
73-
(test-equal? "Big image"
73+
(test-equal? "Image bigger than maximum size"
7474
(responsive-images
7575
'((div ((class "figure"))
7676
(img ((src "/img/1300px-image.gif") (alt "")))
@@ -80,46 +80,50 @@
8080
(current-image-default-size)))
8181
(srcset
8282
,(string-join (map (λ (s)
83-
(format "/img/resized/1300px-image-~a.gif ~aw" s s))
84-
(current-image-sizes))
85-
", "))
83+
(format "/img/resized/1300px-image-~a.gif ~aw" s s))
84+
(current-image-sizes))
85+
", "))
8686
(sizes "(max-width: 1300px) 100vw, 1300px")
8787
(alt "")))
8888
(p ((class "caption")) "some text"))))
89-
(test-equal? "Medium image"
89+
(test-equal? "Image smaller than biggest image but bigger than smallest size"
9090
(responsive-images
9191
'((div ((class "figure"))
9292
(img ((src "/img/800px-image.gif") (alt "")))
9393
(p ((class "caption")) "some text"))))
9494
`((div ((class "figure"))
9595
(img ((src ,(format "/img/resized/800px-image-~a.gif"
9696
(current-image-default-size)))
97-
(srcset ,(string-join (map (λ (s)
98-
(format "/img/resized/800px-image-~a.gif ~aw" s s))
99-
'(320 600))
100-
", "))
97+
(srcset
98+
,(string-append
99+
(string-join
100+
(map (λ (s)
101+
(format "/img/resized/800px-image-~a.gif ~aw" s s))
102+
'(320 600))
103+
", ")
104+
", /img/800px-image.gif 800w"))
101105
(sizes "(max-width: 800px) 100vw, 800px")
102106
(alt "")))
103107
(p ((class "caption")) "some text"))))
104-
(test-equal? "Small image"
108+
(test-equal? "Image equal to a one of the sizes specified"
105109
(responsive-images
106110
'((div ((class "figure"))
107-
(img ((src "/img/480px-image.gif") (alt "")))
111+
(img ((src "/img/600px-image.gif") (alt "")))
108112
(p ((class "caption")) "some text"))))
109113
'((div ((class "figure"))
110-
(img ((src "/img/480px-image.gif")
111-
(srcset "/img/resized/480px-image-320.gif 320w")
112-
(sizes "(max-width: 480px) 100vw, 480px")
114+
(img ((src "/img/600px-image.gif")
115+
(srcset "/img/resized/600px-image-320.gif 320w, /img/600px-image.gif 600w")
116+
(sizes "(max-width: 600px) 100vw, 600px")
113117
(alt "")))
114118
(p ((class "caption")) "some text"))))
115-
(test-equal? "Tiny image"
119+
(test-equal? "Image smaller than smallest size"
116120
(responsive-images
117121
'((div ((class "figure"))
118122
(img ((src "/img/1x1.gif") (alt ""))) ; Tiny image
119123
(p ((class "caption")) "some text"))))
120124
'((div ((class "figure"))
121125
(img ((src "/img/1x1.gif")
122-
(srcset "")
126+
(srcset "/img/1x1.gif 2w")
123127
(sizes "(max-width: 2px) 100vw, 2px")
124128
(alt "")))
125129
(p ((class "caption")) "some text"))))

frog/responsive-images.rkt

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
(let ([out (make-temporary-file)])
5252
(test-eq? "resize"
5353
(begin
54-
(resize-image (build-path (www/img-path) "480px-image.gif") 10 out)
54+
(resize-image (build-path (www/img-path) "600px-image.gif") 10 out)
5555
(image-width out))
5656
10)
5757
(delete-file* out)))))
@@ -62,22 +62,26 @@
6262
(unless (directory-exists? output-dir)
6363
(make-directory output-dir))
6464
(let* ([orig-size (image-width image-path)]
65-
[sizes (filter ((curry >=) orig-size) (current-image-sizes))])
65+
[sizes (filter ((curry >) orig-size) (current-image-sizes))])
66+
;; FIXME include original image if smaller or equal than max image size!
6667
(values (cons image-path orig-size)
67-
(for/list ([width sizes])
68-
(let* ([filename (file-name-from-path image-path)]
69-
[extension (get-extension filename)]
70-
[out-path
71-
(build-path
72-
output-dir
73-
(replace-extension
74-
filename
75-
(string-append "-" (number->string width) "." extension)))])
76-
(unless (and (file-exists? out-path)
77-
(< (file-or-directory-modify-seconds image-path)
78-
(file-or-directory-modify-seconds out-path)))
79-
(resize-image image-path width out-path))
80-
(cons out-path width))))))
68+
(append (for/list ([width sizes])
69+
(let* ([filename (file-name-from-path image-path)]
70+
[extension (get-extension filename)]
71+
[out-path
72+
(build-path
73+
output-dir
74+
(replace-extension
75+
filename
76+
(string-append "-" (number->string width) "." extension)))])
77+
(unless (and (file-exists? out-path)
78+
(< (file-or-directory-modify-seconds image-path)
79+
(file-or-directory-modify-seconds out-path)))
80+
(resize-image image-path width out-path))
81+
(cons out-path width)))
82+
(if (< (length sizes) (length (current-image-sizes)))
83+
(list (cons image-path orig-size))
84+
'())))))
8185

8286
(define default-image-idx
8387
(for/or ([v (current-image-sizes)]

0 commit comments

Comments
 (0)