Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
d0c41ec
fix: remove obsolete references to io/ioutil
GwynethLlewelyn Nov 10, 2023
55343d7
bump to v0.1.0
GwynethLlewelyn Nov 11, 2023
de06fee
feat: add proxy support
GwynethLlewelyn Nov 12, 2023
b4e3cba
warn: `replace` needs to be removed on go.mod
GwynethLlewelyn Nov 12, 2023
fe8c6bc
feat: starting to add CLI functions!
GwynethLlewelyn Nov 12, 2023
cd6ec72
chore: some fixes and a logo! (needs some editing)
GwynethLlewelyn Dec 4, 2023
71401bf
docs: add pangopher logo, some links, some spellchecking
GwynethLlewelyn Dec 4, 2023
be97fdf
doc: added logo on README
GwynethLlewelyn Dec 7, 2023
c49f49c
feat: messing up with default flag values!
GwynethLlewelyn Dec 27, 2023
f00af3f
feat: add eidth/height as parameters
GwynethLlewelyn Dec 27, 2023
4674f1a
bug: typo!
GwynethLlewelyn Dec 27, 2023
91b69b1
Teeny tiny changes
GwynethLlewelyn Mar 1, 2024
499c52c
I don’t even know what this is for….
GwynethLlewelyn Oct 1, 2024
3627ac4
bring it up to date
GwynethLlewelyn Oct 1, 2024
a1f3a1c
Put flags at the end?
GwynethLlewelyn Oct 1, 2024
8a33dfb
Bumping version
GwynethLlewelyn Oct 1, 2024
4ea410b
Major refactoring: commands replaced by flags
GwynethLlewelyn Oct 2, 2024
56ce8f3
Feat: return # of compression calls made so far
GwynethLlewelyn Oct 3, 2024
9dbd0f1
teeny tiny changes that don’t do much!
GwynethLlewelyn Oct 21, 2024
92d103e
refactor: use os.Stdout.Write() instead
GwynethLlewelyn Feb 13, 2025
b4d454f
Adding flag parsing with spf13/pflags
GwynethLlewelyn Feb 14, 2025
6fe5f3d
Add avif as valid image type
GwynethLlewelyn Feb 14, 2025
0deeb11
Fix unused proxyURL parameter
GwynethLlewelyn Feb 14, 2025
1828d2b
Add clarification on the Atoi() err to be ignored
GwynethLlewelyn Feb 14, 2025
edba128
First ready version for te!sting
GwynethLlewelyn Feb 14, 2025
eeae6b6
Grab API key from environment
GwynethLlewelyn Feb 14, 2025
c2b986b
Cosmetic changes
GwynethLlewelyn Feb 14, 2025
8991a60
It *works*!
GwynethLlewelyn Feb 14, 2025
0b85929
Whoops. Name changed on a TODO
GwynethLlewelyn Feb 15, 2025
2571af6
Fix: deal with empty filename by using stdio
GwynethLlewelyn Feb 16, 2025
cd3f5b9
TODO: bug with invalid command
GwynethLlewelyn Feb 16, 2025
c424be3
Fix: more edge cases, more debugging
GwynethLlewelyn Feb 16, 2025
37e00b6
Fix: stdin/stdout issues and help not appearing
GwynethLlewelyn Feb 19, 2025
1021641
Feat: deal better w/help when everything is empty
GwynethLlewelyn Feb 19, 2025
4fd240e
Bumping package versions
GwynethLlewelyn Mar 8, 2025
844b9eb
Fix: don’t show a pointer to a func on logs!
GwynethLlewelyn Mar 17, 2025
db91772
Refact: modernise `interface{}`to `any`
GwynethLlewelyn Jun 6, 2025
4ee2922
Doc: explain what this file is for.
GwynethLlewelyn Jun 6, 2025
2ffa8ed
Refactoring everything to use github.com/urfave/cli/v3
GwynethLlewelyn Jun 20, 2025
9fedd77
Refactoring, part II
GwynethLlewelyn Jun 28, 2025
bb1ec77
Chore: refactor to use cli/v3, part 2
GwynethLlewelyn Jun 28, 2025
e9d7fb9
Refactoring: missing still the bits handling files
GwynethLlewelyn Jun 29, 2025
77917b0
Attempt to refactor things differently.
GwynethLlewelyn Jul 8, 2025
96a026c
Debug: add init() just for setting a breakpoint for tests
GwynethLlewelyn Jul 12, 2025
fd4eb6d
Chore: replace `interface{}` with `any`
GwynethLlewelyn Jul 12, 2025
0d1cf8c
Docs: Markdown prettify test
GwynethLlewelyn Jul 12, 2025
a9a360d
Make sure debug execs are *not* exported!
GwynethLlewelyn Jul 12, 2025
6a0a60a
Bug: properly deal with 400 error
GwynethLlewelyn Jul 12, 2025
fc9684c
Revert init() tests, debugger can find it
GwynethLlewelyn Jul 12, 2025
a17aff3
Refactor: moved more init things into the proper CLI handlers
GwynethLlewelyn Jul 12, 2025
d561710
Fix: debug called wrong variables and paniced
GwynethLlewelyn Jul 13, 2025
beb96bd
Refactor: add missing functionality on resize, convert, transform
GwynethLlewelyn Jul 13, 2025
76d315f
Fix: Usage becomes now UsageText
GwynethLlewelyn Jul 13, 2025
f84a6c8
Code cleaning thanks to ChatGPT; also, more tests
GwynethLlewelyn Aug 22, 2025
08bf701
Chore: Go reserves `./testdata` for test purposes
GwynethLlewelyn Aug 23, 2025
a4c6c68
Refactor: rely less on globals!
GwynethLlewelyn Aug 23, 2025
11b4809
Docs: minimal information on th package
GwynethLlewelyn Aug 23, 2025
2b78fce
Completely forgot that `testdata`was ignord by Git
GwynethLlewelyn Aug 23, 2025
cebe71d
Chore: cleaning up .gitignore
GwynethLlewelyn Aug 23, 2025
495553c
Feat: justify help pages!
GwynethLlewelyn Oct 7, 2025
6edc2ca
Fix: stdin cannot be TTY!
GwynethLlewelyn Oct 7, 2025
e9a3499
Fix: if we have no builder name, adapt output str
GwynethLlewelyn Oct 7, 2025
7227b0c
Debug: log levels are not set, flags lose info (?)
GwynethLlewelyn Oct 8, 2025
4ea43c4
Bug: fixed zerolog output
GwynethLlewelyn Oct 9, 2025
967e905
Bug: if width or height are zero omit from JSON
GwynethLlewelyn Oct 9, 2025
1c98dbe
Bug: fix resize logic for method != scale
GwynethLlewelyn Oct 9, 2025
250ea9d
another failed attempt to get log level set
GwynethLlewelyn Oct 9, 2025
e575a20
Feat: 1st attempt to retrieve compression count
GwynethLlewelyn Oct 10, 2025
1f495e0
Whoops, forgot to save first!
GwynethLlewelyn Oct 10, 2025
8b78a7e
git debug: errors with tags (DUH!)
GwynethLlewelyn Oct 12, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
charset = utf-8
indent_style = tab
indent_size = tab
tab_width = 4
trim_trailing_whitespace = true

