thumb

package
v0.0.0-...-ace2fcc Latest Latest
Warning

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

Go to latest
Published: May 21, 2024 License: AGPL-3.0 Imports: 33 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 (
	MiB               = 1024 * 1024
	GiB               = 1024 * MiB
	DefaultCacheMem   = 128 * MiB
	DefaultCacheSize  = 128
	DefaultCacheFiles = 16
	DefaultWorkers    = 1
)
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 (
	MaxCacheMem   = DefaultCacheMem
	MaxCacheSize  = DefaultCacheSize
	MaxCacheFiles = DefaultCacheFiles
	NumWorkers    = DefaultWorkers
)
View Source
var (
	SizeCached   = SizeFit1920.Width
	SizeOnDemand = SizeFit7680.Width
)
View Source
var (
	SizeColors   = Size{Colors, Fit720, "Color Detection", 3, 3, false, false, false, true, Options{ResampleResize, ResampleNearestNeighbor, ResamplePng}}
	SizeTile50   = Size{Tile50, Fit720, "List View", 50, 50, false, false, false, true, Options{ResampleFillCenter, ResampleDefault}}
	SizeTile100  = Size{Tile100, Fit720, "Places View", 100, 100, false, false, false, true, Options{ResampleFillCenter, ResampleDefault}}
	SizeTile224  = Size{Tile224, Fit720, "TensorFlow, Mosaic View", 224, 224, false, false, false, true, Options{ResampleFillCenter, ResampleDefault}}
	SizeLeft224  = Size{Left224, Fit720, "TensorFlow", 224, 224, false, false, false, false, Options{ResampleFillTopLeft, ResampleDefault}}
	SizeRight224 = Size{Right224, Fit720, "TensorFlow", 224, 224, false, false, false, false, Options{ResampleFillBottomRight, ResampleDefault}}
	SizeFit720   = Size{Fit720, "", "SD TV, Mobile", 720, 720, true, true, false, true, Options{ResampleFit, ResampleDefault}}
	SizeTile500  = Size{Tile500, Fit1920, "Cards View", 500, 500, false, false, false, true, Options{ResampleFillCenter, ResampleDefault}}
	SizeTile1080 = Size{Tile1080, Fit1920, "Instagram", 1080, 1080, false, false, true, false, Options{ResampleFillCenter, ResampleDefault}}
	SizeFit1280  = Size{Fit1280, Fit1920, "HD TV, SXGA", 1280, 1024, true, true, false, false, Options{ResampleFit, ResampleDefault}}
	SizeFit1600  = Size{Fit1600, Fit1920, "Social Media", 1600, 900, false, true, true, false, Options{ResampleFit, ResampleDefault}}
	SizeFit1920  = Size{Fit1920, "", "Full HD", 1920, 1200, true, true, false, false, Options{ResampleFit, ResampleDefault}}
	SizeFit2048  = Size{Fit2048, Fit4096, "DCI 2K, Tablets", 2048, 2048, false, true, true, false, Options{ResampleFit, ResampleDefault}}
	SizeFit2560  = Size{Fit2560, Fit4096, "Quad HD, Notebooks", 2560, 1600, true, true, false, false, Options{ResampleFit, ResampleDefault}}
	SizeFit3840  = Size{Fit3840, Fit4096, "4K Ultra HD", 3840, 2400, false, true, true, false, Options{ResampleFit, ResampleDefault}}
	SizeFit4096  = Size{Fit4096, "", "DCI 4K, Retina 4K", 4096, 4096, true, true, false, false, Options{ResampleFit, ResampleDefault}}
	SizeFit7680  = Size{Fit7680, "", "8K Ultra HD 2", 7680, 4320, true, true, false, false, Options{ResampleFit, ResampleDefault}}
)
View Source
var (
	CachePublic = false
)
View Source
var Color = ColorAuto

Color sets the standard color profile for thumbnails.

