thumb

package
v0.0.0-...-c8acfb9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2024 License: AGPL-3.0 Imports: 29 Imported by: 0

Documentation

Overview

Package thumb provides JPEG resampling and thumbnail generation.

Copyright (c) 2018 - 2024 PhotoPrism UG. All rights reserved.

This program is free software: you can redistribute it and/or modify
it under Version 3 of the GNU Affero General Public License (the "AGPL"):
<https://docs.photoprism.app/license/agpl>

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

The AGPL is supplemented by our Trademark and Brand Guidelines,
which describe how our Brand Assets may be used:
<https://www.photoprism.app/trademark>

Feel free to send an email to hello@photoprism.app if you have questions, want to support our work, or just want to say hello.

Additional information can be found in our Developer Guide: <https://docs.photoprism.app/developer-guide/>

Index

Constants

View Source
const (
	KB = 1024
	MB = KB * 1024
	GB = MB * 1024
)

Byte size factors.

View Source
const (
	OrientationUnspecified int = 0
	OrientationNormal          = 1
	OrientationFlipH           = 2
	OrientationRotate180       = 3
	OrientationFlipV           = 4
	OrientationTranspose       = 5
	OrientationRotate270       = 6
	OrientationTransverse      = 7
	OrientationRotate90        = 8
)

Variables

View Source
var (
	JpegQuality      = QualityDefault
	JpegQualitySmall = QualityLow
)

Current Quality settings.

View Source
var (
	SizePrecached = 2048
	SizeUncached  = 7680
	Filter        = ResampleLanczos
)
View Source
var (
	SizeTile50   = Size{Tile50, Tile500, "List View", 50, 50, false, false, []ResampleOption{ResampleFillCenter, ResampleDefault}}
	SizeTile100  = Size{Tile100, Tile500, "Places View", 100, 100, false, false, []ResampleOption{ResampleFillCenter, ResampleDefault}}
	SizeTile224  = Size{Tile224, Tile500, "TensorFlow, Mosaic View", 224, 224, false, false, []ResampleOption{ResampleFillCenter, ResampleDefault}}
	SizeTile500  = Size{Tile500, "", "Cards View", 500, 500, false, false, []ResampleOption{ResampleFillCenter, ResampleDefault}}
	SizeColors   = Size{Colors, Fit720, "Color Detection", 3, 3, false, false, []ResampleOption{ResampleResize, ResampleNearestNeighbor, ResamplePng}}
	SizeLeft224  = Size{Left224, Fit720, "TensorFlow", 224, 224, false, false, []ResampleOption{ResampleFillTopLeft, ResampleDefault}}
	SizeRight224 = Size{Right224, Fit720, "TensorFlow", 224, 224, false, false, []ResampleOption{ResampleFillBottomRight, ResampleDefault}}
)
View Source
var (
	CachePublic = false
)
View Source
var (
	ErrNotCached = errors.New("not cached")
)

FitSizes contains "fit" cropped thumbnail sizes from largest to smallest. Best for the viewer as proportional resizing maintains the aspect ratio.

Names contains all default size names.

View Source
var QualityLevels = map[string]Quality{
	"5":         QualityBest,
	"ultra":     QualityBest,
	"best":      QualityBest,
	"4":         QualityHigh,
	"excellent": QualityHigh,
	"good":      QualityHigh,
	"high":      QualityHigh,
	"3":         QualityDefault,
	"":          QualityDefault,
	"ok":        QualityDefault,
	"default":   QualityDefault,
	"standard":  QualityDefault,
	"medium":    QualityDefault,
	"2":         QualityLow,
	"low":       QualityLow,
	"small":     QualityLow,
	"1":         QualityBad,
	"bad":       QualityBad,
	"0":         QualityWorst,
	"worst":     QualityWorst,
	"lowest":    QualityWorst,
}

QualityLevels maps human-readable settings to a numeric Quality.

View Source
var ResampleMethods = map[ResampleOption]string{
	ResampleFillCenter:      "center",
	ResampleFillTopLeft:     "left",
	ResampleFillBottomRight: "right",
	ResampleFit:             "fit",
	ResampleResize:          "resize",
}
View Source
var Sizes = SizeMap{
	Tile50:   SizeTile50,
	Tile100:  SizeTile100,
	Tile224:  SizeTile224,
	Tile500:  SizeTile500,
	Colors:   SizeColors,
	Left224:  SizeLeft224,
	Right224: SizeRight224,
	Fit720:   {Fit720, "", "SD TV, Mobile", 720, 720, true, true, []ResampleOption{ResampleFit, ResampleDefault}},
	Fit1280:  {Fit1280, Fit2048, "HD TV, SXGA", 1280, 1024, true, true, []ResampleOption{ResampleFit, ResampleDefault}},
	Fit1920:  {Fit1920, Fit2048, "Full HD", 1920, 1200, true, true, []ResampleOption{ResampleFit, ResampleDefault}},
	Fit2048:  {Fit2048, "", "DCI 2K, Tablets", 2048, 2048, true, true, []ResampleOption{ResampleFit, ResampleDefault}},
	Fit2560:  {Fit2560, "", "Quad HD, Notebooks", 2560, 1600, true, true, []ResampleOption{ResampleFit, ResampleDefault}},
	Fit3840:  {Fit3840, "", "4K Ultra HD", 3840, 2400, true, true, []ResampleOption{ResampleFit, ResampleDefault}},
	Fit4096:  {Fit4096, "", "DCI 4K, Retina 4K", 4096, 4096, true, true, []ResampleOption{ResampleFit, ResampleDefault}},
	Fit7680:  {Fit7680, "", "8K Ultra HD 2", 7680, 4320, true, true, []ResampleOption{ResampleFit, ResampleDefault}},
}

