@@ -43,6 +43,7 @@ type ImageProcessorOptions struct {
4343 CropMode string
4444 BorderRadius uint64
4545 BGColor string
46+ Focalpoint Focalpoint
4647}
4748
4849type imageProcessor struct {
@@ -111,7 +112,7 @@ func (ip *imageProcessor) scaleWand(wand *imagick.MagickWand, request *ImageProc
111112 }
112113
113114 if request .CropMode == "fill" {
114- if err = ip .cropImage (newDimensions , request .Dimensions , wand ); err != nil {
115+ if err = ip .cropImage (wand , newDimensions , request .Dimensions , request . Focalpoint ); err != nil {
115116 ip .Logger .Warnf ("ImageMagick error cropping image: %s" , err )
116117 return true , err
117118 }
@@ -295,13 +296,18 @@ func (ip *imageProcessor) clampDimensionsToMaxima(dimensions ImageDimensions, re
295296 return dimensions
296297}
297298
298- func (ip * imageProcessor ) cropImage (currentDimensions ImageDimensions , requestedDimensions ImageDimensions , wand * imagick.MagickWand ) (err error ) {
299- err = wand .CropImage (
300- uint (requestedDimensions .Width ),
301- uint (requestedDimensions .Height ),
302- int ((currentDimensions .Width - requestedDimensions .Width )/ 2 ),
303- int ((currentDimensions .Height - requestedDimensions .Height )/ 2 ),
304- )
299+ func (ip * imageProcessor ) cropImage (wand * imagick.MagickWand , id ImageDimensions , rd ImageDimensions , fp Focalpoint ) (err error ) {
300+ xo := fp .X
301+ yo := fp .Y
302+ xc := float64 (id .Width )* xo - float64 (rd .Width )* xo
303+ yc := float64 (id .Height )* yo - float64 (rd .Height )* yo
304+
305+ w := uint (rd .Width )
306+ h := uint (rd .Height )
307+ x := int (xc )
308+ y := int (yc )
309+
310+ err = wand .CropImage (w , h , x , y )
305311 return
306312}
307313
0 commit comments