Skip to content

Commit

Permalink
Add fontSize as option
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Breidenstein committed Aug 5, 2021
1 parent b1d922f commit 9f5d942
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
36 changes: 23 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
}),
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 9f5d942

Please sign in to comment.