Closed david02324 closed 7 months ago
I tried to implement CSS Object-fit spec requirements using govips
func vipsCropStyle(originalWidth, originalHeight int, style *Style) (vips.Interesting, vips.Size) { switch style.Fit { case Cover: if originalWidth < style.Size.Width || originalHeight < style.Size.Height { return vips.InterestingCentre, vips.SizeUp } return vips.InterestingCentre, vips.SizeDown case Fill: return vips.InterestingAll, vips.SizeForce case Outside: return vips.InterestingAll, vips.SizeUp case Contain: return vips.InterestingCentre, vips.SizeLast } return vips.InterestingNone, vips.SizeDown // "inside" }
and this worked pretty well in most usecases.
But some strange behavior happens when cropping & resizing animated images
if I try to crop this image with Cover or Contain option, this is the result. (Other options work well)
Cover
Contain
The following simple code can reproduce the problem.
func TestAnimatedImageCrop(t *testing.T) { importParams := vips.NewImportParams() importParams.NumPages.Set(-1) importParams.FailOnError.Set(false) img, e := loadImageFromFile("the-simpson.gif", importParams) // original image size is 320x320 if e != nil { t.Fatal(e) return } if err := img.AutoRotate(); err != nil { t.Fatal(err) return } style := &govips.Style{ Size: &govips.Size{ Width: 200, Height: 200, }, Fit: govips.Cover, ImageType: govips.ImageFormatGIF, } interesting, scale := vipsCropStyle(img.Width(), img.Height(), style) err := img.ThumbnailWithSize(style.Size.Width, style.Size.Height, interesting, scale) if err != nil { t.Fatal(err) return } buf, _, err := img.ExportGIF(vips.NewGifExportParams()) if err != nil { t.Fatal(err) return } path, err := govips.SaveImageFileFromBuffer(buf, ".gif") if err != nil { t.Fatal(err) } fmt.Println(path) }
How can I crop animated image well? or please let me know if I'm using govips incorrectly or if it's not supported because it's an old version
Some code like https://github.com/imagecodex/govips/pull/9
Govips version 8.10.5 / Debian GNU/Linux 11 (bullseye)
I tried to implement CSS Object-fit spec requirements using govips
and this worked pretty well in most usecases.
But some strange behavior happens when cropping & resizing animated images
Source image
if I try to crop this image with
Cover
orContain
option, this is the result. (Other options work well)The following simple code can reproduce the problem.
How can I crop animated image well? or please let me know if I'm using govips incorrectly or if it's not supported because it's an old version