Skip to content

Instantly share code, notes, and snippets.

@tenntenn
Last active February 17, 2016 13:23
Show Gist options
  • Save tenntenn/a04814add2c49398eec4 to your computer and use it in GitHub Desktop.
Save tenntenn/a04814add2c49398eec4 to your computer and use it in GitHub Desktop.
package main
import (
"image"
"image/color"
"image/draw"
"image/png"
"net/http"
"regexp"
"strconv"
)
// http://localhost:8080/100x100.png とかで100x100の画像を返す
var mockimgRegexp = regexp.MustCompile(`^.*/([0-9]+)x([0-9]+).png$`)
func mockimg(w http.ResponseWriter, r *http.Request) {
matches := mockimgRegexp.FindStringSubmatch(r.RequestURI)
width, height := int64(150), int64(150)
if len(matches) == 3 {
width, _ = strconv.ParseInt(matches[1], 10, 32)
height, _ = strconv.ParseInt(matches[2], 10, 32)
}
img := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
gray := &image.Uniform{color.RGBA{128, 128, 128, 255}}
draw.Draw(img, img.Bounds(), gray, image.ZP, draw.Src)
png.Encode(w, img)
}
func main() {
http.HandleFunc("/", mockimg)
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment