Skip to content

Commit

Permalink
Merge pull request #43 from Pixboost/feature/42/remove-border
Browse files Browse the repository at this point in the history
Feature/42/remove border
  • Loading branch information
dooman87 committed Dec 22, 2023
2 parents 3438afa + f0d1311 commit 0440383
Show file tree
Hide file tree
Showing 20 changed files with 237 additions and 52 deletions.
37 changes: 20 additions & 17 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM dpokidov/imagemagick:7.1.1-17-bullseye AS build
FROM dpokidov/imagemagick:7.1.1-22-bullseye AS build

RUN apt-get update && apt-get upgrade -y && apt-get install -y --no-install-recommends \
g++ \
Expand All @@ -16,50 +16,53 @@ RUN apt-get update && apt-get upgrade -y && apt-get install -y --no-install-reco
#Installing golang
ENV PATH /usr/local/go/bin:$PATH

ENV GOLANG_VERSION 1.20.5
ENV GOLANG_VERSION 1.20.12

RUN set -eux; \
arch="$(dpkg --print-architecture)"; arch="${arch##*-}"; \
url=; \
case "$arch" in \
'amd64') \
url='https://dl.google.com/go/go1.20.5.linux-amd64.tar.gz'; \
sha256='d7ec48cde0d3d2be2c69203bc3e0a44de8660b9c09a6e85c4732a3f7dc442612'; \
url='https://dl.google.com/go/go1.20.12.linux-amd64.tar.gz'; \
sha256='9c5d48c54dd8b0a3b2ef91b0f92a1190aa01f11d26e98033efa64c46a30bba7b'; \
;; \
'armel') \
export GOARCH='arm' GOARM='5' GOOS='linux'; \
;; \
'armhf') \
url='https://dl.google.com/go/go1.20.5.linux-armv6l.tar.gz'; \
sha256='79d8210efd4390569912274a98dffc16eb85993cccdeef4d704e9b0dfd50743a'; \
url='https://dl.google.com/go/go1.20.12.linux-armv6l.tar.gz'; \
sha256='bf4687cbbf0c44a82311d52e2dcccb263f3d9c3b512007e1fae569e03dc0a189'; \
;; \
'arm64') \
url='https://dl.google.com/go/go1.20.5.linux-arm64.tar.gz'; \
sha256='aa2fab0a7da20213ff975fa7876a66d47b48351558d98851b87d1cfef4360d09'; \
url='https://dl.google.com/go/go1.20.12.linux-arm64.tar.gz'; \
sha256='8afe8e3fb6972eaa2179ef0a71678c67f26509fab4f0f67c4b00f4cdfa92dc87'; \
;; \
'i386') \
url='https://dl.google.com/go/go1.20.5.linux-386.tar.gz'; \
sha256='d394ac8fecf66812c78ffba7fb9a265bb1b9917564c7fd77f0edb0df6d5777a1'; \
url='https://dl.google.com/go/go1.20.12.linux-386.tar.gz'; \
sha256='77db17c6350448b0c3afa5c4248426d8a445b5e8ac2411fff57463b47f8d7f80'; \
;; \
'mips64el') \
export GOARCH='mips64le' GOOS='linux'; \
;; \
'ppc64el') \
url='https://dl.google.com/go/go1.20.5.linux-ppc64le.tar.gz'; \
sha256='049b8ab07d34077b90c0642138e10207f6db14bdd1743ea994a21e228f8ca53d'; \
url='https://dl.google.com/go/go1.20.12.linux-ppc64le.tar.gz'; \
sha256='2ae0ec3736216dfbd7b01ff679842dc1bed365e53a024d522645bcffd01c7328'; \
;; \
'riscv64') \
export GOARCH='riscv64' GOOS='linux'; \
;; \
's390x') \
url='https://dl.google.com/go/go1.20.5.linux-s390x.tar.gz'; \
sha256='bac14667f1217ccce1d2ef4e204687fe6191e6dc19a8870cfb81a41f78b04e48'; \
url='https://dl.google.com/go/go1.20.12.linux-s390x.tar.gz'; \
sha256='ee48b23e1978a866cb60a8e8ddf0bd61cbbaf86bcfcdbf4f9509f34e9159ce45'; \
;; \
*) echo >&2 "error: unsupported architecture '$arch' (likely packaging update needed)"; exit 1 ;; \
esac; \
build=; \
if [ -z "$url" ]; then \
# https://github.com/golang/go/issues/38536#issuecomment-616897960
build=1; \
url='https://dl.google.com/go/go1.20.5.src.tar.gz'; \
sha256='9a15c133ba2cfafe79652f4815b62e7cfc267f68df1b9454c6ab2a3ca8b96a88'; \
url='https://dl.google.com/go/go1.20.12.src.tar.gz'; \
sha256='c5bf934751d31c315c1d0bb5fb02296545fa6d08923566f7a5afec81f2ed27d6'; \
echo >&2; \
echo >&2 "warning: current architecture ($arch) does not have a compatible Go binary release; will be building from source"; \
echo >&2; \
Expand Down Expand Up @@ -134,7 +137,7 @@ WORKDIR /go/src/github.com/Pixboost/transformimgs/cmd

