Moved separate functionality into dedicated packages

This commit is contained in:
Samuel Hawksby-Robinson 2020-09-18 16:19:43 +01:00
parent d5ab052e59
commit 207b54730f
No known key found for this signature in database
GPG Key ID: 64CF99D4A64A1205
5 changed files with 41 additions and 35 deletions

View File

@ -1,4 +1,4 @@
package main
package files
import (
"fmt"
@ -9,9 +9,10 @@ import (
"strconv"
"github.com/status-im/image_resizer/common"
"github.com/status-im/image_resizer/images"
)
func getImage(fileName string) image.Image {
func GetImage(fileName string) image.Image {
file, err := os.Open(fileName)
if err != nil {
log.Fatal(err)
@ -26,7 +27,7 @@ func getImage(fileName string) image.Image {
return img
}
func outputImage(img image.Image, imgDetail *imageDetails) {
func RenderImage(img image.Image, imgDetail *images.Details) {
out, err := os.Create(imgDetail.FileName)
if err != nil {
log.Fatal(err)
@ -42,10 +43,10 @@ func outputImage(img image.Image, imgDetail *imageDetails) {
imgDetail.SizeFile = fi.Size()
}
func makeReadMe(imgDs map[string][]imageDetails) {
func MakeReadMe(imageList []string, imgDs map[string][]images.Details) {
var txt string
for _, imageName := range images {
for _, imageName := range imageList {
txt += "## " + imageName + "\n\n"
txt += fmt.Sprintf("![Original %s image](%s)\n\n", imageName, common.ImageDir + imageName + ".jpg")

View File

@ -1,4 +1,4 @@
package main
package images
import (
"image"

View File

@ -1,4 +1,4 @@
package main
package images
import (
"image"
@ -9,7 +9,7 @@ import (
"github.com/oliamb/cutter"
)
func cropImage(img image.Image) image.Image {
func Crop(img image.Image) image.Image {
var sl int
if img.Bounds().Max.X > img.Bounds().Max.Y {
sl = img.Bounds().Max.Y
@ -29,11 +29,11 @@ func cropImage(img image.Image) image.Image {
return croppedImg
}
func resizeSquareImage(size uint, img image.Image) image.Image {
func ResizeSquare(size uint, img image.Image) image.Image {
return resize.Resize(size, 0, img, resize.Bilinear)
}
func circleCropImage(img image.Image, size int) image.Image {
func CropCircle(img image.Image, size int) image.Image {
dst := image.NewRGBA(image.Rect(0, 0, size, size))
draw.DrawMask(
dst,

View File

@ -1,4 +1,4 @@
package main
package images
import (
"fmt"
@ -7,7 +7,7 @@ import (
"github.com/status-im/image_resizer/common"
)
type imageDetails struct {
type Details struct {
SizePixel uint
SizeFile int64
Quality int
@ -15,22 +15,22 @@ type imageDetails struct {
Properties string
}
func getSourceImageName(imageName string) string {
func GetSourceName(imageName string) string {
return common.ImageDir + imageName + ".jpg"
}
func makeOutputImageName(imageName string, size uint, i int, properties string) string {
func makeOutputName(imageName string, size uint, i int, properties string) string {
if properties != "" {
properties = "_" + strings.ReplaceAll(properties, " ", "-")
}
return fmt.Sprintf(common.ImageDir + "%s_s-%d_q-%d%s.jpg", imageName, size, i, properties)
}
func makeImageDetails(imageName string, size uint, quality int, properties string) imageDetails {
return imageDetails{
SizePixel: size,
Quality: quality,
func MakeDetails(imageName string, size uint, quality int, properties string) Details {
return Details{
SizePixel: size,
Quality: quality,
Properties: properties,
FileName: makeOutputImageName(imageName, size, quality, properties),
FileName: makeOutputName(imageName, size, quality, properties),
}
}

37
main.go
View File

@ -1,7 +1,12 @@
package main
import (
"github.com/status-im/image_resizer/files"
"github.com/status-im/image_resizer/images"
)
var (
images = []string{
imageList = []string{
"elephant",
"frog",
"goat",
@ -20,33 +25,33 @@ var (
)
func main() {
imgDs := make(map[string][]imageDetails)
imgDs := make(map[string][]images.Details)
for _, imageName := range images {
img := getImage(getSourceImageName(imageName))
croppedImg := cropImage(img)
for _, imageName := range imageList {
img := files.GetImage(images.GetSourceName(imageName))
croppedImg := images.Crop(img)
for _, size := range sizes {
for i := 1; i < 11; i++ {
ri := resizeSquareImage(size, croppedImg)
id := makeImageDetails(imageName, size, i*10, "")
outputImage(ri, &id)
ri := images.ResizeSquare(size, croppedImg)
id := images.MakeDetails(imageName, size, i*10, "")
files.RenderImage(ri, &id)
imgDs[imageName] = append(imgDs[imageName], id)
precci := circleCropImage(ri, int(size))
precid := makeImageDetails(imageName, size, i*10, "pre-render circle crop")
outputImage(precci, &precid)
precci := images.CropCircle(ri, int(size))
precid := images.MakeDetails(imageName, size, i*10, "pre-render circle crop")
files.RenderImage(precci, &precid)
imgDs[imageName] = append(imgDs[imageName], precid)
li := getImage(id.FileName)
postcci := circleCropImage(li, int(size))
postcid := makeImageDetails(imageName, size, i*10, "post-render circle crop")
outputImage(postcci, &postcid)
li := files.GetImage(id.FileName)
postcci := images.CropCircle(li, int(size))
postcid := images.MakeDetails(imageName, size, i*10, "post-render circle crop")
files.RenderImage(postcci, &postcid)
imgDs[imageName] = append(imgDs[imageName], postcid)
}
}
}
makeReadMe(imgDs)
files.MakeReadMe(imageList, imgDs)
}