在我们开发APP或Game时,经常会遇到要更新图标的情况,然而每次都让美术童鞋出很多尺寸的图是一件很麻烦的事。尤其是项目要多的话,需要出的图就更多了,这不是一件高效率的做事方式。
所以写了个Go脚本,通过传参的方式,只需美术同学出一张较大尺寸的原图,就可以随意导出任意想要尺寸的图标啦!
package main
import (
"flag"
"fmt"
"image"
"image/jpeg"
"image/png"
"os"
"path"
"strconv"
"github.com/nfnt/resize"
)
var _Service *TService
var iconSizes = []int{48, 72, 96, 144}
// TService 服务
type TService struct {
SavePath string
FileExt string
IconImage image.Image
}
func main() {
inPath := flag.String("i", "", "The import icon path.")
outPath := flag.String("o", "", "The export icon path.")
flag.Parse()
if *inPath == "" || *outPath == "" {
fmt.Println("Please enter the correct parameters")
return
}
err := _Service.initService(*inPath, *outPath)
if err != nil {
fmt.Println(err.Error())
return
}
_Service.make()
}
func (service *TService) initService(inPath, outPath string) error {
file, err := os.Open(inPath)
if err != nil {
return err
}
defer file.Close()
_Service = &TService{}
_Service.SavePath = outPath
_Service.FileExt = path.Ext(inPath)
switch _Service.FileExt {
case ".png":
img, err := png.Decode(file)
if err != nil {
return err
}
_Service.IconImage = img
break
case ".jpg":
img, err := jpeg.Decode(file)
if err != nil {
return err
}
_Service.IconImage = img
break
}
service.createSavePath(_Service.SavePath)
return nil
}
func (service *TService) createSavePath(fileName string) {
os.RemoveAll(fileName)
if _, err := os.Stat(fileName); os.IsNotExist(err) {
os.MkdirAll(fileName, os.ModePerm)
}
}
func (service *TService) make() {
for index := 0; index < len(iconSizes); index++ {
num := strconv.Itoa(iconSizes[index])
img := resize.Resize(uint(iconSizes[index]), 0, service.IconImage, resize.Lanczos3)
out, err := os.Create(service.SavePath + "/" + num + service.FileExt)
if err != nil {
fmt.Println("Icon Make Failed: \n" + err.Error())
return
}
defer out.Close()
png.Encode(out, img)
}
fmt.Println("Icon Make Success!")
}