2020-12-02 10:09:37 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/disintegration/imaging"
|
2021-01-11 10:10:32 -05:00
|
|
|
|
2021-07-21 11:08:52 -04:00
|
|
|
"gitlab.com/gitlab-org/gitlab/workhorse/cmd/gitlab-resize-image/png"
|
2020-12-02 10:09:37 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if err := _main(); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s: fatal: %v\n", os.Args[0], err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func _main() error {
|
|
|
|
widthParam := os.Getenv("GL_RESIZE_IMAGE_WIDTH")
|
|
|
|
requestedWidth, err := strconv.Atoi(widthParam)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GL_RESIZE_IMAGE_WIDTH: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:10:32 -05:00
|
|
|
pngReader, err := png.NewReader(os.Stdin)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("construct PNG reader: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
src, formatName, err := image.Decode(pngReader)
|
2020-12-02 10:09:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decode: %w", err)
|
|
|
|
}
|
|
|
|
imagingFormat, err := imaging.FormatFromExtension(formatName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("find imaging format: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
image := imaging.Resize(src, requestedWidth, 0, imaging.Lanczos)
|
|
|
|
return imaging.Encode(os.Stdout, image, imagingFormat)
|
|
|
|
}
|