Sizes contains the properties of all thumbnail sizes.

View Source
var StandardRGB = true

StandardRGB configures whether colors in the Apple Display P3 color space should be converted to standard RGB.

VideoSizes contains all valid video output sizes sorted by size.

Functions

func Create

func Create(img image.Image, fileName string, width, height int, opts ...ResampleOption) (result image.Image, err error)

Create creates an image thumbnail.

func FileName

func FileName(hash, thumbPath string, width, height int, opts ...ResampleOption) (fileName string, err error)

FileName returns the file name of the thumbnail for the matching size.

func Find

func Find(limit int) (name Name, size Size)

Find returns the largest default thumbnail type for the given size limit.

func FromCache

func FromCache(imageFilename, hash, thumbPath string, width, height int, opts ...ResampleOption) (fileName string, err error)

FromCache returns the filename if a thumbnail image with the matching size is in the cache.

func FromFile

func FromFile(imageFilename, hash, thumbPath string, width, height, orientation int, opts ...ResampleOption) (fileName string, err error)

FromFile creates a new thumbnail with the specified size if it was not found in the cache, and returns the filename.

func InvalidSize

func InvalidSize(size int) bool

InvalidSize tests if the size in pixels is invalid.

func Jpeg

func Jpeg(srcFile, jpgFile string, orientation int) (img image.Image, err error)

Jpeg converts an image to JPEG, saves it, and returns it.

func MaxSize

func MaxSize() int

MaxSize returns the max supported size in pixels.

func Open

func Open(fileName string, orientation int) (result image.Image, err error)

Open loads an image from disk, rotates it, and converts the color profile if necessary.

func OpenJpeg

func OpenJpeg(fileName string, orientation int) (image.Image, error)

OpenJpeg loads a JPEG image from disk, rotates it, and converts the color profile if necessary.

func Png

func Png(srcFile, pngFile string, orientation int) (img image.Image, err error)

Png converts an image to PNG, saves it, and returns it.

func Report

func Report(sizes SizeList, short bool) (rows [][]string, cols []string)

Report returns a file format documentation table.

func Resample

func Resample(img image.Image, width, height int, opts ...ResampleOption) image.Image

Resample downscales an image and returns it.

func ResolvedName

func ResolvedName(hash, thumbPath string, width, height int, opts ...ResampleOption) (fileName string, err error)

ResolvedName returns the file name of the thumbnail for the matching size with all symlinks resolved.

func Rotate

func Rotate(img image.Image, o int) image.Image

Rotate rotates an image based on the Exif orientation.

func Suffix

func Suffix(width, height int, opts ...ResampleOption) (result string)

Suffix returns the thumb cache file suffix.

func Url

func Url(h, name, contentUri, previewToken string) string

Url returns a thumbnail url based on hash, thumb name, cdn uri, and preview token.

Types

type Bytes

type Bytes uint64

Bytes represents memory usage in bytes.

func MemSize

func MemSize(img image.Image) Bytes

MemSize returns the estimated size of the image in memory in bytes.

func (Bytes) GByte

func (b Bytes) GByte() float64

GByte returns the size in gigabyte.

func (Bytes) KByte

func (b Bytes) KByte() float64

KByte returns the size in kilobyte.

func (Bytes) MByte

func (b Bytes) MByte() float64

MByte returns the size in megabyte.

func (Bytes) String

func (b Bytes) String() string

String returns a human-readable memory usage string.

type Name

type Name string

Name represents a thumbnail size name.

const (
	Tile50   Name = "tile_50"
	Tile100  Name = "tile_100"
	Tile224  Name = "tile_224"
	Tile500  Name = "tile_500"
	Colors   Name = "colors"
	Left224  Name = "left_224"
	Right224 Name = "right_224"
	Fit720   Name = "fit_720"
	Fit1280  Name = "fit_1280"
	Fit1920  Name = "fit_1920"
	Fit2048  Name = "fit_2048"
	Fit2560  Name = "fit_2560"
	Fit3840  Name = "fit_3840"
	Fit4096  Name = "fit_4096"
	Fit7680  Name = "fit_7680"
)

Names of thumbnail sizes.

func (Name) Jpeg

func (n Name) Jpeg() string

