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
- Variables
- func Create(img image.Image, fileName string, width, height int, opts ...ResampleOption) (result image.Image, err error)
- func FileName(hash, thumbPath string, width, height int, opts ...ResampleOption) (fileName string, err error)
- func Find(limit int) (name Name, size Size)
- func FromCache(imageFilename, hash, thumbPath string, width, height int, ...) (fileName string, err error)
- func FromFile(imageName, hash, thumbPath string, width, height, orientation int, ...) (fileName string, err error)
- func Init(availableMemory uint64, maxWorkers int, imgLib string)
- func InvalidSize(size int) bool
- func Jpeg(srcFile, jpgFile string, orientation int) (img image.Image, err error)
- func MaxSize() int
- func Open(fileName string, orientation int) (result image.Image, err error)
- func OpenJpeg(fileName string, orientation int) (image.Image, error)
- func Png(srcFile, pngFile string, orientation int) (img image.Image, err error)
- func Report(sizes SizeList, short bool) (rows [][]string, cols []string)
- func Resample(img image.Image, width, height int, opts ...ResampleOption) image.Image
- func ResampleOptions(opts ...ResampleOption) (method ResampleOption, filter ResampleFilter, format fs.Type)
- func ResolvedName(hash, thumbPath string, width, height int, opts ...ResampleOption) (fileName string, err error)
- func Rotate(img image.Image, o int) image.Image
- func Shutdown()
- func Skip(s Size, bounds image.Rectangle) bool
- func Suffix(width, height int, opts ...ResampleOption) (result string)
- func Url(h, name, contentUri, previewToken string) string
- func Vips(imageName string, imageBuffer []byte, hash, thumbPath string, ...) (thumbName string, thumbBuffer []byte, err error)
- func VipsImportParams() *vips.ImportParams
- func VipsInit()
- func VipsJpegExportParams(width, height int) *vips.JpegExportParams
- func VipsPngExportParams(width, height int) *vips.PngExportParams
- func VipsRotate(img *vips.ImageRef, orientation int) error
- func VipsShutdown()
- type Bytes
- type ColorSpace
- type Lib
- type Name
- type Options
- type Public
- type Quality
- type ResampleFilter
- type ResampleOption
- type Size
- func (s Size) Bounds() image.Rectangle
- func (s Size) Create(img image.Image, fileName string) (image.Image, error)
- func (s Size) ExceedsLimit() bool
- func (s Size) FileName(hash, thumbPath string) (string, error)
- func (s Size) FromCache(fileName, fileHash, cachePath string) (string, error)
- func (s Size) FromFile(fileName, fileHash, cachePath string, fileOrientation int) (string, error)
- func (s Size) ResolvedName(hash, thumbPath string) (string, error)
- func (s Size) Skip(img image.Image) bool
- func (s Size) Uncached() bool
- type SizeList
- type SizeMap
- type Thumb
Constants ¶
const ( MiB = 1024 * 1024 GiB = 1024 * MiB DefaultCacheMem = 128 * MiB DefaultCacheSize = 128 DefaultCacheFiles = 16 DefaultWorkers = 1 )
const ( KB = 1024 MB = KB * 1024 GB = MB * 1024 )
Byte size factors.
const ( OrientationUnspecified int = 0 OrientationNormal = 1 OrientationFlipH = 2 OrientationRotate180 = 3 OrientationFlipV = 4 OrientationTranspose = 5 OrientationRotate270 = 6 OrientationTransverse = 7 OrientationRotate90 = 8 )
Variables ¶
var ( MaxCacheMem = DefaultCacheMem MaxCacheSize = DefaultCacheSize MaxCacheFiles = DefaultCacheFiles NumWorkers = DefaultWorkers )
var ( SizeCached = SizeFit1920.Width SizeOnDemand = SizeFit7680.Width )
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}} )
var (
CachePublic = false
)
var Color = ColorAuto
Color sets the standard color profile for thumbnails.
var (
ErrNotCached = errors.New("not cached")
)
var Filter = ResampleLanczos
Filter specifies the default downscaling filter.
var FitSizes = SizeList{ Sizes[Fit7680], Sizes[Fit4096], Sizes[Fit2560], Sizes[Fit1920], Sizes[Fit1280], Sizes[Fit720], }
FitSizes contains "fit" cropped thumbnail sizes from largest to smallest. Best for the viewer as proportional resizing maintains the aspect ratio.
var JpegQualityDefault = QualityMedium
JpegQualityDefault sets the compression level of newly created JPEGs.
var Library = LibImaging
Library specifies the image library to be used.
var Names = []Name{ Fit7680, Fit4096, Fit2560, Fit1920, Fit1280, Tile500, Fit720, Tile224, Right224, Left224, Tile100, Tile50, Colors, }
Names contains all default size names.
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.
var ResampleMethods = map[ResampleOption]string{ ResampleFillCenter: "center", ResampleFillTopLeft: "left", ResampleFillBottomRight: "right", ResampleFit: "fit", ResampleResize: "resize", }
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.
var VideoSizes = SizeList{ Sizes[Fit7680], Sizes[Fit4096], Sizes[Fit3840], Sizes[Fit2560], Sizes[Fit2048], Sizes[Fit1920], Sizes[Fit1280], Sizes[Fit720], }
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 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 ¶
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 ¶
InvalidSize tests if the size in pixels is invalid.
func OpenJpeg ¶
OpenJpeg loads a JPEG image from disk, rotates it, and converts the color profile if necessary.
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 Skip ¶
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 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 ¶
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.
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 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.
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 ¶
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 ¶
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.
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 ¶
func (a ResampleFilter) Imaging() imaging.ResampleFilter
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 (Size) Create ¶
Create creates a thumbnail with the matching size and returns it as image.Image.
func (Size) ExceedsLimit ¶
ExceedsLimit tests if thumbnail type is too large, and can not be rendered at all.
func (Size) FromCache ¶
FromCache returns the filename if a thumbnail image with the matching size is in the cache.
func (Size) FromFile ¶
FromFile generates a new thumbnail with the requested size, if it does not already exist, and returns its filename.
func (Size) ResolvedName ¶
ResolvedName returns the file name of the thumbnail for the matching size with all symlinks resolved.