[MM-62191] Remove disintegration/imaging dependency (#29657)
* Remove disintegration/imaging dependency * Simplify FillCenter logic --------- Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
28dbc3cabb
Коммит
316cde2569
@@ -6,8 +6,10 @@ package imaging
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
"math"
|
||||
|
||||
"github.com/disintegration/imaging"
|
||||
"github.com/anthonynsimon/bild/clone"
|
||||
"github.com/anthonynsimon/bild/transform"
|
||||
)
|
||||
|
||||
type rawImg interface {
|
||||
@@ -140,8 +142,126 @@ func FillImageTransparency(img image.Image, c color.Color) {
|
||||
}
|
||||
}
|
||||
|
||||
// CropAnchor cuts out a rectangular region with the specified size
|
||||
// from the image using the specified anchor point and returns the cropped image.
|
||||
// Adapted from github.com/disintegration/imaging
|
||||
func CropCenter(img image.Image, w, h int) image.Image {
|
||||
srcBounds := img.Bounds()
|
||||
anchorPoint := image.Pt(srcBounds.Min.X+(srcBounds.Dx()-w)/2, srcBounds.Min.Y+(srcBounds.Dy()-h)/2)
|
||||
r := image.Rect(0, 0, w, h).Add(anchorPoint)
|
||||
b := srcBounds.Intersect(r)
|
||||
return transform.Crop(img, b)
|
||||
}
|
||||
|
||||
// resizeAndCrop resizes the image to the smallest possible size that will cover the specified dimensions,
|
||||
// crops the resized image to the specified dimensions using a centered anchor point and returns
|
||||
// the transformed image.
|
||||
// Adapted from github.com/disintegration/imaging
|
||||
func resizeAndCropCenter(img image.Image, width, height int) image.Image {
|
||||
dstW, dstH := width, height
|
||||
|
||||
srcBounds := img.Bounds()
|
||||
srcW := srcBounds.Dx()
|
||||
srcH := srcBounds.Dy()
|
||||
srcAspectRatio := float64(srcW) / float64(srcH)
|
||||
dstAspectRatio := float64(dstW) / float64(dstH)
|
||||
|
||||
var tmp image.Image
|
||||
if srcAspectRatio < dstAspectRatio {
|
||||
tmp = Resize(img, dstW, 0, transform.Lanczos)
|
||||
} else {
|
||||
tmp = Resize(img, 0, dstH, transform.Lanczos)
|
||||
}
|
||||
|
||||
return CropCenter(tmp, dstW, dstH)
|
||||
}
|
||||
|
||||
// FillCenter creates an image with the specified dimensions and fills it with
|
||||
// the centered and scaled source image.
|
||||
func FillCenter(img image.Image, w, h int) *image.NRGBA {
|
||||
return imaging.Fill(img, w, h, imaging.Center, imaging.Lanczos)
|
||||
// To achieve the correct aspect ratio without stretching, the source image will be cropped.
|
||||
// Adapted from github.com/disintegration/imaging
|
||||
func FillCenter(img image.Image, dstW, dstH int) image.Image {
|
||||
if dstW <= 0 || dstH <= 0 {
|
||||
return &image.RGBA{}
|
||||
}
|
||||
|
||||
srcBounds := img.Bounds()
|
||||
srcW := srcBounds.Dx()
|
||||
srcH := srcBounds.Dy()
|
||||
|
||||
if srcW <= 0 || srcH <= 0 {
|
||||
return &image.RGBA{}
|
||||
}
|
||||
|
||||
if srcW == dstW && srcH == dstH {
|
||||
return clone.AsShallowRGBA(img)
|
||||
}
|
||||
|
||||
return resizeAndCropCenter(img, dstW, dstH)
|
||||
}
|
||||
|
||||
// Fit scales down the image to fit the specified
|
||||
// maximum width and height and returns the transformed image.
|
||||
// Adapted from github.com/disintegration/imaging
|
||||
func Fit(img image.Image, maxW, maxH int) image.Image {
|
||||
if maxW <= 0 || maxH <= 0 {
|
||||
return &image.NRGBA{}
|
||||
}
|
||||
|
||||
srcBounds := img.Bounds()
|
||||
srcW := srcBounds.Dx()
|
||||
srcH := srcBounds.Dy()
|
||||
|
||||
if srcW <= 0 || srcH <= 0 {
|
||||
return &image.RGBA{}
|
||||
}
|
||||
|
||||
if srcW <= maxW && srcH <= maxH {
|
||||
return clone.AsShallowRGBA(img)
|
||||
}
|
||||
|
||||
srcAspectRatio := float64(srcW) / float64(srcH)
|
||||
maxAspectRatio := float64(maxW) / float64(maxH)
|
||||
|
||||
var newW, newH int
|
||||
if srcAspectRatio > maxAspectRatio {
|
||||
newW = maxW
|
||||
newH = int(float64(newW) / srcAspectRatio)
|
||||
} else {
|
||||
newH = maxH
|
||||
newW = int(float64(newH) * srcAspectRatio)
|
||||
}
|
||||
|
||||
return Resize(img, newW, newH, transform.Lanczos)
|
||||
}
|
||||
|
||||
// Resize resizes the image to the specified width and height using the specified resampling filter and returns the transformed image.
|
||||
// If one of width or height is 0, the image aspect ratio is preserved.
|
||||
// Adapted from github.com/disintegration/imaging
|
||||
func Resize(img image.Image, targetWidth, targetHeight int, filter transform.ResampleFilter) image.Image {
|
||||
if targetWidth < 0 || targetHeight < 0 {
|
||||
return &image.NRGBA{}
|
||||
}
|
||||
|
||||
if targetWidth == 0 && targetHeight == 0 {
|
||||
return &image.NRGBA{}
|
||||
}
|
||||
|
||||
srcW := img.Bounds().Dx()
|
||||
srcH := img.Bounds().Dy()
|
||||
if srcW <= 0 || srcH <= 0 {
|
||||
return &image.NRGBA{}
|
||||
}
|
||||
|
||||
// If new width or height is 0 then preserve aspect ratio, minimum 1px.
|
||||
if targetWidth == 0 {
|
||||
tmpW := float64(targetHeight) * float64(srcW) / float64(srcH)
|
||||
targetWidth = int(math.Max(1.0, math.Floor(tmpW+0.5)))
|
||||
}
|
||||
if targetHeight == 0 {
|
||||
tmpH := float64(targetWidth) * float64(srcH) / float64(srcW)
|
||||
targetHeight = int(math.Max(1.0, math.Floor(tmpH+0.5)))
|
||||
}
|
||||
|
||||
return transform.Resize(img, targetWidth, targetHeight, filter)
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user