imgutil

package
v0.0.0-...-bedf5c0 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: MPL-2.0 Imports: 32 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CacheAge = 7 * 24 * time.Hour // 7 days cache

CacheAge is the age to keep for all cached images.

View Source
var FFmpegProvider = FFmpegOpts{
	Format:    "jpeg",
	AllowFile: false,
}

FFmpegProvider implements imgutil.Provider and uses FFmpeg to render the image using a given HTTP(S) URL. It supports images, videos and more.

Functions

func AppendURLSize

func AppendURLSize(urlstr string, w, h int) string

AppendURLSize appends into the URL fragments the width and height parameters. Providers that support these fragments will be able to read them.

func AsyncGET

func AsyncGET(ctx context.Context, url string, img ImageSetter)

AsyncGET GETs the given URL and calls f in the main loop. If the context is cancelled by the time GET is done, then f will not be called. If the given URL is nil, then the function does nothing.

This function can be called from any thread. It will synchronize accordingly by itself.

func AsyncGETIcon

func AsyncGETIcon(ctx context.Context, url string, iconFn func(gio.Iconner))

AsyncGETIcon GETs the given URL as a GIcon and calls f in the main loop. If the context is cancelled by the time GET is done, then f will not be called.

func DoProviderURL

func DoProviderURL(ctx context.Context, p Provider, uri string, img ImageSetter)

DoProviderURL invokes a Provider with the given URI string (instead of a *url.URL instance).

func FFmpegThumbnail

func FFmpegThumbnail(ctx context.Context, format, url string) (string, error)

FFmpegThumbnail fetches the thumbnail of the given URL and returns the path to the file. If format is empty, then jpeg is used.

func FetchImageToFile

func FetchImageToFile(ctx context.Context, url string, o Opts) (string, error)

FetchImageToFile fetches an image from the given URL and saves it to the given file. If the image is already cached, then it will be loaded from the cache instead.

func GET

func GET(ctx context.Context, url string, img ImageSetter)

GET gets the given URL into a Paintable.

func IconPaintable

func IconPaintable(name string, w, h int) gdk.Paintabler

IconPaintable gets the icon with the given name and returns the size. Nil is never returned.

func MaxSize

func MaxSize(w, h, maxW, maxH int) (int, int)

MaxSize returns the maximum size that can fit within the given max width and height. Aspect ratio is preserved.

func OptsError

func OptsError(ctx context.Context, err error)

OptsError calls Error on the Opts inside the context. It is a convenient function.

func ParseURLSize

func ParseURLSize(url *url.URL) (w, h int)

ParseURLSize parses the optional width and height fragments from the URL. If the URL has none, then (0, 0) is returned.

func WithOpts

func WithOpts(ctx context.Context, optFuncs ...OptFunc) context.Context

WithOpts injects the given imgutil.OptFunc options into the context. imgutil calls that takes in the returned context will have the given options. Calling WithOpts with a context returned from another WithOpts will make it create a copy that inherits the properties of the top-level Opts.

Types

type FFmpegOpts

type FFmpegOpts struct {
	Format    string // default "jpeg"
	AllowFile bool   // default false
}

FFmpegOpts is the options for FFmpeg.

func (FFmpegOpts) Do

func (p FFmpegOpts) Do(ctx context.Context, url *url.URL, img ImageSetter)

Do implements Provider.

func (FFmpegOpts) Schemes

func (p FFmpegOpts) Schemes() []string

Schemes implements Provider.

type ImageSetter

type ImageSetter struct {
	SetFromPixbuf    func(*gdkpixbuf.Pixbuf)
	SetFromAnimation func(*gdkpixbuf.PixbufAnimation)
	SetFromPaintable func(gdk.Paintabler)
}

ImageSetter contains functions for setting images fetched from a Provider.

func ImageSetterFromImage

func ImageSetterFromImage(img *gtk.Image) ImageSetter

ImageSetterFromImage returns an ImageSetter for a gtk.Image.

func ImageSetterFromPicture

func ImageSetterFromPicture(picture *gtk.Picture) ImageSetter

ImageSetterFromPicture returns an ImageSetter for a gtk.Picture.

type OptFunc

type OptFunc func(*Opts)

OptFunc is a type that can optionally modify the default internal options for each call.

func WithDoneFn

func WithDoneFn(done func(error)) OptFunc

WithDoneFn is like WithErrorFn, except it's called once the routine is done on the main thread with a possibly nil error.

func WithErrorFn

func WithErrorFn(f func(error)) OptFunc

WithErrorFn adds a callback that is called on an error.

func WithFallbackIcon

func WithFallbackIcon(name string) OptFunc

WithFallbackIcon makes image functions use the icon as the image given into the callback instead of a nil one. If name is empty, then dialog-error is used. Note that this function overrides WithErrorFn if it is after.

func WithMaxSize

func WithMaxSize(w, h int) OptFunc

WithMaxSize sets the maximum size of the image. The image will be scaled down to fit the size while respecting its aspect ratio. If the screen is HiDPI, then the size will be scaled up.

func WithRectRescale

func WithRectRescale(size int) OptFunc

WithRectRescale is a convenient function around WithRescale for rectangular or circular images.

func WithRescale deprecated

func WithRescale(w, h int) OptFunc

WithRescale rescales the image to the given max width and height while respecting its aspect ratio. The given sizes will be used as the maximum sizes.

Deprecated: Use WithMaxSize instead.

func WithSizeOverrider

func WithSizeOverrider(widget gtk.Widgetter, w, h int) OptFunc

WithSizeOverrider overrides the widget's size request to be of the given size.

type Opts

type Opts struct {
	// contains filtered or unexported fields
}

func OptsFromContext

func OptsFromContext(ctx context.Context) Opts

OptsFromContext gets the Opts from the given context. If there is none, then a zero-value instance is returned.

func (*Opts) Error

func (o *Opts) Error(err error)

Error triggers the error handler inside OptFunc if there's one. Otherwise, an error is logged down. This is useful for asynchronous imgutil function wrappers to signal an error.

func (*Opts) Size

func (o *Opts) Size() (w, h int)

Size returns the requested size from the Opts or (0, 0) if there is none.

type Provider

type Provider interface {
	Schemes() []string
	Do(ctx context.Context, url *url.URL, img ImageSetter)
}

Provider describes a universal resource provider.

var FileProvider Provider = fileProvider{}

FileProvider is the universal resource provider for a file (file://).

var HTTPProvider Provider = httpProvider{}

HTTPProvider is the universal resource provider that handles HTTP and HTTPS schemes (http:// and https://).

type Providers

type Providers map[string]Provider

Providers holds multiple providers. A Providers instance is also a Provider in itself.

func NewProviders

func NewProviders(providers ...Provider) Providers

NewProviders creates a new Providers instance. Providers that are put last can override schemes of providers put before.

func (Providers) Do

func (p Providers) Do(ctx context.Context, url *url.URL, img ImageSetter)

Do invokes any of the providers inside.

func (Providers) Schemes

func (p Providers) Schemes() []string

Schemes returns all schemes within the Providers. It exists only to implement Provider and generally shouldn't be used. The returned list is always sorted.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL