adding some niceties for images

pull/8/head
Stephen Searles 8 years ago
parent 9591120da9
commit 5d78da0f30
  1. 17
      media.go

@ -29,16 +29,16 @@ type Media struct {
Name string
}
func (ms *MediaSource) ThumbMax(m Media, maxDim int) (string, error) {
func (ms *MediaSource) ThumbMax(m Media, maxDim int) (string, int, int, error) {
f, err := os.Open(ms.LocationOrig(m))
if err != nil {
return "", err
return "", 0, 0, err
}
defer f.Close()
img, _, err := image.Decode(f)
if err != nil {
return "", err
return "", 0, 0, err
}
rect := img.Bounds()
@ -53,7 +53,8 @@ func (ms *MediaSource) ThumbMax(m Media, maxDim int) (string, error) {
height = maxDim
}
return ms.ThumbImage(img, m, width, height)
src, err := ms.ThumbImage(img, m, width, height)
return src, width, height, err
}
func (ms *MediaSource) ByName(name string) *Media {
@ -128,7 +129,7 @@ func (ch *CaddyHugo) serveMediaPage(w http.ResponseWriter, r *http.Request) (int
}
io.WriteString(w, `<html>
<head><style>img { float: left; }</style></head>
<head><style>.img { float: left; text-align: center; }</style></head>
<body>
`)
if ch.Media != nil {
@ -139,12 +140,12 @@ func (ch *CaddyHugo) serveMediaPage(w http.ResponseWriter, r *http.Request) (int
}
for _, m := range media {
src, err := ch.Media.ThumbMax(*m, 100)
src, width, height, err := ch.Media.ThumbMax(*m, 100)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return 500, nil
}
fmt.Fprintf(w, "<img src=%q />\n", src)
fmt.Fprintf(w, `<div class="img"><img width=%d height=%d src=%q /><br /><input type="text" disabled value=%q /></div>`, width, height, src, src)
}
}
io.WriteString(w, `</body><html>`)
@ -174,7 +175,7 @@ func (ch *CaddyHugo) serveMedia(w http.ResponseWriter, r *http.Request) (int, er
return 400, nil
}
file, err = ch.Media.ThumbMax(*m, max)
file, _, _, err = ch.Media.ThumbMax(*m, max)
case 5:
var width, height int
width, err = strconv.Atoi(segs[3])

Loading…
Cancel
Save