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
447 changed files
with
234,075 additions
and
94 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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
testdata/output | ||
dist | ||
*.output.* | ||
*.comp.* | ||
output | ||
imagic | ||
|
||
# Binaries for programs and plugins | ||
|
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
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,64 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/joway/imagic/pkg/image" | ||
"github.com/joway/imagic/pkg/util" | ||
"path/filepath" | ||
"sync" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var resizeParallelCh chan int | ||
var resizeWidth int | ||
var resizeHeight int | ||
|
||
func init() { | ||
resizeCmd.Flags().IntVarP(&resizeWidth, "width", "w", 1024, "number of concurrent tasks") | ||
resizeCmd.Flags().IntVarP(&resizeHeight, "height", "l", 0, "number of concurrent tasks") | ||
|
||
rootCmd.AddCommand(resizeCmd) | ||
} | ||
|
||
var resizeCmd = &cobra.Command{ | ||
Use: "resize", | ||
Args: cobra.MinimumNArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
resizeParallelCh = make(chan int, parallel) | ||
wg := &sync.WaitGroup{} | ||
|
||
files := getFilesFromPatterns(args) | ||
for _, filename := range files { | ||
resizeParallelCh <- 1 | ||
wg.Add(1) | ||
|
||
go func() { | ||
defer wg.Done() | ||
|
||
outputFileName := getOutputFileName(filename, suffix, output) | ||
img, err := image.NewImageFromPath(filename) | ||
if err != nil { | ||
util.LogError(err) | ||
return | ||
} | ||
outImg, err := img.Resize(resizeWidth, resizeHeight) | ||
if err != nil { | ||
util.LogError(err) | ||
return | ||
} | ||
if err := outImg.Write(outputFileName); err != nil { | ||
util.LogError(err) | ||
return | ||
} | ||
|
||
util.LogInfo( | ||
fmt.Sprintf("%s resized", filepath.Base(outputFileName)), | ||
) | ||
|
||
<-resizeParallelCh | ||
}() | ||
} | ||
wg.Wait() | ||
}, | ||
} |
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 |
---|---|---|
@@ -1,23 +1,36 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/joway/imagic/pkg/util" | ||
"github.com/spf13/cobra" | ||
"os" | ||
) | ||
|
||
var parallel int | ||
var output string | ||
var suffix string | ||
|
||
func init() { | ||
rootCmd.PersistentFlags().IntVarP(¶llel, "parallel", "p", 4, "number of concurrent tasks") | ||
rootCmd.PersistentFlags().StringVarP(&output, "output", "o", "", "output directory to write precessed images") | ||
rootCmd.PersistentFlags().StringVarP(&suffix, "suffix", "s", "", "suffix of precessed image filename") | ||
} | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "imagic", | ||
Short: "Imagic is a fast and easy image tool", | ||
Long: `A Fast and Easy image tool`, | ||
Short: "An easy and fast tool to process images.", | ||
Long: ` | ||
Imagic is an easy and fast tool to process images. | ||
Created by Joway Wang (https://joway.io). | ||
Issues on https://github.com/joway/imagic/issues. | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
// Do Stuff Here | ||
_ = cmd.Help() | ||
}, | ||
} | ||
|
||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
util.LogFatal(err) | ||
} | ||
} |
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,39 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/joway/imagic/pkg/util" | ||
"path/filepath" | ||
) | ||
|
||
func getFilesFromPatterns(patterns []string) []string { | ||
result := make([]string, 0) | ||
for _, pattern := range patterns { | ||
files := getFilesFromPattern(pattern) | ||
result = append(result, files...) | ||
} | ||
return result | ||
} | ||
|
||
func getFilesFromPattern(pattern string) []string { | ||
files, err := filepath.Glob(pattern) | ||
if err != nil { | ||
util.LogFatal(err) | ||
} | ||
return files | ||
} | ||
|
||
func getOutputFileName(filename string, suffix string, outputDir string) string { | ||
var outputPath string | ||
if outputDir != "" { | ||
absOutput, _ := filepath.Abs(outputDir) | ||
outputPath = absOutput | ||
} else { | ||
outputPath = filepath.Dir(filename) | ||
} | ||
baseFilename := filepath.Base(filename) | ||
dotPos := util.IndexOf(baseFilename, ".", true) | ||
if dotPos == -1 { | ||
return outputPath + "/" + baseFilename + suffix + baseFilename | ||
} | ||
return outputPath + "/" + baseFilename[:dotPos] + suffix + baseFilename[dotPos:] | ||
} |
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,5 @@ | ||
# /bin/bash | ||
|
||
GO111MODULE=on go mod download | ||
GO111MODULE=on go mod vendor | ||
GO111MODULE=on go mod verify |
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 |
---|---|---|
@@ -1,10 +1,16 @@ | ||
module github.com/joway/imagic | ||
|
||
require ( | ||
github.com/disintegration/imaging v1.5.0 | ||
github.com/fatih/color v1.7.0 | ||
github.com/inconshreveable/mousetrap v1.0.0 // indirect | ||
github.com/joway/libimagequant-go v0.1.0 | ||
github.com/mattn/go-colorable v0.0.9 // indirect | ||
github.com/mattn/go-isatty v0.0.4 // indirect | ||
github.com/pkg/errors v0.8.1 | ||
github.com/spf13/cobra v0.0.3 | ||
github.com/spf13/pflag v1.0.3 // indirect | ||
github.com/stretchr/testify v1.3.0 | ||
github.com/ultimate-guitar/go-imagequant v0.0.0-20180623193211-b579eaf0f7d8 | ||
golang.org/x/image v0.0.0-20181116024801-cd38e8056d9b // indirect | ||
golang.org/x/sys v0.0.0-20190109145017-48ac38b7c8cb // indirect | ||
) |
Oops, something went wrong.