# The property below is not yet universally supported
[*.md]
max_line_length = 108
word_wrap = true
# Markdown sometimes uses two spaces at the end to
# mark soft line breaks
trim_trailing_whitespace = false

[*.css]
indent_style = space
indent_size = 2
62 changes: 57 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,57 @@
~*
# Stupid macOS temporary files

# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


Icon?

# Thumbnails
._*
nohup.out

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

# Stuff from the Nova editor
.nova
node_modules
package.json
package-lock.json
.eslintrc.yml
.prettierrc.json
.env

# logs
*.log
profile.out
coverage.html
coverage.txt
delay.txt


# executables
tinify-go

# Created by .ignore support plugin (hsz.mobi)
### Go template
# Binaries for programs and plugins
Expand All @@ -11,9 +65,7 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
testdata/output/*

# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/
.idea/
test_output/*.jpg
temp
# Temporary debugger files
__*
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
58 changes: 43 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,54 @@
![Tinify API client for Golang logo](testdata/assets/tinify-go-logo-pangopher-128x128.png)

# Tinify API client for Golang

[:book: ε›½ε†…ηš„ζœ‹ε‹ηœ‹θΏ™ι‡Œ](http://www.jianshu.com/p/5c4161db4ac8)

------
---

Golang client for the [Tinify API](https://tinypng.com/developers/reference), used for [TinyPNG](https://tinypng.com) and [TinyJPG](https://tinyjpg.com). Tinify compresses or resizes your images intelligently. Read more at [http://tinify.com](http://tinify.com).

Golang client for the Tinify API, used for [TinyPNG](https://tinypng.com) and [TinyJPG](https://tinyjpg.com). Tinify compresses or resize your images intelligently. Read more at [http://tinify.com](http://tinify.com).
The code on this repository is the work of volunteers who are neither affiliated with, nor endorsed by Tinify B.V., the makers of the Tinify API and of TinyPNG.

## Documentation

[Go to the documentation for the HTTP client](https://tinypng.com/developers/reference).

## Installation

Install the API client with `go get`.
Install the API client with `go install`.

```shell
go get -u github.com/gwpp/tinify-go
go install github.com/gwpp/tinify-go
```

Note that this repository will install two different things:

1. A port of the Tinify API to the Go programming language, which will be placed under its own directory `./tinify`, a stand-alone package/module named `Tinify`;
2. A client application, at the root of the repository, which will compile to an executable binary, using the `Tinify` package as an imported module.

The client application serves both as a testing tool and as a stand-alone binary. It is very loosely based on other, similar client applications [written for other programming languages](https://github.com/tinify/).

Remember, to use it, you need a valid Tinify API Key, passed wither with the `--key=XXXX` flag, or via the environment variable `TINIFY_API_KEY`.

## Usage

- About key
- About the TinyPNG API key

Get your API key from https://tinypng.com/developers
Get your API key from https://tinypng.com/developers

- Compress

```golang
func TestCompressFromFile(t *testing.T) {
Tinify.SetKey(Key)
source, err := Tinify.FromFile("./test.jpg")
source, err := Tinify.FromFile("./testdata/input/test.jpg")
if err != nil {
t.Error(err)
return
}

err = source.ToFile("./test_output/CompressFromFile.jpg")
err = source.ToFile("./testdata/output/CompressFromFile.jpg")
if err != nil {
t.Error(err)
return
Expand All @@ -44,11 +58,12 @@ go get -u github.com/gwpp/tinify-go
```

- Resize

```golang
func TestResizeFromBuffer(t *testing.T) {
Tinify.SetKey(Key)

buf, err := ioutil.ReadFile("./test.jpg")
buf, err := ioutil.ReadFile("./testdata/input/test.jpg")
if err != nil {
t.Error(err)
return
Expand All @@ -68,7 +83,7 @@ go get -u github.com/gwpp/tinify-go
return
}

err = source.ToFile("./test_output/ResizesFromBuffer.jpg")
err = source.ToFile("./testdata/output/ResizesFromBuffer.jpg")
if err != nil {
t.Error(err)
return
Expand All @@ -77,12 +92,11 @@ go get -u github.com/gwpp/tinify-go
}
```

- ***Notice:***

Tinify.ResizeMethod support `scale`, `fit` and `cover`. If used fit or cover, you must provide `both a width and a height`. But used scale, you must provide either a target width or a target height, `but not both`.
- **_Notice:_**

`Tinify.ResizeMethod()` supports `scale`, `fit` and `cover`. If you use either fit or cover, you must provide **both a width and a height**. But if you use scale, you must instead provide _either_ a target width _or_ a target height, **but not both**.

- More usage, please see [tinify_test.go](./tinify_test.go)
- For further usage, please read the comments in [tinify_test.go](./tinify_test.go)

## Running tests

Expand All @@ -91,6 +105,20 @@ cd $GOPATH/src/github.com/gwpp/tinify-go
go test
```

## Command-line utility

To build it:

```shell
cd $GOPATH/src/github.com/gwpp/tinify-go
go build -ldflags "-X main.TheBuilder=$USER"
# or, if you prefer, `go install`
```

and then invoke `./tinify-go --help` to get some basic instructions for the CLI.

## License

This software is licensed under the MIT License. [View the license](LICENSE).
This software is licensed under the [MIT License](LICENSE).

The logo (a cross-breed between the gopher mascot and the TinyPNG panda!) was provided courtesy of Microsoft's image generative AI, which is currently based on OpenAI's DALL-E technology.
58 changes: 58 additions & 0 deletions aux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Test suite for auxiiary functions.
// The main testing, done by gwpp, is under tinify_test.go
package main

import (
"testing"
)

var tests = []struct {
hex string
check bool
}{
{"#000000", true},
{"00FF", true},
{"0#0", false},
{"", false},
{"#", false},
{"#F", false},
{"B", false},
{"abcdef01", true},
{"#abcdef01", true},
{"#abcdef0", false},
{"#abcdefg0", false},
{"#0f0", false},
{"0x0F", false},
{"10000", false},
}

// Given a series of what we consider to be valid colours in hexadecimal, test if
// we got the expected results.
func TestIsValidHex(t *testing.T) {
for _, tc := range tests {
if isValidHex(tc.hex) != tc.check {
t.Fatalf("checked %q if it was valid hex or not and expected %t", tc.hex, tc.check)
}
}
}

func BenchmarkIsValidHex(b *testing.B) {
for b.Loop() {
for _, tc := range tests {
if isValidHex(tc.hex) != tc.check {
b.Fatalf("checked %q if it was valid hex or not and expected %t", tc.hex, tc.check)
}
}
}
}

/*
func BenchmarkIsValidHexChatGPT(b *testing.B) {
for b.Loop() {
for _, tc := range tests {
if isValidHexChatGPT(tc.hex) != tc.check {
b.Fatalf("checked %q if it was valid hex or not and expected %t", tc.hex, tc.check)
}
}
}
} */
Loading