This repository was archived by the owner on Oct 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Scale Mode #17
Merged
Merged
Scale Mode #17
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d6b9f59
Refactor image processor interfaces
artnez 013a8d0
Add scale mode, refactor image processor
artnez e4fefe5
Update README and examples
artnez b823668
Add aspect_crop. Change semantics of aspect_fill.
artnez a858e16
Update scale_mode in README, add cheat sheet
artnez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,61 +22,90 @@ package halfshell | |
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "io/ioutil" | ||
| "mime" | ||
| "net/http" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/rafikk/imagick/imagick" | ||
| ) | ||
|
|
||
| // Image contains a byte array of the image data and its MIME type. | ||
| // TODO: See if we can use the std library's Image type without incurring | ||
| // the hit of extra copying. | ||
| var EmptyImageDimensions = ImageDimensions{} | ||
| var EmptyResizeDimensions = ResizeDimensions{} | ||
|
|
||
| type Image struct { | ||
| Bytes []byte | ||
| MimeType string | ||
| Wand *imagick.MagickWand | ||
| Signature string | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this value is no longer used. |
||
| destroyed bool | ||
| } | ||
|
|
||
| // NewImageFromHTTPResponse returns a pointer to a new Image created from an | ||
| // HTTP response object. | ||
| func NewImageFromHTTPResponse(httpResponse *http.Response) (*Image, error) { | ||
| imageBytes, err := ioutil.ReadAll(httpResponse.Body) | ||
| defer httpResponse.Body.Close() | ||
| func NewImageFromBuffer(buffer io.Reader) (image *Image, err error) { | ||
| bytes, err := ioutil.ReadAll(buffer) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &Image{ | ||
| Bytes: imageBytes, | ||
| MimeType: httpResponse.Header.Get("Content-Type"), | ||
| }, nil | ||
| } | ||
|
|
||
| // NewImageFromFile returns a pointer to a new Image created from a file. | ||
| func NewImageFromFile(file *os.File) (*Image, error) { | ||
| imageBytes, err := ioutil.ReadAll(file) | ||
| image = &Image{Wand: imagick.NewMagickWand()} | ||
| err = image.Wand.ReadImageBlob(bytes) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &Image{ | ||
| Bytes: imageBytes, | ||
| MimeType: mime.TypeByExtension(filepath.Ext(file.Name())), | ||
| }, nil | ||
| return image, nil | ||
| } | ||
|
|
||
| func NewImageFromFile(file *os.File) (image *Image, err error) { | ||
| image, err = NewImageFromBuffer(file) | ||
| return image, err | ||
| } | ||
|
|
||
| func (i *Image) GetMIMEType() string { | ||
| return fmt.Sprintf("image/%s", strings.ToLower(i.Wand.GetImageFormat())) | ||
| } | ||
|
|
||
| func (i *Image) GetBytes() (bytes []byte, size int) { | ||
| bytes = i.Wand.GetImageBlob() | ||
| size = len(bytes) | ||
| return bytes, size | ||
| } | ||
|
|
||
| func (i *Image) GetWidth() uint { | ||
| return i.Wand.GetImageWidth() | ||
| } | ||
|
|
||
| func (i *Image) GetHeight() uint { | ||
| return i.Wand.GetImageHeight() | ||
| } | ||
|
|
||
| func (i *Image) GetDimensions() ImageDimensions { | ||
| return ImageDimensions{i.GetWidth(), i.GetHeight()} | ||
| } | ||
|
|
||
| func (i *Image) GetSignature() string { | ||
| return i.Wand.GetImageSignature() | ||
| } | ||
|
|
||
| func (i *Image) Destroy() { | ||
| if !i.destroyed { | ||
| i.Wand.Destroy() | ||
| i.destroyed = true | ||
| } | ||
| } | ||
|
|
||
| // ImageDimensions is the width and height of an image. | ||
| type ImageDimensions struct { | ||
| Width uint64 | ||
| Height uint64 | ||
| Width uint | ||
| Height uint | ||
| } | ||
|
|
||
| // AspectRatio returns the image dimension's aspect ratio. | ||
| func (d ImageDimensions) AspectRatio() float64 { | ||
| return float64(d.Width) / float64(d.Height) | ||
| } | ||
|
|
||
| func (d ImageDimensions) String() string { | ||
| return fmt.Sprintf("%dx%d", d.Width, d.Height) | ||
| } | ||
|
|
||
| type ResizeDimensions struct { | ||
| Scale ImageDimensions | ||
| Crop ImageDimensions | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a
DEPRECATEDwarning here instead? Also documentcrop_mode.