tinypng

package
v4.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package tinypng is `tinypng.com` API client implementation.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is a tinypng client implementation.

func NewClient

func NewClient(apiKey string, options ...ClientOption) *Client

NewClient creates a new tinypng client instance. Options can be used to fine client tuning.

func (*Client) Compress

func (c *Client) Compress(ctx context.Context, src io.Reader) (*Compressed, error)

Compress uploads image content from the provided source to the tinypng server for compression. When the process is done, the compression result (just information, not compressed image content) will be returned. If the provided source is also an io.Closer - it will be closed automatically by the HTTP client (if the default HTTP client is used).

Example (And_Download)
package main

import (
	"context"
	"fmt"
	"os"

	_ "embed"
	"gh.tarampamp.am/tinifier/v4/pkg/tinypng"
)

func main() {
	c := tinypng.NewClient("YOUR-API-KEY")

	srcFile, err := os.OpenFile("/tmp/image.png", os.O_RDONLY, 0)
	if err != nil {
		panic(err)
	}
	defer srcFile.Close()

	info, err := c.Compress(context.TODO(), srcFile)
	if err != nil {
		panic(err)
	}

	destFile, err := os.OpenFile("/tmp/image_compressed.png", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
	if err != nil {
		panic(err)
	}
	defer destFile.Close()

	if err = info.Download(context.TODO(), destFile); err != nil {
		panic(err)
	}

	fmt.Printf("%+v\n", info)
}
Output:

func (*Client) SetAPIKey

func (c *Client) SetAPIKey(key string)

SetAPIKey sets the API key for the requests making.

func (*Client) UsedQuota

func (c *Client) UsedQuota(ctx context.Context) (uint64, error)

UsedQuota returns compressions count for current API key (used quota value). By default, for free API keys quota is equals to 500.

type ClientOption

type ClientOption func(*Client)

ClientOption allows to set up some internal client properties from outside.

func WithHTTPClient

func WithHTTPClient(httpClient httpClient) ClientOption

WithHTTPClient setups allows to pass custom HTTP client implementation.

type Compressed

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

Compressed represents tinypng compression result.

func (Compressed) Dimensions

func (c Compressed) Dimensions() (width, height uint32)

Dimensions returns the dimensions of the compressed image.

func (Compressed) Download

func (c Compressed) Download(ctx context.Context, to io.Writer) error

Download image from remote server and write to the passed destination.

If the provided source is also an io.Closer - it will be closed automatically by the HTTP client (if the default HTTP client is used).

func (Compressed) Size

func (c Compressed) Size() uint64

Size returns the size (in bytes) of the compressed image.

func (Compressed) Type

func (c Compressed) Type() string

Type returns the type of the compressed image.

func (Compressed) URL

func (c Compressed) URL() string

URL returns the URL of the compressed image.

func (Compressed) UsedQuota

func (c Compressed) UsedQuota() uint64

UsedQuota returns the used quota value.

type Error

type Error uint8

Error is a special type for package-specific errors.

const (
	ErrTooManyRequests Error = iota + 1 // too many requests (limit has been exceeded)
	ErrUnauthorized                     // unauthorized (invalid credentials)
	ErrBadRequest                       // bad request (empty file or wrong format)
)

Package-specific error constants.

func (Error) Error

func (err Error) Error() string

Error returns error in a string representation.

Jump to

Keyboard shortcuts

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