Documentation ¶
Index ¶
- Constants
- Variables
- func ClearCache() error
- func DeleteImageTx(ctx context.Context, tx *sql.Tx, db *sql.DB, image uid.ID) error
- func ImageColumns(tableAlias string) []string
- func ImageContainSize(imageWidth, imageHeight, boxWidth, boxHeight int) (int, int)
- func ImagePath(id uid.ID) string
- func ImageRecordColumns() []string
- func SaveImageTx(ctx context.Context, tx *sql.Tx, storeName string, file []byte, ...) (uid.ID, error)
- func SetImagesRootFolder(p string)
- type Image
- type ImageCopy
- type ImageFit
- type ImageFormat
- type ImageOptions
- type ImageRecord
- type ImageSize
- type RGB
- type Server
Constants ¶
const ( ImageFormatJPEG = ImageFormat("jpeg") ImageFormatWEBP = ImageFormat("webp") ImageFormatPNG = ImageFormat("png") )
List of image formats.
const ( // ImageFitCover covers the given container with the image. The resulting // image may be shrunken, enlarged, and/or cropped. ImageFitCover = ImageFit("cover") // ImageFitContain fits the image in the container without either enlarging // the image or cropping it. ImageFitContain = ImageFit("contain") ImageFitDefault = ImageFitContain )
There are the valid ImageFit values.
Variables ¶
var ( // HMACKey is the key used to set the HMAC portion of an image's URL. HMACKey []byte // FullImageURL takes in a partial url (a pathname with a set of query // parameters) and it should return a more complete url. This variable may // be nil. FullImageURL = func(s string) string { return "/images/" + s } )
var ( ErrImageNotFound = errors.New("image not found") ErrStoreNotRegistered = errors.New("store not registered") ErrBadURL = errors.New("bad image request url") ErrImageFormatUnsupported = errors.New("image format not supported") ErrImageFitUnsupported = errors.New("invalid image fit") )
var SkipProcessing = false
If SkipProcessing is set to true, images are saved as is, without compressing nor changing their size or format. Warning: This may lead to inadvertently storing (and leaking) image metadata.
Functions ¶
func DeleteImageTx ¶
func ImageColumns ¶
ImageColumns returns a list of columns of the images table (not all of them) for when selecting using and outer join. Use Image.ScanDestinations in conjunction with this function.
func ImageContainSize ¶
ImageContainSize returns the width and height of an image as it would fit into a box, while keeping the aspect ratio of the image as it was.
func ImagePath ¶
ImagePath returns the relative path of where an image is stored on disk (without the filename extension).
func ImageRecordColumns ¶
func ImageRecordColumns() []string
ImageRecordColumns returns the list of columns of the images table. Use this function in conjuction with ImageRecordScanDestinations when selecting SQL joins.
func SaveImageTx ¶
func SetImagesRootFolder ¶
func SetImagesRootFolder(p string)
SetImagesRootFolder sets the folder in which images are saved (for the disk store) and where cache files are stored. Make sure to call this function. The path p must be an absolute path.
Types ¶
type Image ¶
type Image struct { ID *uid.ID `json:"id"` Format *ImageFormat `json:"format"` MimeType *string `json:"mimetype"` Width *int `json:"width"` Height *int `json:"height"` Size *int `json:"size"` AverageColor *RGB `json:"averageColor"` URL *string `json:"url"` Copies []*ImageCopy `json:"copies"` }
Image is an image that's to be sent to the client. It is to be derived from an ImageRecord.
func NewImage ¶
func NewImage() *Image
NewImage returns an Image with all pointer fields allocated and set to zero values.
func (*Image) AppendCopy ¶
func (m *Image) AppendCopy(name string, boxWidth, boxHeight int, fit ImageFit, format ImageFormat) *ImageCopy
AppendCopy is a helper function that appends an ImageCopy to m.Copies slice. If format is zero, m.Format is used.
func (*Image) PostScan ¶
func (m *Image) PostScan()
PostScan is to be called after m's fields are scanned from the database. It sets fields of m that are derived from database values (like m.URL).
func (*Image) ScanDestinations ¶
type ImageCopy ¶
type ImageCopy struct { ImageID uid.ID `json:"-"` Name string `json:"name,omitempty"` // To identify a copy. Width int `json:"width"` // Real width of the image. Height int `json:"height"` // Real height of the image. BoxWidth int `json:"boxWidth"` // Width of the box the image fits into (for Format == ImageFitContain) BoxHeight int `json:"boxHeight"` // Height of the box the image fits into (for Format == ImageFitContain) Fit ImageFit `json:"objectFit"` Format ImageFormat `json:"format"` URL string `json:"url"` }
An ImageCopy is a transformed (size, format, and/or fit changed) copy of an Image.
type ImageFit ¶
type ImageFit string
ImageFit denotes how an image is to be fitted into a rectangle.
type ImageFormat ¶
type ImageFormat string
ImageFormat represents the type of image.
func (ImageFormat) BIMGType ¶
func (f ImageFormat) BIMGType() (t bimg.ImageType, err error)
BIMGType converts f into its matching bimg.ImageType value.
func (ImageFormat) Extension ¶
func (f ImageFormat) Extension() string
func (ImageFormat) Valid ¶
func (f ImageFormat) Valid() bool
Valid reports whether f is supported by the image package.
type ImageOptions ¶
type ImageOptions struct {
Width, Height int
Format ImageFormat
Fit ImageFit
}
ImageOptions hold optional arguments to SaveImage.
type ImageRecord ¶
type ImageRecord struct { ID uid.ID `json:"id"` StoreName string `json:"storeName"` StoreMetadata map[string]any `json:"storeMetadata"` Format ImageFormat `json:"format"` Width int `json:"width"` Height int `json:"height"` Size int `json:"size"` UploadSize int `json:"uploadSize"` AverageColor RGB `json:"averageColor"` CreatedAt time.Time `json:"createdAt"` DeletedAt *time.Time `json:"deletedAt"` // contains filtered or unexported fields }
ImageRecord is a database row of an image item.
Table name: images.
func GetImageRecord ¶
GetImageRecords returns an image record. If no image was found it returns ErrImageNotFound.
func GetImageRecords ¶
GetImageRecords returns a slice of image records. If no images were found it returns ErrImageNotFound.
func SaveImage ¶
func SaveImage(ctx context.Context, db *sql.DB, storeName string, file []byte, opts *ImageOptions) (*ImageRecord, error)
SaveImage saves the provided image in the image store with the name storeName and creates a row in the images table. The argument opts can be nil, in which case default values are used.
func (*ImageRecord) Image ¶
func (r *ImageRecord) Image() *Image
func (*ImageRecord) ScanDestinations ¶
func (r *ImageRecord) ScanDestinations() []any
ScanDestinations returns a slice of pointers that sql.Rows.Scan can populate. The pointers match the column names returned by ImageRecordSelectColumns.
func (*ImageRecord) StoreExists ¶
func (r *ImageRecord) StoreExists() bool
func (*ImageRecord) UnmarshalMetadataJSON ¶
func (r *ImageRecord) UnmarshalMetadataJSON() error
UnmarshalMetadataJSON allocates r.StoreMetadata and fills it with JSON metadata fetched from the database.
type ImageSize ¶
type ImageSize struct {
Width, Height int
}
ImageSize represents the size of an image.
func (ImageSize) MarshalText ¶
MarshalText implements encoding.TextMarshaler interface. Output is the same as String method.
func (ImageSize) String ¶
String returns, for example, "400" if width and height are both 400px, and "400x600" if width is 400px and height is 600px.
func (*ImageSize) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler interface.
type RGB ¶
RGB represents color values of range (0, 255). It implements sql.Scanner and driver.Valuer interfaces. Use a 12-byte binary database column type to store values of this type in SQL databases.
func AverageColor ¶
AverageColor returns the average RGB color of img by averaging the colors of at most 10^4 pixels.
func (RGB) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*RGB) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*RGB) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler interface.