Skip to content

Commit

Permalink
Switch to libvips
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Berhent committed Sep 3, 2022
1 parent b78aba3 commit e0887ba
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 44 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ jobs:
name: Test
runs-on: ubuntu-latest
container:
image: appditto/imagemagick_go:latest
image: appditto/libvips:latest
steps:
- name: Check out code
uses: actions/checkout@master

- name: Run Tests
run: |
cd server
apt-get update && apt-get install -y gcc pkg-config
go test ./...
build_and_publish_nuxt:
Expand Down
31 changes: 22 additions & 9 deletions server/controller/natricon.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ import (
"github.com/appditto/natricon/server/color"
"github.com/appditto/natricon/server/db"
"github.com/appditto/natricon/server/image"
"github.com/appditto/natricon/server/magickwand"
"github.com/appditto/natricon/server/spc"
"github.com/appditto/natricon/server/utils"
"github.com/gin-gonic/gin"
"github.com/h2non/bimg"
)

const defaultRasterSize = 128 // Default size of PNG/WEBP images
const minConvertedSize = 100 // Minimum size of PNG/WEBP converted output
const maxConvertedSize = 1000 // Maximum size of PNG/WEBP converted output

type NatriconController struct {
Seed string
StatsChannel *chan *gin.Context
Seed string
StatsChannel *chan *gin.Context
ImageConverter *image.ImageConverter
}

// APIs
Expand Down Expand Up @@ -91,9 +92,9 @@ func (nc NatriconController) GetNano(c *gin.Context) {
}

if specialNatricon {
generateSpecialIcon(vanity, badgeType, c)
generateSpecialIcon(vanity, badgeType, c, nc.ImageConverter)
} else {
generateIcon(&sha256, badgeType, c)
generateIcon(&sha256, badgeType, c, nc.ImageConverter)
}
}

Expand Down Expand Up @@ -218,7 +219,7 @@ func (nc NatriconController) GetNatricon(c *gin.Context) {
}

// Generate natricon with given hash
func generateIcon(hash *string, badgeType spc.BadgeType, c *gin.Context) {
func generateIcon(hash *string, badgeType spc.BadgeType, c *gin.Context, imageConverter *image.ImageConverter) {
var err error

format := strings.ToLower(c.Query("format"))
Expand Down Expand Up @@ -271,7 +272,13 @@ func generateIcon(hash *string, badgeType spc.BadgeType, c *gin.Context) {
if format != "svg" {
// Convert
var converted []byte
converted, err = magickwand.ConvertSvgToBinary(svg, magickwand.ImageFormat(format), uint(size))
var bimgFormat bimg.ImageType
if format == "png" {
bimgFormat = bimg.PNG
} else {
bimgFormat = bimg.WEBP
}
converted, err = imageConverter.ConvertSvgToBinary(svg, bimgFormat, uint(size))
if err != nil {
c.String(http.StatusInternalServerError, "Error occured")
return
Expand All @@ -283,7 +290,7 @@ func generateIcon(hash *string, badgeType spc.BadgeType, c *gin.Context) {
}

// Generate icon for special accounts
func generateSpecialIcon(vanity *spc.Vanity, badgeType spc.BadgeType, c *gin.Context) {
func generateSpecialIcon(vanity *spc.Vanity, badgeType spc.BadgeType, c *gin.Context, imageConverter *image.ImageConverter) {
var err error

format := strings.ToLower(c.Query("format"))
Expand Down Expand Up @@ -326,7 +333,13 @@ func generateSpecialIcon(vanity *spc.Vanity, badgeType spc.BadgeType, c *gin.Con
if format != "svg" {
// Convert
var converted []byte
converted, err = magickwand.ConvertSvgToBinary(svg, magickwand.ImageFormat(format), uint(size))
var bimgFormat bimg.ImageType
if format == "png" {
bimgFormat = bimg.PNG
} else {
bimgFormat = bimg.WEBP
}
converted, err = imageConverter.ConvertSvgToBinary(svg, bimgFormat, uint(size))
if err != nil {
c.String(http.StatusInternalServerError, "Error occured")
return
Expand Down
1 change: 1 addition & 0 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ require (
github.com/gofrs/uuid v4.2.0+incompatible // indirect
github.com/gomodule/redigo v2.0.0+incompatible // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/h2non/bimg v1.1.9
github.com/jpillora/backoff v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ github.com/googollee/go-socket.io v1.6.2/go.mod h1:0vGP8/dXR9SZUMMD4+xxaGo/lohOw
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/h2non/bimg v1.1.9 h1:WH20Nxko9l/HFm4kZCA3Phbgu2cbHvYzxwxn9YROEGg=
github.com/h2non/bimg v1.1.9/go.mod h1:R3+UiYwkK4rQl6KVFTOFJHitgLbZXBZNFh2cv3AEbp8=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
Expand Down
35 changes: 35 additions & 0 deletions server/image/raster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package image

import (
"errors"
"sync"

"github.com/h2non/bimg"
)

type ImageFormat string

type ImageConverter struct {
mutex sync.Mutex
}

func (c *ImageConverter) ConvertSvgToBinary(svgData []byte, format bimg.ImageType, size uint) ([]byte, error) {
c.mutex.Lock()
defer c.mutex.Unlock()
inputImage := bimg.NewImage(svgData)
if inputImage == nil {
return nil, errors.New("Unable to load svg for rasterization")
}

op := bimg.Options{
Width: int(size),
Height: int(size),
Type: format,
StripMetadata: true,
}
processed, err := inputImage.Process(op)
if err != nil {
return nil, err
}
return processed, nil
}
31 changes: 0 additions & 31 deletions server/magickwand/convert.go

This file was deleted.

5 changes: 3 additions & 2 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ func main() {

// Setup natricon controller
natriconController := controller.NatriconController{
Seed: seed,
StatsChannel: &statsChan,
Seed: seed,
StatsChannel: &statsChan,
ImageConverter: &image.ImageConverter{},
}
// Setup nano controller
nanoController := controller.NanoController{
Expand Down

0 comments on commit e0887ba

Please sign in to comment.