github.com/go-oss/image@v0.1.1-0.20230517025328-001b78555e78/imageutil/fit.go (about)

     1  package imageutil
     2  
     3  import (
     4  	"image"
     5  
     6  	"github.com/disintegration/imaging"
     7  )
     8  
     9  // FitCrop fit to narrow side of image and crop square.
    10  func FitCrop(img image.Image) image.Image {
    11  	size := img.Bounds().Size()
    12  	if size.X != size.Y {
    13  		narrowSide := size.Y
    14  		if size.X < size.Y {
    15  			narrowSide = size.X
    16  		}
    17  		img = imaging.CropCenter(img, narrowSide, narrowSide)
    18  	}
    19  
    20  	return img
    21  }
    22  
    23  // FitCrop for Image struct.
    24  func (img *Image) FitCrop() {
    25  	img.Image = FitCrop(img.Image)
    26  }