resmush

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: 9 Imported by: 0

README

resmush

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

Usage

package main

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

	"github.com/moemoe89/go-helpers/imagecompressor/resmush"
)

func main() {
	// Create a resmush client.
	client, err := resmush.New(
		// Optionally customize the http.Client.
		// If not specify, will use http.DefaultClient.
		resmush.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 required for reSmush,
	// need to fill the filename with the extension.
	compressedFile, err := client.Upload(context.Background(), file, "test.jpg")
	if err != nil {
		panic(err)
	}

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

Options

The New function accepts the following options:

  • 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 {
	Src       string `json:"src"`
	Dest      string `json:"dest"`
	SrcSize   int    `json:"src_size"`
	DestSize  int    `json:"dest_size"`
	Percent   int    `json:"percent"`
	Output    string `json:"output"`
	Expires   string `json:"expires"`
	Generator string `json:"generator"`
	Error     int    `json:"error"`
	ErrorLong string `json:"error_long"`
}

type HTTPClient

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

type Option

type Option func(r *reSmushClient) error

Option configures reSmush.

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