aboutsummaryrefslogtreecommitdiff
path: root/internal/metadata/dimensions.go
blob: 55472a0f95e1d3e2386223fca71f29dafc9435ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package metadata

import (
	"fmt"
	"image"
	"os"

	_ "image/gif"
	_ "image/jpeg"
	_ "image/png"

	_ "golang.org/x/image/webp"
)

// readDimensions returns the dimensions of an image without decoding
// the entire image into memory.
func readDimensions(path string) (width, height int, err error) {
	file, err := os.Open(path)
	if err != nil {
		return 0, 0, fmt.Errorf("open image: %w", err)
	}
	defer func() {
		_ = file.Close()
	}()

	cfg, _, err := image.DecodeConfig(file)
	if err != nil {
		return 0, 0, fmt.Errorf("decode image config: %w", err)
	}

	return cfg.Width, cfg.Height, nil
}