image resizing via bimg

This commit is contained in:
Toni Melisma 2020-09-24 00:55:43 +03:00
parent 881b38073a
commit 9c10bf36ef
3 changed files with 71 additions and 15 deletions

2
go.mod
View File

@ -1,3 +1,5 @@
module github.com/tonimelisma/gogallery
go 1.15
require github.com/h2non/bimg v1.1.4 // indirect

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/h2non/bimg v1.1.4 h1:6qf7qDo3d9axbNUOcSoQmzleBCMTcQ1PwF3FgGhX4O0=
github.com/h2non/bimg v1.1.4/go.mod h1:R3+UiYwkK4rQl6KVFTOFJHitgLbZXBZNFh2cv3AEbp8=

View File

@ -8,6 +8,8 @@ import (
"path/filepath"
"strings"
"time"
"github.com/h2non/bimg"
)
// global defaults
@ -100,12 +102,8 @@ func isEmptyDir(directory string) (isEmpty bool) {
return false
}
func isMediaFile(filename string) (isMedia bool) {
func isVideoFile(filename string) bool {
switch filepath.Ext(strings.ToLower(filename)) {
case ".jpg", ".jpeg", ".heic", ".png", ".gif", ".tif":
return true
case ".cr2", ".raw", ".arw":
return true
case ".mp4", ".mov", ".3gp", ".avi", ".mts", ".m4v", ".mpg":
return true
default:
@ -113,6 +111,24 @@ func isMediaFile(filename string) (isMedia bool) {
}
}
func isImageFile(filename string) bool {
switch filepath.Ext(strings.ToLower(filename)) {
case ".jpg", ".jpeg", ".heic", ".png", ".gif", ".tif":
return true
case ".cr2", ".raw", ".arw":
return true
default:
return false
}
}
func isMediaFile(filename string) bool {
if isImageFile(filename) || isVideoFile(filename) {
return true
}
return false
}
func recurseDirectory(thisDirectory string, relativeDirectory string) (root directory) {
root.name = filepath.Base(thisDirectory)
asIsStat, _ := os.Stat(thisDirectory)
@ -142,15 +158,11 @@ func compareDirectories(source *directory, gallery *directory, changes *int) {
for i, inputFile := range source.files {
for j, outputFile := range gallery.files {
if inputFile.name == outputFile.name {
fmt.Println("gallery exists:", gallery.files[j].absPath, gallery.files[j].exists)
gallery.files[j].exists = true
fmt.Println("after update:", gallery.files[j].modTime, source.files[i].modTime)
if !outputFile.modTime.Before(inputFile.modTime) {
source.files[i].exists = true
fmt.Println("outputfile not modified before inputfile:", inputFile.absPath)
*changes--
}
fmt.Println("")
}
}
}
@ -227,19 +239,59 @@ func symlinkFile(source string, destination string, optDryRun bool) {
}
}
func thumbnailImage(source string, destination string) {
buffer, err := bimg.Read(source)
checkError(err)
newImage, err := bimg.NewImage(buffer).ResizeAndCrop(1920, 1080)
checkError(err)
bimg.Write(destination, newImage)
}
func fullsizeImage(source string, destination string) {
buffer, err := bimg.Read(source)
checkError(err)
newImage, err := bimg.NewImage(buffer).Thumbnail(150)
checkError(err)
bimg.Write(destination, newImage)
}
func fullsizeCopyFile(source string, destination string, optDryRun bool) {
if optDryRun {
fmt.Println("Would full-size copy", source, "to", destination)
if isImageFile(source) {
if optDryRun {
fmt.Println("Would full-size copy image", source, "to", destination)
} else {
// TODO Image magic here
}
} else if isVideoFile(source) {
if optDryRun {
fmt.Println("Would full-size copy video ", source, "to", destination)
} else {
// TODO Image magic here
}
} else {
// TODO MAGIC HAPPENS HERE
fmt.Println("can't recognize file type for copy")
}
}
func thumbnailCopyFile(source string, destination string, optDryRun bool) {
if optDryRun {
fmt.Println("Would thumbnail copy", source, "to", destination)
if isImageFile(source) {
if optDryRun {
fmt.Println("Would thumbnail copy image", source, "to", destination)
} else {
// TODO Video magic here
}
} else if isVideoFile(source) {
if optDryRun {
fmt.Println("Would thumbnail copy video ", source, "to", destination)
} else {
// TODO Video magic here
}
} else {
// TODO MAGIC HAPPENS HERE
fmt.Println("can't recognize file type for copy")
}
}