Skip to content

Commit edf7ca6

Browse files
authored
refactor: drop nfnt/resize dependency (#6897)
* refactor: replace github.com/nfnt/resize usage with x/draw * refactor: replace nfnt/resize * fix: TestCompressToFileLimits new image size * chore: make vendor * fix: update test image files * fix: manager_test * chore: minor typo fix * refactor: replace cutter with standard library * chore: make vendor * fix: update images * test: fix TestCrop
1 parent f0db8d1 commit edf7ca6

28 files changed

+91
-2544
lines changed
87 Bytes
Loading
1 Byte
Loading
1 Byte
Loading
0 Bytes
Loading

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ require (
4040
github.com/multiformats/go-multibase v0.2.0
4141
github.com/multiformats/go-multihash v0.2.3
4242
github.com/multiformats/go-varint v0.0.7
43-
github.com/nfnt/resize v0.0.0-00010101000000-000000000000
44-
github.com/oliamb/cutter v0.2.2
4543
github.com/pborman/uuid v1.2.0
4644
github.com/pkg/errors v0.9.1
4745
github.com/prometheus/client_golang v1.20.5

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,8 +1611,6 @@ github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn
16111611
github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
16121612
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
16131613
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
1614-
github.com/oliamb/cutter v0.2.2 h1:Lfwkya0HHNU1YLnGv2hTkzHfasrSMkgv4Dn+5rmlk3k=
1615-
github.com/oliamb/cutter v0.2.2/go.mod h1:4BenG2/4GuRBDbVm/OPahDVqbrOemzpPiG5mi1iryBU=
16161614
github.com/onsi/ginkgo v0.0.0-20151202141238-7f8ab55aaf3b/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
16171615
github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
16181616
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
@@ -2076,8 +2074,6 @@ github.com/status-im/mvds v0.0.27-0.20241031073756-b192c603a75d h1:+eBm+aBGFgXsJ
20762074
github.com/status-im/mvds v0.0.27-0.20241031073756-b192c603a75d/go.mod h1:2fiAx0q9XYIPKYRq2B1oiO9zZESy/n4D32gWw6lMDsE=
20772075
github.com/status-im/notify v1.0.2-status h1:x8wev0Sh8H8KAf4bVcv+L0dVHldBESOKUlqRqRY7uL8=
20782076
github.com/status-im/notify v1.0.2-status/go.mod h1:gF3zSOrafR9DQEWSE8TjfI9NkooDxbyT4UgRGKZA0lc=
2079-
github.com/status-im/resize v0.0.0-20201215164250-7c6d9f0d3088 h1:ClCAP2FPCvl8hGMhbUx/tq/sOu2wibztAa5jAvQEe4Q=
2080-
github.com/status-im/resize v0.0.0-20201215164250-7c6d9f0d3088/go.mod h1:+92j1tN27DypDeBFxkg0uzkqfh1bNHTZe3Bv2PjvxpM=
20812077
github.com/status-im/tcp-shaker v1.1.1-status h1:TnVeeWlq2SKCWotHc4Vi6qZQfY8TTe3VLmu1xpEFYhg=
20822078
github.com/status-im/tcp-shaker v1.1.1-status/go.mod h1:RYo/itke1oU5k/6sj9DNM3QAwtE5rZSgg5JnkOv83hk=
20832079
github.com/status-im/zxcvbn-go v0.0.0-20220311183720-5e8676676857 h1:sPkzT7Z7uLmejOsBRlZ0kwDWpqjpHJsp834o5nbhqho=

images/encode.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"io"
1111
"regexp"
1212
"strings"
13-
14-
"github.com/nfnt/resize"
1513
)
1614

1715
type EncodeConfig struct {
@@ -78,12 +76,12 @@ func CompressToFileLimits(bb *bytes.Buffer, img image.Image, bounds FileSizeLimi
7876
if img.Bounds().Max.X > img.Bounds().Max.Y {
7977
// X is longer
8078
if img.Bounds().Max.X > longSideMax {
81-
img = resize.Resize(uint(longSideMax), 0, img, resize.Bilinear)
79+
img = resizeImage(img, longSideMax, 0)
8280
}
8381
} else {
8482
// Y is longer or equal
8583
if img.Bounds().Max.Y > longSideMax {
86-
img = resize.Resize(0, uint(longSideMax), img, resize.Bilinear)
84+
img = resizeImage(img, 0, longSideMax)
8785
}
8886
}
8987

@@ -97,7 +95,7 @@ func CompressToFileLimits(bb *bytes.Buffer, img image.Image, bounds FileSizeLimi
9795
return err
9896
}
9997

100-
img = ResizeTo(95, img)
98+
img = Scale(95, img)
10199
}
102100
}
103101

images/encode_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func TestCompressToFileLimits(t *testing.T) {
9898
bb := bytes.NewBuffer([]byte{})
9999
err = CompressToFileLimits(bb, img, FileSizeLimits{50000, 350000})
100100
require.NoError(t, err)
101-
require.Equal(t, 291645, bb.Len())
101+
require.Equal(t, 292395, bb.Len())
102102
}
103103

104104
func TestGetPayloadFromURI(t *testing.T) {

images/manipulation.go

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ import (
77
"image/color"
88
"image/draw"
99
"image/png"
10-
"math"
1110
"os"
1211

13-
"github.com/nfnt/resize"
14-
"github.com/oliamb/cutter"
1512
"go.uber.org/zap"
1613
xdraw "golang.org/x/image/draw"
1714

@@ -31,40 +28,75 @@ func (c *Circle) Bounds() image.Rectangle {
3128
func (c *Circle) At(x, y int) color.Color {
3229
xx, yy, rr := float64(x-c.X)+0.5, float64(y-c.Y)+0.5, float64(c.R)
3330
if xx*xx+yy*yy < rr*rr {
34-
return color.Alpha{255}
31+
return color.Alpha{A: 255}
3532
}
36-
return color.Alpha{0}
33+
return color.Alpha{A: 0}
34+
}
35+
36+
// Calculates scaling factors using old and new image dimensions.
37+
func calcFactors(width, height int, oldWidth, oldHeight float64) (scaleX, scaleY float64) {
38+
if width == 0 {
39+
if height == 0 {
40+
scaleX = 1.0
41+
scaleY = 1.0
42+
} else {
43+
scaleY = oldHeight / float64(height)
44+
scaleX = scaleY
45+
}
46+
} else {
47+
scaleX = oldWidth / float64(width)
48+
if height == 0 {
49+
scaleY = scaleX
50+
} else {
51+
scaleY = oldHeight / float64(height)
52+
}
53+
}
54+
return
55+
}
56+
57+
func resizeImage(img image.Image, width int, height int) image.Image {
58+
scaleX, scaleY := calcFactors(width, height, float64(img.Bounds().Dx()), float64(img.Bounds().Dy()))
59+
if width == 0 {
60+
width = int(0.7 + float64(img.Bounds().Dx())/scaleX)
61+
}
62+
if height == 0 {
63+
height = int(0.7 + float64(img.Bounds().Dy())/scaleY)
64+
}
65+
66+
out := image.NewNRGBA(image.Rect(0, 0, width, height))
67+
xdraw.BiLinear.Scale(out, out.Bounds(), img, img.Bounds(), draw.Over, nil)
68+
return out
3769
}
3870

3971
func Resize(size ResizeDimension, img image.Image) image.Image {
40-
var width, height uint
72+
var width, height int
4173

4274
switch {
4375
case img.Bounds().Max.X == img.Bounds().Max.Y:
44-
width, height = uint(size), uint(size)
76+
width, height = int(size), int(size)
4577
case img.Bounds().Max.X > img.Bounds().Max.Y:
46-
width, height = 0, uint(size)
78+
width, height = 0, int(size)
4779
default:
48-
width, height = uint(size), 0
80+
width, height = int(size), 0
4981
}
5082

5183
logutils.ZapLogger().Info("resizing",
5284
zap.Uint("size", uint(size)),
53-
zap.Uint("width", width),
54-
zap.Uint("height", height))
85+
zap.Int("width", width),
86+
zap.Int("height", height))
5587

56-
return resize.Resize(width, height, img, resize.Bilinear)
88+
return resizeImage(img, width, height)
5789
}
5890

59-
func ResizeTo(percent int, img image.Image) image.Image {
60-
width := uint(img.Bounds().Max.X * percent / 100)
61-
height := uint(img.Bounds().Max.Y * percent / 100)
91+
func Scale(percent int, img image.Image) image.Image {
92+
width := img.Bounds().Max.X * percent / 100
93+
height := img.Bounds().Max.Y * percent / 100
6294

63-
return resize.Resize(width, height, img, resize.Bilinear)
95+
return resizeImage(img, width, height)
6496
}
6597

6698
func ShrinkOnly(size ResizeDimension, img image.Image) image.Image {
67-
finalSize := int(math.Min(float64(size), math.Min(float64(img.Bounds().Dx()), float64(img.Bounds().Dy()))))
99+
finalSize := min(int(size), img.Bounds().Dx(), img.Bounds().Dy())
68100
return Resize(ResizeDimension(finalSize), img)
69101
}
70102

@@ -78,11 +110,14 @@ func Crop(img image.Image, rect image.Rectangle) (image.Image, error) {
78110
)
79111
}
80112

81-
return cutter.Crop(img, cutter.Config{
82-
Width: rect.Dx(),
83-
Height: rect.Dy(),
84-
Anchor: rect.Min,
85-
})
113+
// Use standard library cropping via SubImage on an RGBA buffer
114+
// Ensure the requested rectangle is within the image bounds (checked above)
115+
// Create a new RGBA of the crop size and draw from the source offset by rect.Min
116+
cropW, cropH := rect.Dx(), rect.Dy()
117+
out := image.NewRGBA(image.Rect(0, 0, cropW, cropH))
118+
// Source point corresponds to rect.Min in the original image
119+
draw.Draw(out, out.Bounds(), img, rect.Min, draw.Src)
120+
return out, nil
86121
}
87122

88123
// CropCenter takes an image, usually downloaded from a URL

images/manipulation_test.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package images
33
import (
44
"bytes"
55
"errors"
6+
"fmt"
67
"image"
78
"testing"
89

@@ -145,7 +146,7 @@ func TestCrop(t *testing.T) {
145146
{
146147
"elephant.jpg",
147148
[]params{
148-
{topLeftSquare, topLeftSquare, 1447, nil},
149+
{topLeftSquare, topLeftSquare, 1459, nil},
149150
{offsetSquare, rect, 0, errors.New("crop dimensions out of bounds of image, image width '80px' & height '80px'; crop bottom right coordinate at X '160px' Y '160px'")},
150151
{outOfBoundsSquare, rect, 0, errors.New("crop dimensions out of bounds of image, image width '80px' & height '80px'; crop bottom right coordinate at X '1000000px' Y '1000000px'")},
151152
},
@@ -180,21 +181,24 @@ func TestCrop(t *testing.T) {
180181
img, err := Decode(path + c.Filename)
181182
require.NoError(t, err)
182183

183-
for _, p := range c.Params {
184-
cImg, err := Crop(img, p.Rectangle)
185-
if p.CropError != nil {
186-
require.EqualError(t, err, p.CropError.Error())
187-
continue
188-
} else {
184+
for i, p := range c.Params {
185+
testName := fmt.Sprintf("%s-%d", c.Filename, i+1)
186+
t.Run(testName, func(t *testing.T) {
187+
cImg, err := Crop(img, p.Rectangle)
188+
if p.CropError != nil {
189+
require.EqualError(t, err, p.CropError.Error())
190+
return
191+
}
192+
189193
require.NoError(t, err)
190-
}
191-
require.Exactly(t, p.OutputBound.Dx(), cImg.Bounds().Dx(), c.Filename)
192-
require.Exactly(t, p.OutputBound.Dy(), cImg.Bounds().Dy(), c.Filename)
194+
require.Exactly(t, p.OutputBound.Dx(), cImg.Bounds().Dx(), c.Filename)
195+
require.Exactly(t, p.OutputBound.Dy(), cImg.Bounds().Dy(), c.Filename)
193196

194-
bb := bytes.NewBuffer([]byte{})
195-
err = Encode(bb, cImg, options)
196-
require.NoError(t, err)
197-
require.Exactly(t, p.OutputSize, bb.Len())
197+
bb := bytes.NewBuffer([]byte{})
198+
err = Encode(bb, cImg, options)
199+
require.NoError(t, err)
200+
require.Exactly(t, p.OutputSize, bb.Len())
201+
})
198202
}
199203
}
200204
}

0 commit comments

Comments
 (0)