Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for cwebp resize command #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions cwebp.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ type cropInfo struct {
height int
}

type resizeInfo struct {
width int
height int
}

// CWebP compresses an image using the WebP format. Input format can be either PNG, JPEG, TIFF, WebP or raw Y'CbCr samples.
// https://developers.google.com/speed/webp/docs/cwebp
type CWebP struct {
Expand All @@ -27,6 +32,7 @@ type CWebP struct {
output io.Writer
quality int
crop *cropInfo
resize *resizeInfo
}

// NewCWebP creates new CWebP instance.
Expand Down Expand Up @@ -106,6 +112,12 @@ func (c *CWebP) Crop(x, y, width, height int) *CWebP {
return c
}

// Resize the source to a rectangle with size width x height. If either (but not both) of the width or height parameters is 0, the value will be calculated preserving the aspect-ratio. Note: scaling is applied after cropping.
func (c *CWebP) Resize(width, height int) *CWebP {
c.resize = &resizeInfo{width, height}
return c
}

// Run starts cwebp with specified parameters.
func (c *CWebP) Run() error {
defer c.BinWrapper.Reset()
Expand All @@ -118,6 +130,10 @@ func (c *CWebP) Run() error {
c.Arg("-crop", fmt.Sprintf("%d", c.crop.x), fmt.Sprintf("%d", c.crop.y), fmt.Sprintf("%d", c.crop.width), fmt.Sprintf("%d", c.crop.height))
}

if c.resize != nil {
c.Arg("-resize", fmt.Sprintf("%d", c.resize.width), fmt.Sprintf("%d", c.resize.height))
}

output, err := c.getOutput()

if err != nil {
Expand Down Expand Up @@ -148,6 +164,7 @@ func (c *CWebP) Run() error {
// Reset all parameters to default values
func (c *CWebP) Reset() *CWebP {
c.crop = nil
c.resize = nil
c.quality = -1
return c
}
Expand Down