Skip to content

Commit

Permalink
Allow compression of JPEG files (Via TinyJPG)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterhellberg committed Jan 2, 2015
1 parent b720606 commit 0a9c5f6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Then you can run the command:
If only the input filename was specified, then the
output filename will be `tiny-<input.png>`

You can also compress JPEG files.

## License (MIT)

Copyright (c) 2014 [Peter Hellberg](http://c7.se/)
Expand Down
33 changes: 30 additions & 3 deletions tinypng/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ Then you can run the command:
If only the input filename was specified, then the
output filename will be 'tiny-<input.png>'
You can also compress JPEG files.
*/
package main

import (
"errors"
"fmt"
"image/jpeg"
"image/png"
"os"
"path"
Expand All @@ -43,9 +46,9 @@ func main() {
fatalError("Input file does not exist.")
}

// Verify that the input file is a PNG file
if !validPNGFile(inputFilename) {
fatalError("Input file is not a valid PNG file.")
// Verify that the input file is a PNG or JPEG file
if !validFileType(inputFilename) {
fatalError("Input file is not a valid PNG or JPEG file.")
}

// Then make sure that the output file doesn’t exist
Expand Down Expand Up @@ -115,6 +118,12 @@ func fileExists(name string) bool {
return true
}

// Valid file type

func validFileType(fn string) bool {
return validPNGFile(fn) || validJPEGFile(fn)
}

// PNG

func validPNGFile(fn string) bool {
Expand All @@ -133,6 +142,24 @@ func validPNGFile(fn string) bool {
return true
}

// JPEG

func validJPEGFile(fn string) bool {
jpegImage, err := os.Open(fn)

check(err)

defer jpegImage.Close()

_, err = jpeg.DecodeConfig(jpegImage)

if err != nil {
return false
}

return true
}

// Fatal

func check(err error) {
Expand Down

0 comments on commit 0a9c5f6

Please sign in to comment.