Created
August 29, 2020 21:35
-
-
Save Oppodelldog/e6ef0dcfeb21c1b83f4c17e3cd1449cd to your computer and use it in GitHub Desktop.
app icon creation
This file contains hidden or 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
package main | |
import ( | |
"bytes" | |
"fmt" | |
ico "github.com/Kodeworks/golang-image-ico" | |
"github.com/nfnt/resize" | |
"image" | |
"image/gif" | |
"image/png" | |
"io" | |
"io/ioutil" | |
"os" | |
"path" | |
"strings" | |
) | |
const outputPath = "out" | |
type target struct { | |
n string | |
w uint | |
h uint | |
f bool | |
e encodeFunc | |
} | |
type encodeFunc func(io.Writer, image.Image) error | |
var targets = []target{ | |
{n: "apple-touch-icon-%vx%v.png", w: 114, h: 114, e: png.Encode}, | |
{n: "apple-touch-icon-%vx%v.png", w: 120, h: 120, e: png.Encode}, | |
{n: "apple-touch-icon-%vx%v.png", w: 128, h: 128, e: png.Encode}, | |
{n: "apple-touch-icon-%vx%v.png", w: 144, h: 144, e: png.Encode}, | |
{n: "apple-touch-icon-%vx%v.png", w: 152, h: 152, e: png.Encode}, | |
{n: "apple-touch-icon-%vx%v.png", w: 180, h: 180, e: png.Encode}, | |
{n: "apple-touch-icon-%vx%v.png", w: 57, h: 57, e: png.Encode}, | |
{n: "apple-touch-icon-%vx%v.png", w: 60, h: 60, e: png.Encode}, | |
{n: "apple-touch-icon-%vx%v.png", w: 72, h: 72, e: png.Encode}, | |
{n: "apple-touch-icon-%vx%v.png", w: 76, h: 76, e: png.Encode}, | |
{n: "apple-touch-icon.png", w: 0, h: 0, e: png.Encode}, | |
{n: "apple-touch-icon-precomposed.png", w: 0, h: 0, e: png.Encode}, | |
{n: "favicon-%vx%v.png", w: 160, h: 160, e: png.Encode}, | |
{n: "favicon-%vx%v.png", w: 16, h: 16, e: png.Encode}, | |
{n: "favicon-%vx%v.png", w: 192, h: 192, e: png.Encode}, | |
{n: "favicon-%vx%v.png", w: 196, h: 196, e: png.Encode}, | |
{n: "favicon-%vx%v.png", w: 32, h: 32, e: png.Encode}, | |
{n: "favicon-%vx%v.png", w: 96, h: 96, e: png.Encode}, | |
{n: "win8-tile-%vx%v.png", w: 144, h: 144, e: png.Encode}, | |
{n: "win8-tile-%vx%v.png", w: 150, h: 150, e: png.Encode}, | |
{n: "win8-tile-%vx%v.png", w: 310, h: 310, e: png.Encode}, | |
{n: "win8-tile-%vx%v.png", w: 310, h: 150, e: png.Encode}, | |
{n: "win8-tile-%vx%v.png", w: 70, h: 70, e: png.Encode}, | |
{n: "favicon.gif", w: 0, h: 0, e: encodeGif}, | |
{n: "favicon.ico", w: 16, h: 16, e: ico.Encode}, | |
{n: "favicon.png", w: 0, h: 0, e: png.Encode}, | |
} | |
func main() { | |
must(os.RemoveAll(outputPath)) | |
must(os.MkdirAll(outputPath, 0777)) | |
img := getSourceImage() | |
for _, target := range targets { | |
newImage := resize.Resize(target.w, target.h, img, resize.Lanczos3) | |
f, err := os.Create(path.Join(outputPath, getFilename(target))) | |
panicOnError(err) | |
must(target.e(f, newImage)) | |
must(f.Close()) | |
} | |
} | |
func getSourceImage() image.Image { | |
b, err := ioutil.ReadFile("icon.png") | |
panicOnError(err) | |
img, err := png.Decode(bytes.NewReader(b)) | |
panicOnError(err) | |
return img | |
} | |
func encodeGif(w io.Writer, i image.Image) error { | |
return gif.Encode(w, i, &gif.Options{}) | |
} | |
func getFilename(target target) string { | |
var filename = target.n | |
if strings.Contains(target.n, "%v") { | |
filename = fmt.Sprintf(target.n, target.w, target.h) | |
} | |
return filename | |
} | |
func must(err error) { | |
panicOnError(err) | |
} | |
func panicOnError(err error) { | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment