summaryrefslogtreecommitdiff
path: root/internal/processor/image.go
blob: 9a7d7696ef65d9fba4ac0a6d6f02c359b964ebe9 (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package processor

import (
	"fmt"
	"image"
	"image/gif"
	"image/jpeg"
	"image/png"
	"os"
	"path/filepath"

	"golang.org/x/image/draw"
)

const (
	maxImageWidth  = 1024
	jpegQuality    = 80
)

// processImage reads the source image, resizes it if wider than maxImageWidth,
// encodes it as JPEG at jpegQuality, and writes the result to destDir.
// Returns the output filename (always a .jpg) and an HTML <img> snippet.
func processImage(srcPath, destDir, postID string) (filename, htmlContent string, err error) {
	img, err := decodeImage(srcPath)
	if err != nil {
		return "", "", err
	}

	img = resizeIfNeeded(img)

	outName := "image.jpg"
	outPath := filepath.Join(destDir, outName)

	if err := writeJPEG(img, outPath); err != nil {
		return "", "", err
	}

	// The <img> src is relative to the site root, pointing into the posts dir.
	src := fmt.Sprintf("posts/%s/%s", postID, outName)
	html := fmt.Sprintf(`<img src="%s" alt="" class="post-image">`, src)

	return outName, html, nil
}

// decodeImage decodes a JPEG, PNG, or GIF (first frame) from srcPath.
func decodeImage(srcPath string) (image.Image, error) {
	f, err := os.Open(srcPath)
	if err != nil {
		return nil, fmt.Errorf("open image %s: %w", srcPath, err)
	}
	defer f.Close()

	ext := filepath.Ext(srcPath)
	switch ext {
	case ".jpg", ".jpeg":
		img, err := jpeg.Decode(f)
		if err != nil {
			return nil, fmt.Errorf("decode JPEG %s: %w", srcPath, err)
		}
		return img, nil

	case ".png":
		img, err := png.Decode(f)
		if err != nil {
			return nil, fmt.Errorf("decode PNG %s: %w", srcPath, err)
		}
		return img, nil

	case ".gif":
		// Use only the first frame of animated GIFs.
		g, err := gif.Decode(f)
		if err != nil {
			return nil, fmt.Errorf("decode GIF %s: %w", srcPath, err)
		}
		return g, nil

	default:
		return nil, fmt.Errorf("unsupported image format: %s", ext)
	}
}

// resizeIfNeeded returns a resized copy of img if its width exceeds maxImageWidth,
// preserving aspect ratio. Otherwise the original is returned unchanged.
func resizeIfNeeded(img image.Image) image.Image {
	bounds := img.Bounds()
	w := bounds.Dx()

	if w <= maxImageWidth {
		return img
	}

	h := bounds.Dy()
	newW := maxImageWidth
	newH := (h * newW) / w

	dst := image.NewRGBA(image.Rect(0, 0, newW, newH))
	draw.BiLinear.Scale(dst, dst.Bounds(), img, bounds, draw.Over, nil)

	return dst
}

// writeJPEG encodes img as JPEG at the configured quality level and writes to path.
func writeJPEG(img image.Image, path string) error {
	f, err := os.Create(path)
	if err != nil {
		return fmt.Errorf("create JPEG %s: %w", path, err)
	}
	defer f.Close()

	opts := &jpeg.Options{Quality: jpegQuality}
	if err := jpeg.Encode(f, img, opts); err != nil {
		return fmt.Errorf("encode JPEG %s: %w", path, err)
	}

	return nil
}