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 option to ignore ShrinkOnLoad functionality #222

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
71 changes: 36 additions & 35 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,39 +188,40 @@ type Sharpen struct {

// Options represents the supported image transformation options.
type Options struct {
Height int
Width int
AreaHeight int
AreaWidth int
Top int
Left int
Quality int
Compression int
Zoom int
Crop bool
SmartCrop bool // Deprecated, use: bimg.Options.Gravity = bimg.GravitySmart
Enlarge bool
Embed bool
Flip bool
Flop bool
Force bool
NoAutoRotate bool
NoProfile bool
Interlace bool
StripMetadata bool
Trim bool
Lossless bool
Extend Extend
Rotate Angle
Background Color
Gravity Gravity
Watermark Watermark
WatermarkImage WatermarkImage
Type ImageType
Interpolator Interpolator
Interpretation Interpretation
GaussianBlur GaussianBlur
Sharpen Sharpen
Threshold float64
OutputICC string
Height int
Width int
AreaHeight int
AreaWidth int
Top int
Left int
Quality int
Compression int
Zoom int
Crop bool
SmartCrop bool // Deprecated, use: bimg.Options.Gravity = bimg.GravitySmart
Enlarge bool
Embed bool
Flip bool
Flop bool
Force bool
NoAutoRotate bool
NoProfile bool
Interlace bool
StripMetadata bool
Trim bool
Lossless bool
Extend Extend
Rotate Angle
Background Color
Gravity Gravity
Watermark Watermark
WatermarkImage WatermarkImage
Type ImageType
Interpolator Interpolator
Interpretation Interpretation
GaussianBlur GaussianBlur
Sharpen Sharpen
Threshold float64
OutputICC string
IgnoreShrinkOnLoad bool
}
28 changes: 15 additions & 13 deletions resizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,23 @@ func resizer(buf []byte, o Options) ([]byte, error) {
}
}

// Try to use libjpeg/libwebp shrink-on-load
supportsShrinkOnLoad := imageType == WEBP && VipsMajorVersion >= 8 && VipsMinorVersion >= 3
supportsShrinkOnLoad = supportsShrinkOnLoad || imageType == JPEG
if supportsShrinkOnLoad && shrink >= 2 {
tmpImage, factor, err := shrinkOnLoad(buf, image, imageType, factor, shrink)
if err != nil {
return nil, err
}

image = tmpImage
factor = math.Max(factor, 1.0)
shrink = int(math.Floor(factor))
residual = float64(shrink) / factor
if !o.IgnoreShrinkOnLoad {
// Try to use libjpeg/libwebp shrink-on-load
supportsShrinkOnLoad := imageType == WEBP && VipsMajorVersion >= 8 && VipsMinorVersion >= 3
supportsShrinkOnLoad = supportsShrinkOnLoad || imageType == JPEG
if supportsShrinkOnLoad && shrink >= 2 {
tmpImage, factor, err := shrinkOnLoad(buf, image, imageType, factor, shrink)
if err != nil {
return nil, err
}

image = tmpImage
factor = math.Max(factor, 1.0)
shrink = int(math.Floor(factor))
residual = float64(shrink) / factor
}
}

// Zoom image, if necessary
image, err = zoomImage(image, o.Zoom)
if err != nil {
Expand Down