tinify

package
v0.0.0-...-51e4ec8 Latest Latest
Warning

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

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

README

tinify

tinify is a Go client library for the Tinify API. The library provides an implementation of the Image Compressor interface for Tinify service. The library can be used to compress images using Tinify API.

Usage

package main

import (
	"context"
	"fmt"
	"net/http"
	"os"

	"github.com/moemoe89/go-helpers/pkg/imagecompressor/tinify"
)

func main() {
	// Create a tinify client using an API key.
	client, err := tinify.New(
		// Your API key.
		tinify.WithAPIKey("<API_KEY>"),
		// Optionally customize the http.Client.
		// If not specify, will use http.DefaultClient.
		tinify.WithHTTPClient(
			&http.Client{
				Transport:     nil,
				CheckRedirect: nil,
				Jar:           nil,
				Timeout:       0,
			},
		),
	)
	if err != nil {
		panic(err)
	}

	// Open an image file for compression.
	file, err := os.Open("test.jpg")
	if err != nil {
		panic(err)
	}

	// Compress the image and get the compressed file.
	// filename is optional for tinify,
	// leave it as empty string should be fine.
	compressedFile, err := client.Upload(context.Background(), file, "")
	if err != nil {
		panic(err)
	}

	// Print the URL of the compressed file.
	fmt.Println(compressedFile.URL)
}

Options

The New function accepts the following options:

  • WithAPIKey: sets the API key for Tinify API.
  • WithHTTPClient: sets the HTTPClient interface to make HTTP requests.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(opts ...Option) (imagecompressor.Client, error)

New returns Image Compressor interface implementations.

Types

type Data

type Data struct {
	Input struct {
		Size int    `json:"size"`
		Type string `json:"type"`
	} `json:"input"`
	Output struct {
		Size   int     `json:"size"`
		Type   string  `json:"type"`
		Width  int     `json:"width"`
		Height int     `json:"height"`
		Ratio  float64 `json:"ratio"`
		URL    string  `json:"url"`
	} `json:"output"`
	Error   string `json:"error"`
	Message string `json:"message"`
}

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

type Option

type Option func(t *tinifyClient) error

Option configures tinifyClient.

func WithAPIKey

func WithAPIKey(str string) Option

WithAPIKey returns an option that set the api key.

func WithHTTPClient

func WithHTTPClient(httpClient HTTPClient) Option

WithHTTPClient returns an option that set the http client.

Jump to

Keyboard shortcuts

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