images

package
v6.8.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2024 License: Apache-2.0 Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Images

The images package provides methods for working with images.

ImageCropper

ImageCropper is a service designed to crop images. There are a couple of ways to perform a crop. The first is a traditional crop starting from the top left corner with a width and height (pixels or ratio). The second is a crop originating from the center, going out a number of pixels or ratio.

Top Left Crop Sample

Center Crop Sample

Here is an example of cropping from the top left.

package main

import (
	"bytes"
	"image"
	"io/ioutil"
	"os"

	"github.com/app-nerds/kit/v6/images"
)

func main() {
	var (
		err          error
		imageBytes   []byte
		croppedBytes *bytes.Buffer
		newFile      *os.File
	)

	/*
	 * Open the original image
	 */
	if imageBytes, err = ioutil.ReadFile("./yolie.jpeg"); err != nil {
		panic(err)
	}

	cropper := images.ImageCropper{}

	croppedBytes, err = cropper.Crop(imageBytes, images.CropOptions{
		Anchor:     image.Point{X: 10, Y: 10},
		AnchorMode: images.CropAnchorModeTopLeft,
		Height:     100,
		UseRatio:   false,
		Width:      100,
	})

	if err != nil {
		panic(err)
	}

	if newFile, err = os.Create("./new-image.jpg"); err != nil {
		panic(err)
	}

	if _, err = croppedBytes.WriteTo(newFile); err != nil {
		panic(err)
	}

	newFile.Close()
}

Now here is an example of a center crop with a 1:1 ratio (square).

package main

import (
	"bytes"
	"io/ioutil"
	"os"

	"github.com/app-nerds/kit/v6/images"
)

func main() {
	var (
		err          error
		imageBytes   []byte
		croppedBytes *bytes.Buffer
		newFile      *os.File
	)

	/*
	 * Open the original image
	 */
	if imageBytes, err = ioutil.ReadFile("./image.jpeg"); err != nil {
		panic(err)
	}

	cropper := images.ImageCropper{}

	croppedBytes, err = cropper.Crop(imageBytes, images.CropOptions{
		AnchorMode: images.CropAnchorModeCentered,
		Height:     3,
		UseRatio:   true,
		Width:      4,
	})

	if err != nil {
		panic(err)
	}

	if newFile, err = os.Create("./new-image.png"); err != nil {
		panic(err)
	}

	if _, err = croppedBytes.WriteTo(newFile); err != nil {
		panic(err)
	}

	newFile.Close()
}

Resizer

The resizer service provides methods for resizing images.

ResizeImage

ResizeImage takes a source image, and a predefined image size, then returns the proportionally resized image. Sizes are:

  • Thumbnail - Resizes the image to a 10th of its original size
  • Small - Resizes the image to 25% of its original size
  • Medium - Resizes the image to 50% of its original size
  • Large - Original size

Below is an example of opening an image file and resizing it, saving it as a new file.

package main

import (
	"bytes"
	"os"

	"github.com/app-nerds/kit/v6/images"
)

func main() {
	var (
		err          error
		originalFile *os.File
		newFile      *os.File
		resizedBytes *bytes.Buffer
	)

	/*
	 * Open the original image
	 */
	if originalFile, err = os.Open("./image.jpeg"); err != nil {
		panic(err)
	}

	defer originalFile.Close()

	resizer := images.Resizer{}

	if resizedBytes, err = resizer.ResizeImage(originalFile, "image/jpeg", images.MEDIUM); err != nil {
		panic(err)
	}

	if newFile, err = os.Create("./new-image.jpg"); err != nil {
		panic(err)
	}

	if _, err = resizedBytes.WriteTo(newFile); err != nil {
		panic(err)
	}

	newFile.Close()
}
ResizeImagePixels

ResizeImagePixels is similar to ResizeImage, except it deals with a specific width and height setting instead of relative sizing. Note that this method does not concern itself with preserving aspect ratio.

package main

import (
	"bytes"
	"os"

	"github.com/app-nerds/kit/v6/images"
)

