This repository has been archived by the owner on Jun 3, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
178 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package arn | ||
|
||
import ( | ||
"bytes" | ||
"image" | ||
"path" | ||
"time" | ||
|
||
"github.com/blitzprog/imageoutput" | ||
) | ||
|
||
const ( | ||
// GroupImageSmallWidth is the minimum width in pixels of a small group image. | ||
GroupImageSmallWidth = 70 | ||
|
||
// GroupImageSmallHeight is the minimum height in pixels of a small group image. | ||
GroupImageSmallHeight = 70 | ||
|
||
// GroupImageWebPQuality is the WebP quality of group images. | ||
GroupImageWebPQuality = 70 | ||
|
||
// GroupImageJPEGQuality is the JPEG quality of group images. | ||
GroupImageJPEGQuality = 70 | ||
|
||
// GroupImageQualityBonusLowDPI ... | ||
GroupImageQualityBonusLowDPI = 12 | ||
|
||
// GroupImageQualityBonusLarge ... | ||
GroupImageQualityBonusLarge = 10 | ||
|
||
// GroupImageQualityBonusMedium ... | ||
GroupImageQualityBonusMedium = 15 | ||
|
||
// GroupImageQualityBonusSmall ... | ||
GroupImageQualityBonusSmall = 15 | ||
) | ||
|
||
// Define the group image outputs | ||
var groupImageOutputs = []imageoutput.Output{ | ||
// Original at full size | ||
&imageoutput.OriginalFile{ | ||
Directory: path.Join(Root, "images/groups/original/"), | ||
Width: 0, | ||
Height: 0, | ||
}, | ||
|
||
// JPEG - Small | ||
&imageoutput.JPEGFile{ | ||
Directory: path.Join(Root, "images/groups/small/"), | ||
Width: GroupImageSmallWidth, | ||
Height: GroupImageSmallHeight, | ||
Quality: GroupImageJPEGQuality + GroupImageQualityBonusLowDPI + GroupImageQualityBonusSmall, | ||
}, | ||
|
||
// WebP - Small | ||
&imageoutput.WebPFile{ | ||
Directory: path.Join(Root, "images/groups/small/"), | ||
Width: GroupImageSmallWidth, | ||
Height: GroupImageSmallHeight, | ||
Quality: GroupImageWebPQuality + GroupImageQualityBonusLowDPI + GroupImageQualityBonusSmall, | ||
}, | ||
} | ||
|
||
// Define the high DPI group image outputs | ||
var groupImageOutputsHighDPI = []imageoutput.Output{ | ||
// JPEG - Small | ||
&imageoutput.JPEGFile{ | ||
Directory: path.Join(Root, "images/groups/small/"), | ||
Width: GroupImageSmallWidth * 2, | ||
Height: GroupImageSmallHeight * 2, | ||
Quality: GroupImageJPEGQuality + GroupImageQualityBonusSmall, | ||
}, | ||
|
||
// WebP - Small | ||
&imageoutput.WebPFile{ | ||
Directory: path.Join(Root, "images/groups/small/"), | ||
Width: GroupImageSmallWidth * 2, | ||
Height: GroupImageSmallHeight * 2, | ||
Quality: GroupImageWebPQuality + GroupImageQualityBonusSmall, | ||
}, | ||
} | ||
|
||
// GroupImage ... | ||
type GroupImage AnimeImage | ||
|
||
// SetImageBytes accepts a byte buffer that represents an image file and updates the group image. | ||
func (group *Group) SetImageBytes(data []byte) error { | ||
// Decode | ||
img, format, err := image.Decode(bytes.NewReader(data)) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return group.SetImage(&imageoutput.MetaImage{ | ||
Image: img, | ||
Format: format, | ||
Data: data, | ||
}) | ||
} | ||
|
||
// SetImage sets the group image to the given MetaImage. | ||
func (group *Group) SetImage(metaImage *imageoutput.MetaImage) error { | ||
var lastError error | ||
|
||
// Save the different image formats and sizes in low DPI | ||
for _, output := range groupImageOutputs { | ||
err := output.Save(metaImage, group.ID) | ||
|
||
if err != nil { | ||
lastError = err | ||
} | ||
} | ||
|
||
// Save the different image formats and sizes in high DPI | ||
for _, output := range groupImageOutputsHighDPI { | ||
err := output.Save(metaImage, group.ID+"@2") | ||
|
||
if err != nil { | ||
lastError = err | ||
} | ||
} | ||
|
||
group.Image.Extension = metaImage.Extension() | ||
group.Image.Width = metaImage.Image.Bounds().Dx() | ||
group.Image.Height = metaImage.Image.Bounds().Dy() | ||
group.Image.AverageColor = GetAverageColor(metaImage.Image) | ||
group.Image.LastModified = time.Now().Unix() | ||
return lastError | ||
} | ||
|
||
// HasImage returns true if the group has an image. | ||
func (group *Group) HasImage() bool { | ||
return group.Image.Extension != "" && group.Image.Width > 0 | ||
} |
This file contains 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