Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resize with fit #260

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ type Options struct {
Quality int
Compression int
Zoom int
Fit bool // Fit image into given dimensions
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed a new option to bypass settings Force=true in normalizeOperation later
I can't use Crop or Embed:

  • Crop will crop extra pixels
  • Embed will pad missing pixes with background

Crop bool
SmartCrop bool // Deprecated, use: bimg.Options.Gravity = bimg.GravitySmart
Enlarge bool
Expand Down
8 changes: 6 additions & 2 deletions resizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func saveImage(image *C.VipsImage, o Options) ([]byte, error) {
}

func normalizeOperation(o *Options, inWidth, inHeight int) {
if !o.Force && !o.Crop && !o.Embed && !o.Enlarge && o.Rotate == 0 && (o.Width > 0 || o.Height > 0) {
if !o.Force && !o.Fit && !o.Crop && !o.Embed && !o.Enlarge && o.Rotate == 0 && (o.Width > 0 || o.Height > 0) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I check Fit option to disable setting Force=true

o.Force = true
}
}
Expand Down Expand Up @@ -443,7 +443,11 @@ func imageCalculations(o *Options, inWidth, inHeight int) float64 {
switch {
// Fixed width and height
case o.Width > 0 && o.Height > 0:
factor = math.Min(xfactor, yfactor)
if o.Crop {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When Crop option is set, then we should resize less and then crop
When no Crop option is set, we resize more, to fit the resulting rectangle

factor = math.Min(xfactor, yfactor)
} else {
factor = math.Max(xfactor, yfactor)
}
// Fixed width, auto height
case o.Width > 0:
if o.Crop {
Expand Down
30 changes: 30 additions & 0 deletions resizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,36 @@ func TestResizeCustomSizes(t *testing.T) {
}
}

func TestResizeFit(t *testing.T) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test new feature

// see https://github.com/h2non/bimg/issues/257
buf, _ := Read("testdata/test_fit.png")
forceOptions := []bool{true, false}

for _, forceOpt := range forceOptions {
opts := Options{Width: 1000, Height: 20, Fit: true, Force: forceOpt}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also check, that Force=true results in old behavior

newImg, err := Resize(buf, opts)
if err != nil {
t.Fatalf("Resize(imgData, %#v) error: %#v", opts, err)
}

size, _ := Size(newImg)
if !forceOpt && size.Width != 115 {
t.Fatalf("Invalid width (Force=false): %d, expected 115", size.Width)
}
if forceOpt && size.Width != 1000 {
t.Fatalf("Invalid width (Force=true): %d, expected 1000", size.Width)
}
if size.Height != 20 {
t.Fatalf("Invalid width: %d, expected 20", size.Width)
}

Write(fmt.Sprintf(
"testdata/test_fit_%v_out.png",
forceOpt,
), newImg)
}
}

func TestResizePrecision(t *testing.T) {
// see https://github.com/h2non/bimg/issues/99
img := image.NewGray16(image.Rect(0, 0, 1920, 1080))
Expand Down
Binary file added testdata/test_fit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.