View Source
var (
	ErrNotCached = errors.New("not cached")
)

Filter specifies the default downscaling filter.

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

View Source
var JpegQualityDefault = QualityMedium

JpegQualityDefault sets the compression level of newly created JPEGs.

View Source
var Library = LibImaging

Library specifies the image library to be used.

Names contains all default size names.

View Source
var QualityLevels = map[string]Quality{
	"max":      QualityMax,
	"ultra":    QualityMax,
	"best":     QualityMax,
	"6":        QualityMax,
	"5":        QualityMax,
	"high":     QualityHigh,
	"good":     QualityHigh,
	"4":        QualityHigh,
	"medium":   QualityMedium,
	"med":      QualityMedium,
	"default":  QualityMedium,
	"standard": QualityMedium,
	"auto":     QualityMedium,
	"":         QualityMedium,
	"3":        QualityMedium,
	"low":      QualityLow,
	"small":    QualityLow,
	"2":        QualityLow,
	"min":      QualityMin,
	"1":        QualityMin,
	"0":        QualityMin,
}

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{
	Colors:   SizeColors,
	Tile50:   SizeTile50,
	Tile100:  SizeTile100,
	Left224:  SizeLeft224,
	Right224: SizeRight224,
	Tile224:  SizeTile224,
	Fit720:   SizeFit720,
	Tile500:  SizeTile500,
	Tile1080: SizeTile1080,
	Fit1280:  SizeFit1280,
	Fit1600:  SizeFit1600,
	Fit1920:  SizeFit1920,
	Fit2048:  SizeFit2048,
	Fit2560:  SizeFit2560,
	Fit3840:  SizeFit3840,
	Fit4096:  SizeFit4096,
	Fit7680:  SizeFit7680,
}

Sizes contains the properties of all thumbnail sizes.

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(imageName, hash, thumbPath string, width, height, orientation int, opts ...ResampleOption) (fileName string, err error)

FromFile generates a new thumbnail with the requested size, if it does not already exist, and returns its filename.

func Init

func Init(availableMemory uint64, maxWorkers int, imgLib string)

Init initializes the package config based on the the available memory, the allowed number of workers and the image processing library to be used.

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 ResampleOptions

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

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

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 Shutdown

func Shutdown()

Shutdown shuts down dependencies like libvips.

func Skip

func Skip(s Size, bounds image.Rectangle) bool

Skip tests if the size can be skipped when generating thumbnails, e.g. because it is larger than the original.

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.

func Vips

func Vips(imageName string, imageBuffer []byte, hash, thumbPath string, width, height int, opts ...ResampleOption) (thumbName string, thumbBuffer []byte, err error)

Vips generates a new thumbnail with the requested size and returns the file name and a buffer with the image bytes, or an error if thumbnail generation failed. For more information on libvips, see https://github.com/libvips/libvips.

func VipsImportParams

func VipsImportParams() *vips.ImportParams

VipsImportParams provides parameters for opening files with libvips.

func VipsInit

func VipsInit()

VipsInit initializes libvips by checking its version and loading the ICC profiles once.

func VipsJpegExportParams

func VipsJpegExportParams(width, height int) *vips.JpegExportParams

VipsJpegExportParams returns JPEG image encoding parameters for libvips.

func VipsPngExportParams

func VipsPngExportParams(width, height int) *vips.PngExportParams

VipsPngExportParams returns PNG image encoding parameters for libvips.

func VipsRotate

func VipsRotate(img *vips.ImageRef, orientation int) error

VipsRotate rotates a vips image based on the Exif orientation.

func VipsShutdown

func VipsShutdown()

VipsShutdown shuts down libvips and removes temporary files.

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 ColorSpace

type ColorSpace = string
const (
	ColorNone     ColorSpace = "none"
	ColorAuto     ColorSpace = "auto"
	ColorSRGB     ColorSpace = "srgb"
	ColorPreserve ColorSpace = "preserve"
)

Supported thumbnail color profile settings.

