Documentation
¶
Overview ¶
Package images provides small, pure helpers for validating, encoding, and thumbnailing images. It takes bytes/io.Reader in and gives bytes out; it has no knowledge of HTTP or object storage.
Supported formats are PNG, JPEG, and GIF. Thumbnailing preserves aspect ratio and never upscales.
Orientation: JPEG thumbnails honor the EXIF Orientation tag, so a photo captured in portrait on a phone (which stores landscape pixels plus an orientation flag) thumbnails upright. Orientation is read only from JPEG data; PNG and GIF have no equivalent tag.
Animation: animated GIFs keep their animation — every frame is resized and re-quantized, and the loop count and per-frame delays are preserved. Single-frame GIFs stay single-frame.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidImageContentType indicates the image was of an unsupported type. ErrInvalidImageContentType = errors.New("invalid image content type") // ErrInvalidThumbnailDimensions indicates a zero width or height was requested. ErrInvalidThumbnailDimensions = errors.New("thumbnail width and height must both be greater than zero") // ErrImageTooLarge indicates the image exceeds the configured size or dimension limits. ErrImageTooLarge = errors.New("image too large") )
Functions ¶
This section is empty.
Types ¶
type Image ¶
Image is a decoded, in-memory image with its detected content type.
func Decode ¶
Decode reads an image from r, validating that it is a supported, decodable image and detecting its content type from the data itself (not from any filename).
Example ¶
package main
import (
"bytes"
"fmt"
"image"
"image/png"
"github.com/primandproper/platform-go/v10/uploads/images"
)
func main() {
var buf bytes.Buffer
if err := png.Encode(&buf, image.NewRGBA(image.Rect(0, 0, 4, 4))); err != nil {
panic(err)
}
// Decode detects the content type from the data itself, not from any filename.
img, err := images.Decode(&buf)
if err != nil {
panic(err)
}
fmt.Println(img.ContentType)
}
Output: image/png
func (*Image) DataURI ¶
DataURI returns the image encoded as a base64 data URI.
Example ¶
package main
import (
"fmt"
"github.com/primandproper/platform-go/v10/uploads/images"
)
func main() {
img := &images.Image{ContentType: "text/plain", Data: []byte("hi")}
fmt.Println(img.DataURI())
}
Output: data:text/plain;base64,aGk=
func (*Image) Thumbnail ¶
Thumbnail returns a resized copy of the image, re-encoded in its original format.
Example ¶
package main
import (
"bytes"
"fmt"
"image"
"image/png"
"github.com/primandproper/platform-go/v10/uploads/images"
)
func main() {
var buf bytes.Buffer
if err := png.Encode(&buf, image.NewRGBA(image.Rect(0, 0, 8, 4))); err != nil {
panic(err)
}
img, err := images.Decode(&buf)
if err != nil {
panic(err)
}
thumb, err := img.Thumbnail(4, 4)
if err != nil {
panic(err)
}
fmt.Println(thumb.ContentType)
}
Output: image/png