From 9f5d942c65e713661218cefecad56747c6525b5d Mon Sep 17 00:00:00 2001 From: Felix Breidenstein Date: Thu, 5 Aug 2021 16:50:39 +0200 Subject: [PATCH] Add fontSize as option --- README.md | 36 +++++++++++++++++++++++------------- main.go | 9 +++++---- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index f8f3da3..9f4c694 100644 --- a/README.md +++ b/README.md @@ -1,44 +1,54 @@ Get customized placeholder images for your mockups and website designs :rocket: ## Features - * Free - * Open Source - * Fast (delivered via a CDN) - + +- Free +- Open Source +- Fast (delivered via a CDN) + ## Usage + The base URL is: `https://nocontent.cloud/img` To customize the image you will get, prodive one or more of the following options as query string parameters: -|name|description||value| -|---|---|---|---| -|x|width of image|optional (default: 200)|number (in pixels, max-width: 4000)| -|y|height of image|optional (default: 200)|number (in pixels, max-height: 4000)| -|bg|background color|optional (default: ffffff)|hex-value (3 or 6 character)| -|fg|text color|optional (default: 333333)|hex-value (3 or 6 character)| -|label|text on the image|optional (default: 'height' x 'width') | string (max-length: 20) +| name | description | | value | +| -------- | ----------------- | -------------------------------------- | ------------------------------------ | +| x | width of image | optional (default: 200) | number (in pixels, max-width: 4000) | +| y | height of image | optional (default: 200) | number (in pixels, max-height: 4000) | +| bg | background color | optional (default: ffffff) | hex-value (3 or 6 character) | +| fg | text color | optional (default: 333333) | hex-value (3 or 6 character) | +| label | text on the image | optional (default: 'height' x 'width') | string (max-length: 20) | +| fontSize | size of the label | optional (default: 30) | number | ## Example #1 + Without any parameters you will get a small white image with black text and 200x200 px + ``` https://nocontent.cloud/img ``` + ![200x200](https://nocontent.cloud/img) ## Example #2 + To get a purple image with the dimensions 600x400, query this address: + ``` https://nocontent.cloud/img?x=600&y=400&bg=980080 ``` + ![600x400](https://nocontent.cloud/img?x=600&y=400&bg=980080) ## Example #3 + Change the text color to blue and provide a custom text with this URL: + ``` https://nocontent.cloud/img?label=Hello%20World&fg=0099FF ``` -![200x200](https://nocontent.cloud/img?label=Hello%20World&fg=0099FF) - +![200x200](https://nocontent.cloud/img?label=Hello%20World&fg=0099FF) :computer: with :heart: by [codemonauts](https://codemonauts.com) diff --git a/main.go b/main.go index 8508ae8..b4c78ce 100644 --- a/main.go +++ b/main.go @@ -52,7 +52,7 @@ func parseHexColor(s string, fallback color.RGBA) color.RGBA { return c } -func addLabel(img *image.RGBA, width int, height int, label string, fg color.RGBA) { +func addLabel(img *image.RGBA, width int, height int, label string, size int, fg color.RGBA) { // Read the font data. fontBytes, err := ioutil.ReadFile("Inconsolata.ttf") if err != nil { @@ -66,13 +66,12 @@ func addLabel(img *image.RGBA, width int, height int, label string, fg color.RGB } // Draw the text. h := font.HintingFull - size := 30.0 dpi := 72.0 d := &font.Drawer{ Dst: img, Src: image.NewUniform(fg), Face: truetype.NewFace(f, &truetype.Options{ - Size: size, + Size: float64(size), DPI: dpi, Hinting: h, }), @@ -118,6 +117,8 @@ func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (events y, _ := strconv.Atoi(params["y"]) y = validatePixelDimension(y) + fontSize, _ := strconv.Atoi(params["fontSize"]) + label, _ := params["label"] if label == "" || len(label) > 20 { label = fmt.Sprintf("%d x %d", x, y) @@ -128,7 +129,7 @@ func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (events fgColor := parseHexColor(params["fg"], color.RGBA{51, 51, 51, 255}) image := createImage(x, y, bgColor) - addLabel(image, x, y, label, fgColor) + addLabel(image, x, y, label, fontSize, fgColor) buf := new(bytes.Buffer) png.Encode(buf, image)