RUN go build -o /transformimgs

FROM dpokidov/imagemagick:7.1.1-17-bullseye
FROM dpokidov/imagemagick:7.1.1-22-bullseye

ENV IM_HOME /usr/local/bin

Expand Down
35 changes: 19 additions & 16 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM dpokidov/imagemagick:7.1.1-17-bullseye
FROM dpokidov/imagemagick:7.1.1-22-bullseye

RUN apt-get update && apt-get upgrade -y && apt-get install -y --no-install-recommends \
g++ \
Expand All @@ -16,50 +16,53 @@ RUN apt-get update && apt-get upgrade -y && apt-get install -y --no-install-reco
#Installing golang
ENV PATH /usr/local/go/bin:$PATH

ENV GOLANG_VERSION 1.20.5
ENV GOLANG_VERSION 1.20.12

RUN set -eux; \
arch="$(dpkg --print-architecture)"; arch="${arch##*-}"; \
url=; \
case "$arch" in \
'amd64') \
url='https://dl.google.com/go/go1.20.5.linux-amd64.tar.gz'; \
sha256='d7ec48cde0d3d2be2c69203bc3e0a44de8660b9c09a6e85c4732a3f7dc442612'; \
url='https://dl.google.com/go/go1.20.12.linux-amd64.tar.gz'; \
sha256='9c5d48c54dd8b0a3b2ef91b0f92a1190aa01f11d26e98033efa64c46a30bba7b'; \
;; \
'armel') \
export GOARCH='arm' GOARM='5' GOOS='linux'; \
;; \
'armhf') \
url='https://dl.google.com/go/go1.20.5.linux-armv6l.tar.gz'; \
sha256='79d8210efd4390569912274a98dffc16eb85993cccdeef4d704e9b0dfd50743a'; \
url='https://dl.google.com/go/go1.20.12.linux-armv6l.tar.gz'; \
sha256='bf4687cbbf0c44a82311d52e2dcccb263f3d9c3b512007e1fae569e03dc0a189'; \
;; \
'arm64') \
url='https://dl.google.com/go/go1.20.5.linux-arm64.tar.gz'; \
sha256='aa2fab0a7da20213ff975fa7876a66d47b48351558d98851b87d1cfef4360d09'; \
url='https://dl.google.com/go/go1.20.12.linux-arm64.tar.gz'; \
sha256='8afe8e3fb6972eaa2179ef0a71678c67f26509fab4f0f67c4b00f4cdfa92dc87'; \
;; \
'i386') \
url='https://dl.google.com/go/go1.20.5.linux-386.tar.gz'; \
sha256='d394ac8fecf66812c78ffba7fb9a265bb1b9917564c7fd77f0edb0df6d5777a1'; \
url='https://dl.google.com/go/go1.20.12.linux-386.tar.gz'; \
sha256='77db17c6350448b0c3afa5c4248426d8a445b5e8ac2411fff57463b47f8d7f80'; \
;; \
'mips64el') \
export GOARCH='mips64le' GOOS='linux'; \
;; \
'ppc64el') \
url='https://dl.google.com/go/go1.20.5.linux-ppc64le.tar.gz'; \
sha256='049b8ab07d34077b90c0642138e10207f6db14bdd1743ea994a21e228f8ca53d'; \
url='https://dl.google.com/go/go1.20.12.linux-ppc64le.tar.gz'; \
sha256='2ae0ec3736216dfbd7b01ff679842dc1bed365e53a024d522645bcffd01c7328'; \
;; \
'riscv64') \
export GOARCH='riscv64' GOOS='linux'; \
;; \
's390x') \
url='https://dl.google.com/go/go1.20.5.linux-s390x.tar.gz'; \
sha256='bac14667f1217ccce1d2ef4e204687fe6191e6dc19a8870cfb81a41f78b04e48'; \
url='https://dl.google.com/go/go1.20.12.linux-s390x.tar.gz'; \
sha256='ee48b23e1978a866cb60a8e8ddf0bd61cbbaf86bcfcdbf4f9509f34e9159ce45'; \
;; \
*) echo >&2 "error: unsupported architecture '$arch' (likely packaging update needed)"; exit 1 ;; \
esac; \
build=; \
if [ -z "$url" ]; then \
# https://github.com/golang/go/issues/38536#issuecomment-616897960
build=1; \
url='https://dl.google.com/go/go1.20.5.src.tar.gz'; \
sha256='9a15c133ba2cfafe79652f4815b62e7cfc267f68df1b9454c6ab2a3ca8b96a88'; \
url='https://dl.google.com/go/go1.20.12.src.tar.gz'; \
sha256='c5bf934751d31c315c1d0bb5fb02296545fa6d08923566f7a5afec81f2ed27d6'; \
echo >&2; \
echo >&2 "warning: current architecture ($arch) does not have a compatible Go binary release; will be building from source"; \
echo >&2; \
Expand Down
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,8 @@ $ jmeter -n -t perf-test-jxl.jmx -l ./results-jxl.jmx -e -o ./results-jxl
## Opened tickets for images related features