func ParseColor

func ParseColor(name string, lib Lib) ColorSpace

ParseColor returns a ColorSpace based on the config value string and image library.

type Lib

type Lib = string
const (
	LibAuto    Lib = "auto"
	LibVips    Lib = "vips"
	LibImaging Lib = "imaging"
)

Supported image processing libraries.

type Name

type Name string

Name represents a thumbnail size name.

const (
	Colors   Name = "colors"
	Tile50   Name = "tile_50"
	Tile100  Name = "tile_100"
	Left224  Name = "left_224"
	Right224 Name = "right_224"
	Tile224  Name = "tile_224"
	Fit720   Name = "fit_720"
	Tile500  Name = "tile_500"
	Tile1080 Name = "tile_1080"
	Fit1280  Name = "fit_1280"
	Fit1600  Name = "fit_1600"
	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 Options

type Options []ResampleOption

Options represents a list of thumbnail rendering options.

func (Options) Contains

func (o Options) Contains(option ResampleOption) bool

Contains checks if the specified option is set.

type Public

type Public struct {
	Fit720  Thumb `json:"fit_720"`
	Fit1280 Thumb `json:"fit_1280"`
	Fit1920 Thumb `json:"fit_1920"`
	Fit2560 Thumb `json:"fit_2560"`
	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 (
	QualityMax    Quality = 91
	QualityHigh   Quality = 87
	QualityMedium Quality = 83
	QualityLow    Quality = 79
	QualityMin    Quality = 70
)

Standard JPEG image quality levels, see https://docs.photoprism.app/user-guide/settings/advanced/#jpeg-quality

func JpegQuality

func JpegQuality(width, height int) Quality

JpegQuality returns the JPEG image quality depending on the image size.

func JpegQualitySmall

func JpegQualitySmall() Quality

JpegQualitySmall returns the quality for images that should be more heavily compressed.

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) Int

func (q Quality) Int() int

Int returns the quality as int.

func (Quality) String

func (q Quality) String() string

String returns the quality as string.

type ResampleFilter

type ResampleFilter string

ResampleFilter represents a downscaling filter.

const (
	ResampleAuto     ResampleFilter = "auto"
	ResampleBlackman ResampleFilter = "blackman"
	ResampleLanczos  ResampleFilter = "lanczos"
	ResampleCubic    ResampleFilter = "cubic"
	ResampleLinear   ResampleFilter = "linear"
	ResampleNearest  ResampleFilter = "nearest"
)

Supported downscaling filter types.

func ParseFilter

func ParseFilter(name string, lib Lib) ResampleFilter

ParseFilter returns a ResampleFilter based on the config value string and image library.

func (ResampleFilter) Imaging

Imaging returns the downscaling filter for use with the "imaging" library.

func (ResampleFilter) String

func (a ResampleFilter) String() string

String returns the downscaling filter name as string.

func (ResampleFilter) Vips

func (a ResampleFilter) Vips() vips.Kernel

Vips returns the downscaling filter for use with the "govips" library.

type ResampleOption

type ResampleOption int

ResampleOption represents a thumbnail rendering option.

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

type Size

type Size struct {
	Name     Name    `json:"name"`  // Name of the thumbnail size.
	Source   Name    `json:"-"`     // Larger size this size can be generated from.
	Usage    string  `json:"usage"` // Usage example.
	Width    int     `json:"w"`     // Width in pixels.
	Height   int     `json:"h"`     // Height in pixels.
	Public   bool    `json:"-"`     // Size is visible in client applications.
	Fit      bool    `json:"-"`     // Image is fitted to fill this size.
	Optional bool    `json:"-"`     // Size must not be generated by default.
	Required bool    `json:"-"`     // Size must always be generated.
	Options  Options `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 generates a new thumbnail with the requested size, if it does not already exist, and returns its 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 tests if this size can be skipped when generating thumbnails, e.g. because it is larger than the original.

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