Jpeg returns the thumbnail name with a jpeg file extension suffix as string.

func (Name) String

func (n Name) String() string

String returns the thumbnail name as string.

type Public

type Public struct {
	Fit720  Thumb `json:"fit_720"`
	Fit1280 Thumb `json:"fit_1280"`
	Fit1920 Thumb `json:"fit_1920"`
	Fit2048 Thumb `json:"fit_2048"`
	Fit2560 Thumb `json:"fit_2560"`
	Fit3840 Thumb `json:"fit_3840"`
	Fit4096 Thumb `json:"fit_4096"`
	Fit7680 Thumb `json:"fit_7680"`
}

Public represents public thumbnail URLs with dimensions.

type Quality

type Quality int

Quality represents a JPEG image quality.

const (
	QualityBest    Quality = 95
	QualityHigh    Quality = 92
	QualityDefault Quality = 85
	QualityLow     Quality = 80
	QualityBad     Quality = 75
	QualityWorst   Quality = 70
)

Common Quality levels. see https://docs.photoprism.app/user-guide/settings/advanced/#jpeg-quality

func ParseQuality

func ParseQuality(s string) Quality

ParseQuality returns the matching quality based on a config value string.

func (Quality) EncodeOption

func (q Quality) EncodeOption() imaging.EncodeOption

EncodeOption returns the quality as imaging.EncodeOption.

func (Quality) String

func (q Quality) String() string

String returns the quality as string.

type ResampleFilter

type ResampleFilter string
const (
	ResampleBlackman ResampleFilter = "blackman"
	ResampleLanczos  ResampleFilter = "lanczos"
	ResampleCubic    ResampleFilter = "cubic"
	ResampleLinear   ResampleFilter = "linear"
)

func (ResampleFilter) Imaging

type ResampleOption

type ResampleOption int
const (
	ResampleFillCenter ResampleOption = iota
	ResampleFillTopLeft
	ResampleFillBottomRight
	ResampleFit
	ResampleResize
	ResampleNearestNeighbor
	ResampleDefault
	ResamplePng
)

func ResampleOptions

func ResampleOptions(opts ...ResampleOption) (method ResampleOption, filter imaging.ResampleFilter, format fs.Type)

ResampleOptions extracts filter, format, and method from resample options.

type Size

type Size struct {
	Name    Name             `json:"name"`
	Source  Name             `json:"-"`
	Usage   string           `json:"usage"`
	Width   int              `json:"w"`
	Height  int              `json:"h"`
	Public  bool             `json:"-"`
	Fit     bool             `json:"-"`
	Options []ResampleOption `json:"-"`
}

Size represents a standard media resolution.

func Fit

func Fit(w, h int) (size Size)

Fit returns the largest fitting thumbnail size.

func FitBounds

func FitBounds(r image.Rectangle) (s Size)

FitBounds returns the largest thumbnail size fitting the rectangle.

func VideoSize

func VideoSize(limit int) Size

VideoSize returns the largest video size type for the given width limit.

func (Size) Bounds

func (s Size) Bounds() image.Rectangle

Bounds returns the thumb size as image.Rectangle.

func (Size) Create

func (s Size) Create(img image.Image, fileName string) (image.Image, error)

Create creates a thumbnail with the matching size and returns it as image.Image.

func (Size) ExceedsLimit

func (s Size) ExceedsLimit() bool

ExceedsLimit tests if thumbnail type is too large, and can not be rendered at all.

func (Size) FileName

func (s Size) FileName(hash, thumbPath string) (string, error)

FileName returns the file name of the thumbnail for the matching size.

func (Size) FromCache

func (s Size) FromCache(fileName, fileHash, cachePath string) (string, error)

FromCache returns the filename if a thumbnail image with the matching size is in the cache.

func (Size) FromFile

func (s Size) FromFile(fileName, fileHash, cachePath string, fileOrientation int) (string, error)

FromFile creates a new thumbnail with the matching size if it was not found in the cache, and returns the filename.

func (Size) ResolvedName

func (s Size) ResolvedName(hash, thumbPath string) (string, error)

ResolvedName returns the file name of the thumbnail for the matching size with all symlinks resolved.

func (Size) Skip

func (s Size) Skip(img image.Image) bool

Skip checks if the thumbnail size is too large for the image and can be skipped.

func (Size) Uncached

func (s Size) Uncached() bool

Uncached tests if thumbnail type exceeds the cached thumbnails size limit.

type SizeList

type SizeList []Size

SizeList represents a list of sizes.

type SizeMap

type SizeMap map[Name]Size

SizeMap maps size names to sizes.

func (SizeMap) All

func (m SizeMap) All() SizeList

All returns a slice containing all sizes.

type Thumb

type Thumb struct {
	W   int    `json:"w"`
	H   int    `json:"h"`
	Src string `json:"src"`
}

Thumb represents a photo thumbnail.

func New

func New(w, h int, hash string, s Size, contentUri, previewToken string) Thumb

New creates a new photo thumbnail.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL