Documentation
¶
Index ¶
Constants ¶
const ( ImageTypeJPEG = ImageType("jpeg") ImageTypeWEBP = ImageType("webp") )
List of image types.
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 )
Valid ImageFit values.
const ( // MaxImagesPerFolder sets the maximum number of image files (without // counting copies) that can be saved in one folder. MaxImagesPerFolder = 1000 )
Variables ¶
var ( ErrInvalidImageFit = errors.New("invalid image fit") ErrUnsupportedImage = errors.New("unsupported image format") )
Errors.
var (
ErrNoDefaultImage = errors.New("no default image was provided")
)
Errors.
Functions ¶
func ContainInResolution ¶
ContainInResolution returns width and height as they fit into an image of size w and h. Aspect ratio is not changed.
func GetImageSize ¶
GetImageSize returns the size of image.
func RunMigrations ¶
RunMigrations runs all the migrations in migrations folder.
Types ¶
type Config ¶
type Config struct {
// MariaDB connection.
Database struct {
User string `json:"user"`
Password string `json:"password"`
Database string `json:"database"`
} `json:"database"`
// Address to listen on.
Addr string `json:"addr"`
// All images are saved inside subfolders in this directory.
RootUploadsDir string `json:"rootUploadsDir"`
// Deleted images are moved here. If DeletedDir is empty, the images are
// deleted.
DeletedDir string `json:"deletedDir"`
// Requests with images larger than this would be discarded.
MaxUploadSize int `json:"maxUploadSize"`
}
Config is the configuration for the HTTP server.
func UnmarshalConfigFile ¶
UnmarshalConfigFile reads the config in file and returns it. In case no such file is found, it returns a default config.
type DBImage ¶
type DBImage struct {
ID luid.ID `json:"id"`
FolderID int `json:"folderId"`
// Always JPEG for now.
Type ImageType `json:"type"`
// Actual width of image.
Width int `json:"width"`
// Actual height of image.
Height int `json:"height"`
// This is the MaxWidth that was provided as an argument
// to addImage API call.
MaxWidth int `json:"maxWidth"`
MaxHeight int `json:"maxHeight"`
// Size of image in bytes.
Size int `json:"size"`
// Size of original uploaded image in bytes.
UploadedSize int `json:"-"`
AverageColor RGB `json:"averageColor"`
// Copies are stored on disk (in appropriate folders) with filename
// {ID}_{MaxWidth}_{MaxHeight}_{ImageFit}.jpg Copies may be nil.
Copies []*ImageCopy `json:"copies"`
CreatedAt time.Time `json:"createdAt"`
IsDeleted bool `json:"deleted"`
DeletedAt *time.Time `json:"deletedAt,omitempty"`
// URL pathname of the image. Is of the format /images/{FolderID}/{ID}.jpg.
URL string `json:"url,omitempty"`
// URL pathnames of the image and all its copies.
URLs []string `json:"urls,omitempty"`
}
DBImage is a record in the images table.
func DeleteImage ¶
DeleteImage sets is_deleted field of images to true. If deletedDir is non-empty, images are moved to that directory. Otherwise they are deleted.
func SaveImage ¶
SaveImage saves the image in buf to disk (in a folder inside rootDir) and creates a record in images table. It also creates and stores copies of the image.
All images are saved as JPEGs (for now).
func (*DBImage) GenerateURLs ¶
func (i *DBImage) GenerateURLs()
GenerateURLs populates i.URL and i.URLs fields.
type ImageCopy ¶
type ImageCopy struct {
Width int `json:"w"`
Height int `json:"h"`
MaxWidth int `json:"mw"`
MaxHeight int `json:"mh"`
ImageFit ImageFit `json:"if"`
// Size of image in bytes.
Size int `json:"s"`
}
ImageCopy is a copy of an image.
type ImageFit ¶
type ImageFit string
ImageFit denotes how an image is to be fitted into a rectangle.
func (*ImageFit) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler interface.
type ImageSize ¶
type ImageSize struct {
Width, Height int
}
ImageSize represents the size of an image.
func ToJPEG ¶
ToJPEG converts the image to a JPEG, if it's not already, and fits the image into maxWidth and maxHeight according to fit.
func (ImageSize) MarshalText ¶
MarshalText implements encoding.TextMarshaler interface. Output 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. It does the reverse of what MarshalText and String do.
type RGB ¶
RGB represents color values of range (0, 255).
func AverageColor ¶
AverageColor returns the average RGB color of img by averaging the colors of at most 10000 pixels. Each RGB value is in the range of (0,255).
type SaveImageArg ¶
type SaveImageArg struct {
MaxWidth int `json:"maxWidth"`
MaxHeight int `json:"maxHeight"`
ImageFit ImageFit `json:"imageFit"`
// If true, this is no longer a copy but the default, or the original,
// image. There can be only one default copy per image (if multiple
// arguments are provided as being default the first one is selected and
// others are discarded).
IsDefault bool `json:"default"`
}
SaveImageArg is an argument to SaveImage. It describes how a copy of the saving image is to be created.