mirror of
https://github.com/mattermost/mattermost.git
synced 2026-04-13 13:08:56 -04:00
* Remove disintegration/imaging dependency * Simplify FillCenter logic --------- Co-authored-by: Mattermost Build <build@mattermost.com>
48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package imaging
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"image"
|
|
"image/jpeg"
|
|
|
|
"github.com/anthonynsimon/bild/transform"
|
|
)
|
|
|
|
// GeneratePreview generates the preview for the given image.
|
|
func GeneratePreview(img image.Image, width int) image.Image {
|
|
preview := img
|
|
w := img.Bounds().Dx()
|
|
|
|
if w > width {
|
|
preview = Resize(img, width, 0, transform.Lanczos)
|
|
}
|
|
|
|
return preview
|
|
}
|
|
|
|
// GenerateThumbnail generates the thumbnail for the given image.
|
|
func GenerateThumbnail(img image.Image, targetWidth, targetHeight int) image.Image {
|
|
width := img.Bounds().Dx()
|
|
height := img.Bounds().Dy()
|
|
|
|
// We keep aspect ratio and ensure the output dimensions are never higher than the provided targets.
|
|
if width > height {
|
|
return Resize(img, targetWidth, 0, transform.Lanczos)
|
|
}
|
|
|
|
return Resize(img, 0, targetHeight, transform.Lanczos)
|
|
}
|
|
|
|
// GenerateMiniPreviewImage generates the mini preview for the given image.
|
|
func GenerateMiniPreviewImage(img image.Image, w, h, q int) ([]byte, error) {
|
|
var buf bytes.Buffer
|
|
preview := Resize(img, w, h, transform.Lanczos)
|
|
if err := jpeg.Encode(&buf, preview, &jpeg.Options{Quality: q}); err != nil {
|
|
return nil, fmt.Errorf("failed to encode image to JPEG format: %w", err)
|
|
}
|
|
return buf.Bytes(), nil
|
|
}
|