* [Safari to support Save-Data](https://bugs.webkit.org/show_bug.cgi?id=199101)
* [Safari to support AVIF](https://bugs.webkit.org/show_bug.cgi?id=207750)
* [Firefox to support JPEG XL](https://bugzilla.mozilla.org/show_bug.cgi?id=1539075)
* [Chrome to support JPEG XL](https://bugs.chromium.org/p/chromium/issues/detail?id=1178058)
* [Safari to support JPEG XL](https://bugs.webkit.org/show_bug.cgi?id=208235)
* Safari to support native lazy loading
* [Implementation](https://bugs.webkit.org/show_bug.cgi?id=196698)
* [Enabled by default](https://bugs.webkit.org/show_bug.cgi?id=208094)
* [Auto sizes for lazy loaded img in Firefox](https://bugzilla.mozilla.org/show_bug.cgi?id=1816615)
* [Auto sizes for lazy loaded img in Safari](https://bugs.webkit.org/show_bug.cgi?id=253143)

## Contribute

Expand Down
11 changes: 7 additions & 4 deletions img/processor/imagemagick.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (p *ImageMagick) Resize(config *img.TransformationConfig) (*img.Image, erro

args := make([]string, 0)
args = append(args, "-") //Input
args = append(args, getBeforeTransformConvertFormatOptions(source, mimeType)...)
args = append(args, getBeforeTransformConvertFormatOptions(config, source, mimeType)...)
args = append(args, beforeResizeConvertOpts...)
args = append(args, "-resize", targetSize)
args = append(args, getQualityOptions(source, config, mimeType)...)
Expand Down Expand Up @@ -199,7 +199,7 @@ func (p *ImageMagick) FitToSize(config *img.TransformationConfig) (*img.Image, e

args := make([]string, 0)
args = append(args, "-") //Input
args = append(args, getBeforeTransformConvertFormatOptions(source, mimeType)...)
args = append(args, getBeforeTransformConvertFormatOptions(config, source, mimeType)...)
args = append(args, beforeResizeConvertOpts...)
args = append(args, "-resize", targetSize+"^")

Expand Down Expand Up @@ -241,7 +241,7 @@ func (p *ImageMagick) Optimise(config *img.TransformationConfig) (*img.Image, er

args := make([]string, 0)
args = append(args, "-") //Input
args = append(args, getBeforeTransformConvertFormatOptions(source, mimeType)...)
args = append(args, getBeforeTransformConvertFormatOptions(config, source, mimeType)...)
args = append(args, beforeResizeConvertOpts...)
args = append(args, getQualityOptions(source, config, mimeType)...)
args = append(args, p.AdditionalArgs...)
Expand Down Expand Up @@ -495,12 +495,15 @@ func getConvertFormatOptions(source *img.Info) []string {
return opts
}

func getBeforeTransformConvertFormatOptions(source *img.Info, outputMimeType string) []string {
func getBeforeTransformConvertFormatOptions(config *img.TransformationConfig, source *img.Info, outputMimeType string) []string {
var opts []string

if outputMimeType == "image/webp" && source.Format == "GIF" {
opts = append(opts, "-coalesce")
}
if config.TrimBorder {
opts = append(opts, "-trim")
}

return opts
}
Expand Down
105 changes: 105 additions & 0 deletions img/processor/imagemagick_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package processor_test

import (
"bytes"
"fmt"
"github.com/Pixboost/transformimgs/v8/img"
"github.com/Pixboost/transformimgs/v8/img/processor"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"reflect"
"strconv"
"testing"
)

Expand Down Expand Up @@ -548,6 +552,107 @@ func TestImageMagick_IsIllustration(t *testing.T) {
}
}

var trimBorderTestFiles = []string{"logo-1.png", "logo-2.png", "no-border.jpg"}

func TestImageMagick_TrimBorder(t *testing.T) {
var overrideExpected = false

for _, tt := range trimBorderTestFiles {
f := fmt.Sprintf("%s/%s", "./test_files/trim-border", tt)

orig, err := ioutil.ReadFile(f)
if err != nil {
t.Errorf("can't read file %s: %+v", f, err)
}

resultImage, err := proc.Optimise(&img.TransformationConfig{
Src: &img.Image{
Id: "img",
Data: orig,
},
TrimBorder: true,
})

if err != nil {
t.Errorf("couldn't optimise image %s", tt)
}

expectedFile := fmt.Sprintf("./test_files/trim-border/expected/optimise/%s", tt)
compareImage(resultImage, expectedFile, t, overrideExpected)

resultImage, err = proc.Resize(&img.TransformationConfig{
Src: &img.Image{
Id: "img",
Data: orig,
},
TrimBorder: true,
Config: &img.ResizeConfig{Size: "300"},
})

if err != nil {
t.Errorf("couldn't resize image %s", tt)
}

expectedFile = fmt.Sprintf("./test_files/trim-border/expected/resize/%s", tt)
compareImage(resultImage, expectedFile, t, overrideExpected)

resultImage, err = proc.FitToSize(&img.TransformationConfig{
Src: &img.Image{
Id: "img",
Data: orig,
},
TrimBorder: true,
Config: &img.ResizeConfig{Size: "200x80"},
})

if err != nil {
t.Errorf("couldn't fit image %s", tt)
}

expectedFile = fmt.Sprintf("./test_files/trim-border/expected/fit/%s", tt)
compareImage(resultImage, expectedFile, t, overrideExpected)
}
}

func compareImage(img *img.Image, expectedFile string, t *testing.T, overrideExpected bool) {
if overrideExpected {
ioutil.WriteFile(expectedFile, img.Data, 0777)
} else {
ext := filepath.Ext(expectedFile)
actualFile, err := os.CreateTemp("", fmt.Sprintf("image*%s", ext))
if err != nil {
t.Errorf("could not create temp file %s", err)
}
_, err = actualFile.Write(img.Data)
if err != nil {
t.Errorf("could not write to temp file %s", err)
}
_ = actualFile.Close()

var out, cmderr bytes.Buffer
cmd := exec.Command(os.ExpandEnv("${IM_HOME}/magick"))
cmd.Args = append(cmd.Args, "compare", "-metric", "AE", actualFile.Name(), expectedFile, "null:")
cmd.Stdout = &out
cmd.Stderr = &cmderr

fmt.Println(cmd.Args)

err = cmd.Run()
errStr := cmderr.String()
outStr := out.String()
if err != nil {
t.Errorf("error executing compare command: %s, %s", err.Error(), errStr)
}
pixelsDiffCnt, err := strconv.Atoi(errStr)
if err != nil {
t.Errorf("could not parse output of compare [%s]", outStr)
}
if pixelsDiffCnt > 0 {
t.Errorf("expected 0 different pixels but found %d when comparing %s", pixelsDiffCnt, expectedFile)
}
}
}

func testImages(t *testing.T, fn transform, files []*testTransformation) {
results := make([]*result, 0)
for _, tt := range files {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/processor/test_files/trim-border/logo-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/processor/test_files/trim-border/logo-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 0440383

Please sign in to comment.