Documentation
¶
Index ¶
- Constants
- Variables
- func DoublePath(relativePath string) string
- func SplitDouble(relativePath string) (bool, string)
- type DerivativeCaches
- type FocalPoint
- type FocalPoints
- type Format
- type ImageProcessor
- type ImageStorageRepository
- type ImageStyle
- func (s *ImageStyle) Anchor() imaging.Anchor
- func (s *ImageStyle) Description() string
- func (s *ImageStyle) Double() (*ImageStyle, bool)
- func (s *ImageStyle) Format() Format
- func (s *ImageStyle) Height() int
- func (s *ImageStyle) HiDPI() bool
- func (s *ImageStyle) Label() string
- func (s *ImageStyle) Mode() ResizeMode
- func (s *ImageStyle) Name() string
- func (s *ImageStyle) Quality() int
- func (s *ImageStyle) SetDescription(desc string)
- func (s *ImageStyle) SetFocal(on bool)
- func (s *ImageStyle) SetFormat(f Format)
- func (s *ImageStyle) SetHiDPI(on bool)
- func (s *ImageStyle) UsesFocal() bool
- func (s *ImageStyle) Width() int
- type ImageStyleRepository
- type ImagingProcessor
- type ProcessedImage
- type Repositories
- type ResizeMode
- type ResizeService
- func (s *ResizeService) GetOriginalDimensions(originalPath string) (width, height int, err error)
- func (s *ResizeService) GetResizedImageURL(originalPath string, width, height int) (string, error)
- func (s *ResizeService) OriginalURL(originalPath string) string
- func (s *ResizeService) URLPrefix() string
- type StorageRepository
- func (r *StorageRepository) ClearAllCache(ctx context.Context) error
- func (r *StorageRepository) ClearAllGeneratedImages(ctx context.Context) error
- func (r *StorageRepository) ClearResizeDir(ctx context.Context) error
- func (r *StorageRepository) ClearStyleCache(ctx context.Context, styleName string) error
- func (r *StorageRepository) OpenCached(ctx context.Context, styleName, relativePath string) (filestore.File, filestore.Info, error)
- func (r *StorageRepository) OpenExact(ctx context.Context, key string) (filestore.File, filestore.Info, error)
- func (r *StorageRepository) OpenOriginal(ctx context.Context, relativePath string) (filestore.File, filestore.Info, error)
- func (r *StorageRepository) SaveToCache(ctx context.Context, styleName, relativePath string, img ProcessedImage) error
- type StyleService
- func (s *StyleService) GetStyledImage(ctx context.Context, styleName, relativePath string) (filestore.File, filestore.Info, error)
- func (s *StyleService) GetStyledOriginal(ctx context.Context, styleName string, double bool, key string) (filestore.File, filestore.Info, error)
- func (s *StyleService) SetFocalPoints(f FocalPoints)
Constants ¶
const ( MaxSourceSide = 20000 MaxSourcePixels = 50_000_000 )
The largest source the processor decodes: a decoded image takes 4 bytes a pixel, and a small file may claim any size in its header.
const ( ThumbnailContentType = "image/webp" ThumbnailExtension = ".webp" )
Thumbnail format for generated resize/ and styles/ derivatives.
const DoubleDir = "@2x"
DoubleDir - the folder of a style's 2x derivatives under its own: styles/<style>/@2x/<path>. No file path starts with it (the paths of uploads are transliterated to letters, digits, - and _).
Variables ¶
var ErrNotRenderable = errors.New("the source can't be rendered")
ErrNotRenderable - the source isn't an image the processor decodes (another file, a broken one, one too large).
var ErrOriginalNotFound = errors.New("original image not found")
ErrOriginalNotFound - the source image of a requested derivative isn't stored.
var ErrStyleNotFound = errors.New("image style not found")
ErrStyleNotFound - no image style with the requested name.
Functions ¶
func DoublePath ¶
DoublePath - relativePath of the 2x derivative under its style's folder.
func SplitDouble ¶
SplitDouble - whether relativePath is a DoublePath, and the path without it.
Types ¶
type DerivativeCaches ¶
type DerivativeCaches interface {
ClearStyleCache(ctx context.Context, styleName string) error
ClearAllGeneratedImages(ctx context.Context) error
}
DerivativeCaches - clearing the derivatives: a style's (its settings changed), or all of them.
type FocalPoint ¶
type FocalPoint struct {
X, Y float64
}
FocalPoint - a point of an image, as a fraction of its width (X) and height (Y) from the top left, 0-1.
type FocalPoints ¶
type FocalPoints interface {
FocalPoint(ctx context.Context, key string) (*FocalPoint, error)
}
FocalPoints - the focal points of the originals, by their keys in the service's store (nil - none: not found, or none set).
type Format ¶
type Format string
Format - the encoding of a style's derivatives.
func (Format) ContentType ¶
ContentType - the derivative MIME type.
type ImageProcessor ¶
type ImageProcessor interface {
Process(src io.Reader, sourceName string, style *ImageStyle, anchor imaging.Anchor, focal *FocalPoint) (ProcessedImage, error)
}
ImageProcessor renders a derivative of a source image (read from src; sourceName - its file name, for its format) for a style; a style cropping (fill) keeps focal (nil - none) in the picture, else crops at anchor.
type ImageStorageRepository ¶
type ImageStorageRepository interface {
OpenOriginal(ctx context.Context, relativePath string) (filestore.File, filestore.Info, error)
// OpenExact opens the file at key itself, no derivative's extension
// mapped to the original's.
OpenExact(ctx context.Context, key string) (filestore.File, filestore.Info, error)
OpenCached(ctx context.Context, styleName, relativePath string) (filestore.File, filestore.Info, error)
SaveToCache(ctx context.Context, styleName, relativePath string, img ProcessedImage) error
ClearStyleCache(ctx context.Context, styleName string) error
ClearAllCache(ctx context.Context) error
ClearResizeDir(ctx context.Context) error
ClearAllGeneratedImages(ctx context.Context) error
}
ImageStorageRepository finds originals and keeps the derivatives (StorageRepository: in the public files' store).
type ImageStyle ¶
type ImageStyle struct {
// contains filtered or unexported fields
}
ImageStyle - a named image derivative preset (size, resize mode, anchor, quality, format).
func NewImageStyle ¶
func NewImageStyle(name, label string, width, height int, mode ResizeMode, anchor imaging.Anchor, quality int) (*ImageStyle, error)
NewImageStyle validates the parameters and creates a style; an invalid quality defaults to 85.
func (*ImageStyle) Anchor ¶
func (s *ImageStyle) Anchor() imaging.Anchor
Anchor returns where ResizeModeFill crops without a focal point.
func (*ImageStyle) Description ¶
func (s *ImageStyle) Description() string
Description returns the admin's note on the style.
func (*ImageStyle) Double ¶
func (s *ImageStyle) Double() (*ImageStyle, bool)
Double - the style at double size (a HiDPI style's 2x derivatives); false for a style without them.
func (*ImageStyle) Format ¶
func (s *ImageStyle) Format() Format
Format - the derivative encoding (FormatWebP unless set).
func (*ImageStyle) Height ¶
func (s *ImageStyle) Height() int
Height returns the derivative's height in pixels; 0 - by the aspect ratio.
func (*ImageStyle) HiDPI ¶
func (s *ImageStyle) HiDPI() bool
HiDPI - whether its derivatives are made at double size too, for high-density screens (srcset 2x, DoublePath).
func (*ImageStyle) Label ¶
func (s *ImageStyle) Label() string
Label returns the style's name shown in the admin.
func (*ImageStyle) Mode ¶
func (s *ImageStyle) Mode() ResizeMode
Mode returns how the image is fitted into the size.
func (*ImageStyle) Quality ¶
func (s *ImageStyle) Quality() int
Quality returns the encoding's quality, 1-100.
func (*ImageStyle) SetDescription ¶
func (s *ImageStyle) SetDescription(desc string)
SetDescription sets the admin's note on the style.
func (*ImageStyle) SetFocal ¶
func (s *ImageStyle) SetFocal(on bool)
SetFocal sets whether the style crops at the focal point.
func (*ImageStyle) SetFormat ¶
func (s *ImageStyle) SetFormat(f Format)
SetFormat sets the derivative encoding (ParseFormat: anything but JPEG is WebP).
func (*ImageStyle) SetHiDPI ¶
func (s *ImageStyle) SetHiDPI(on bool)
SetHiDPI sets whether the derivatives are made at double size too.
func (*ImageStyle) UsesFocal ¶
func (s *ImageStyle) UsesFocal() bool
UsesFocal - whether it crops at the image's focal point (FocalPoint; at Anchor when the image has none).
func (*ImageStyle) Width ¶
func (s *ImageStyle) Width() int
Width returns the derivative's width in pixels.
type ImageStyleRepository ¶
type ImageStyleRepository interface {
FindByName(name string) (*ImageStyle, error)
}
ImageStyleRepository finds the image styles by name.
type ImagingProcessor ¶
type ImagingProcessor struct {
// contains filtered or unexported fields
}
ImagingProcessor implements ImageProcessor with the imaging library.
func NewImagingProcessor ¶
func NewImagingProcessor() *ImagingProcessor
NewImagingProcessor - an ImagingProcessor rendering renderSlots images at a time.
func (*ImagingProcessor) Process ¶
func (p *ImagingProcessor) Process(r io.Reader, sourceName string, style *ImageStyle, anchor imaging.Anchor, focal *FocalPoint) (ProcessedImage, error)
Process renders the derivative; ErrNotRenderable when the source isn't an image it decodes or is larger than MaxSourceSide/MaxSourcePixels.
type ProcessedImage ¶
ProcessedImage - an encoded derivative.
func NewProcessedImage ¶
func NewProcessedImage(data []byte) ProcessedImage
NewProcessedImage wraps encoded WebP bytes.
type Repositories ¶
type Repositories []ImageStorageRepository
Repositories clears the derivatives of every storage repository it holds (the public files', the private files').
func (Repositories) ClearAllGeneratedImages ¶
func (rs Repositories) ClearAllGeneratedImages(ctx context.Context) error
ClearAllGeneratedImages removes every derivative from every repository.
func (Repositories) ClearStyleCache ¶
func (rs Repositories) ClearStyleCache(ctx context.Context, styleName string) error
ClearStyleCache removes the derivatives of the style styleName from every repository.
type ResizeMode ¶
type ResizeMode string
ResizeMode - how a style fits an image into its width and height (a height of 0 keeps the aspect ratio in every mode).
const ( // ResizeModeFill - the exact size, the rest cropped at the anchor or // the focal point. ResizeModeFill ResizeMode = "fill" // ResizeModeFit - within the size, the aspect ratio kept. ResizeModeFit ResizeMode = "fit" // ResizeModeResize - the exact size, the aspect ratio not kept. ResizeModeResize ResizeMode = "resize" )
The resize modes.
type ResizeService ¶
type ResizeService struct {
// contains filtered or unexported fields
}
ResizeService generates WxH image previews, with the size in the file name, in the public files' store.
func NewResizeService ¶
func NewResizeService(processor ImageProcessor, store filestore.Store, urlPrefix, storeURL string) *ResizeService
NewResizeService - previews of the files of store, which the site serves at urlPrefix; storeURL - the store's own address for them, when it serves them itself ("" - none).
func (*ResizeService) GetOriginalDimensions ¶
func (s *ResizeService) GetOriginalDimensions(originalPath string) (width, height int, err error)
GetOriginalDimensions returns the original image's size (e.g. for data-pswp-width/data-pswp-height).
func (*ResizeService) GetResizedImageURL ¶
func (s *ResizeService) GetResizedImageURL(originalPath string, width, height int) (string, error)
GetResizedImageURL returns the preview URL for originalPath ({urlPrefix}images/foo.jpg) at width x height, generating the file if needed: {urlPrefix}resize/images/foo-180x275.webp (at storeURL when the store serves its files itself: the preview exists by then). Called while a page renders, without its context.
func (*ResizeService) OriginalURL ¶
func (s *ResizeService) OriginalURL(originalPath string) string
OriginalURL - the address the file at originalPath (under urlPrefix) is served at; originalPath itself when it isn't one of the files.
func (*ResizeService) URLPrefix ¶
func (s *ResizeService) URLPrefix() string
URLPrefix - the public URL prefix of the files directory.
type StorageRepository ¶
type StorageRepository struct {
// contains filtered or unexported fields
}
StorageRepository keeps the derivatives beside the originals in the public files' store: styles/<style>/<path> and resize/<path>.
func NewStorageRepository ¶
func NewStorageRepository(store filestore.Store) *StorageRepository
NewStorageRepository - a StorageRepository keeping the derivatives in store.
func (*StorageRepository) ClearAllCache ¶
func (r *StorageRepository) ClearAllCache(ctx context.Context) error
ClearAllCache removes all image-style derivatives (styles/).
func (*StorageRepository) ClearAllGeneratedImages ¶
func (r *StorageRepository) ClearAllGeneratedImages(ctx context.Context) error
ClearAllGeneratedImages clears the resize previews and all style derivatives.
func (*StorageRepository) ClearResizeDir ¶
func (r *StorageRepository) ClearResizeDir(ctx context.Context) error
ClearResizeDir removes the generated WxH previews under resize/.
func (*StorageRepository) ClearStyleCache ¶
func (r *StorageRepository) ClearStyleCache(ctx context.Context, styleName string) error
ClearStyleCache removes a style's derivatives (styles/<style>).
func (*StorageRepository) OpenCached ¶
func (r *StorageRepository) OpenCached(ctx context.Context, styleName, relativePath string) (filestore.File, filestore.Info, error)
OpenCached opens the style's derivative of relativePath; filestore.ErrNotExist if it hasn't been made.
func (*StorageRepository) OpenExact ¶
func (r *StorageRepository) OpenExact(ctx context.Context, key string) (filestore.File, filestore.Info, error)
OpenExact opens the file at key itself (no extension candidates); ErrOriginalNotFound if there is none.
func (*StorageRepository) OpenOriginal ¶
func (r *StorageRepository) OpenOriginal(ctx context.Context, relativePath string) (filestore.File, filestore.Info, error)
OpenOriginal opens the original for relativePath (images/foo.jpg, or a derivative's path such as …/foo.webp); ErrOriginalNotFound if there is none.
func (*StorageRepository) SaveToCache ¶
func (r *StorageRepository) SaveToCache(ctx context.Context, styleName, relativePath string, img ProcessedImage) error
SaveToCache stores the style's derivative of relativePath.
type StyleService ¶
type StyleService struct {
// contains filtered or unexported fields
}
StyleService serves image style derivatives, generating and caching them on demand.
func NewStyleService ¶
func NewStyleService( styleRepo ImageStyleRepository, storageRepo ImageStorageRepository, processor ImageProcessor, ) *StyleService
NewStyleService - a StyleService with the styles from styleRepo, the derivatives kept by storageRepo and rendered by processor.
func (*StyleService) GetStyledImage ¶
func (s *StyleService) GetStyledImage(ctx context.Context, styleName, relativePath string) (filestore.File, filestore.Info, error)
GetStyledImage opens the derivative of relativePath for styleName, generating it first if needed; relativePath may be a DoublePath (the 2x derivative of a HiDPI style; of another - ErrStyleNotFound). The caller closes the file.
func (*StyleService) GetStyledOriginal ¶
func (s *StyleService) GetStyledOriginal(ctx context.Context, styleName string, double bool, key string) (filestore.File, filestore.Info, error)
GetStyledOriginal opens styleName's derivative of the original at key exactly - never another file whose name maps to the same derivative's (a.png and a.jpg both to a.webp): it is cached under the original's whole name, <key><format's extension> (double - the 2x one of a HiDPI style, under DoublePath). For files whose access is checked by their key (the private files). The caller closes the file.
func (*StyleService) SetFocalPoints ¶
func (s *StyleService) SetFocalPoints(f FocalPoints)
SetFocalPoints - where a style cropping at the focal point (ImageStyle.UsesFocal) finds it; none - at the style's anchor.