func main() {
	var (
		err          error
		originalFile *os.File
		newFile      *os.File
		resizedBytes *bytes.Buffer
	)

	/*
	 * Open the original image
	 */
	if originalFile, err = os.Open("./image.jpeg"); err != nil {
		panic(err)
	}

	defer originalFile.Close()

	resizer := images.Resizer{}

	if resizedBytes, err = resizer.ResizeImagePixels(originalFile, "image/jpeg", 200, 200); err != nil {
		panic(err)
	}

	if newFile, err = os.Create("./new-image.jpg"); err != nil {
		panic(err)
	}

	if _, err = resizedBytes.WriteTo(newFile); err != nil {
		panic(err)
	}

	newFile.Close()
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrImageFormatNotFound = fmt.Errorf("Image format not found")

ErrImageFormatNotFound is used when the provided image format is unsupported

View Source
var ErrInvalidFileType = errors.New("Invalid image type. Supported image formats are JPG and PNG")

ErrInvalidFileType is an error when the user uploads a bad file type

Functions

This section is empty.

Types

type CropAnchorMode

type CropAnchorMode string

CropAnchorMode is used to describe from where a crop happens

var CropAnchorModeCentered CropAnchorMode = "centered"

CropAnchorModeCentered crops from the center

var CropAnchorModeTopLeft CropAnchorMode = "top-left"

CropAnchorModeTopLeft crops from top-left

type CropOptions

type CropOptions struct {
	Anchor     image.Point
	AnchorMode CropAnchorMode
	Height     int
	UseRatio   bool
	Width      int
}

CropOptions describes how an image is cropped. Width/Height defines how big to make the image. AnchorMode describes how to apply the crop anchor point. Valid options are top-left, centered. Anchor describes the X/Y offset from top left if the anchor mode is top-left. UseRatio, when true, makes width/height a ratio-based instead of pixels.

func NewCropOptions

func NewCropOptions() *CropOptions

NewCropOptions creates a new structure with default values filled in

type IImageCropper

type IImageCropper interface {
	Crop(imageBytes []byte, cropOptions CropOptions) (*bytes.Buffer, error)
}

IImageCropper is an interface for cropping images

type IResizer

type IResizer interface {
	ResizeImage(source io.ReadSeeker, contentType string, imageSize ImageSize) (*bytes.Buffer, error)
	ResizeImagePixels(source io.ReadSeeker, contentType string, width, height int) (*bytes.Buffer, error)
}

IResizer is an interface to describe structs that resize images

type ImageCropper

type ImageCropper struct{}

ImageCropper crops images

func (ImageCropper) Crop

func (ic ImageCropper) Crop(imageBytes []byte, cropOptions CropOptions) (*bytes.Buffer, error)

Crop takes an image reader and performs a crop based on the options provided. It returns the newly cropped image in the form of a PNG

type ImageSize

type ImageSize string

ImageSize describes generic image sizes

const (
	DEFAULT   ImageSize = "default"
	THUMBNAIL ImageSize = "thumbnail"
	SMALL     ImageSize = "small"
	MEDIUM    ImageSize = "medium"
	LARGE     ImageSize = "large"
)

func (ImageSize) String

func (is ImageSize) String() string

type MockResizer

type MockResizer struct {
	ResizeImageFunc       func(source io.ReadSeeker, contentType string, imageSize ImageSize) (*bytes.Buffer, error)
	ResizeImagePixelsFunc func(source io.ReadSeeker, contentType string, width, height int) (*bytes.Buffer, error)
}

func (MockResizer) ResizeImage

func (m MockResizer) ResizeImage(source io.ReadSeeker, contentType string, imageSize ImageSize) (*bytes.Buffer, error)

func (MockResizer) ResizeImagePixels

func (m MockResizer) ResizeImagePixels(source io.ReadSeeker, contentType string, width, height int) (*bytes.Buffer, error)

type Resizer

type Resizer struct{}

A Resizer contains methods for resizing images and re-encoding them.

func (Resizer) ResizeImage

func (r Resizer) ResizeImage(source io.ReadSeeker, contentType string, imageSize ImageSize) (*bytes.Buffer, error)

ResizeImage takes a source image, content type (MIME), and a image size ( THUMBNAIL, SMALL, MEDIUM, LARGE) and resizes proportionally.

func (Resizer) ResizeImagePixels

func (r Resizer) ResizeImagePixels(source io.ReadSeeker, contentType string, width, height int) (*bytes.Buffer, error)

ResizeImagePixels resizes a source image to the specified widthxheight.

Jump to

Keyboard shortcuts

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