Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.9.0

* Added `Tinify::Format` constants for the media types `convert` accepts, including JPEG XL (`image/jxl`).

## 1.8.0

* Update development dependencies.
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
tinify (1.8.0)
tinify (1.9.0)
httpclient (>= 2.9, < 3)

GEM
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@ Tinify.key = "YOUR_API_KEY"
Tinify.from_file("unoptimized.png").to_file("optimized.png")
```

Convert to another format with the `Tinify::Format` constants (`WEBP`, `PNG`,
`JPEG`, `JPG`, `AVIF`, `JXL` and `ANY`):

```ruby
Tinify.from_file("unoptimized.png")
.convert(type: Tinify::Format::JXL)
.to_file("optimized.jxl")
```

Pass several and the API returns the smallest result:

```ruby
result = Tinify.from_file("unoptimized.png")
.convert(type: [Tinify::Format::JXL, Tinify::Format::WEBP])
.result

result.to_file("optimized.#{result.extension}")
```

The constants are plain strings, so `type: "image/jxl"` keeps working.

## Running tests

```
Expand Down
1 change: 1 addition & 0 deletions lib/tinify.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "tinify/version"
require "tinify/error"
require "tinify/format"

require "tinify/client"
require "tinify/result_meta"
Expand Down
39 changes: 39 additions & 0 deletions lib/tinify/format.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Tinify
# Media types that images can be converted to.
#
# Use these constants with Source#convert:
#
# Tinify.from_file("input.png")
# .convert(type: Tinify::Format::JXL)
# .to_file("output.jxl")
#
# Multiple types may be supplied, in which case the API returns the smallest
# result:
#
# convert(type: [Tinify::Format::JXL, Tinify::Format::WEBP])
module Format
# WebP.
WEBP = "image/webp".freeze

# PNG.
PNG = "image/png".freeze

# JPEG.
JPEG = "image/jpeg".freeze

# JPEG, under the alternative image/jpg media type the API also accepts.
# The API normalises this to image/jpeg, so the result is identical to
# JPEG. The two constants hold different strings but select the same
# format.
JPG = "image/jpg".freeze

# AVIF.
AVIF = "image/avif".freeze

# JPEG XL.
JXL = "image/jxl".freeze

# Wildcard, returns the smallest of the supported types.
ANY = "*/*".freeze
end
end
2 changes: 1 addition & 1 deletion lib/tinify/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Tinify
VERSION = "1.8.0"
VERSION = "1.9.0"
end
56 changes: 56 additions & 0 deletions test/tinify_format_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require File.expand_path("../helper", __FILE__)

describe Tinify::Format do
describe "constants" do
it "should expose the supported media types" do
assert_equal "image/webp", Tinify::Format::WEBP
assert_equal "image/png", Tinify::Format::PNG
assert_equal "image/jpeg", Tinify::Format::JPEG
assert_equal "image/jpg", Tinify::Format::JPG
assert_equal "image/avif", Tinify::Format::AVIF
assert_equal "image/jxl", Tinify::Format::JXL
assert_equal "*/*", Tinify::Format::ANY
end
end

describe "convert" do
before do
Tinify.key = "valid"

stub_request(:post, "https://api.tinify.com/shrink")
.with(basic_auth: ['api', 'valid'])
.to_return(
status: 201,
headers: { Location: "https://api.tinify.com/some/location" },
body: '{}')
end

it "should serialize a single format" do
stub_request(:post, "https://api.tinify.com/some/location")
.with(
basic_auth: ['api', 'valid'],
body: '{"convert":{"type":"image/jxl"}}')
.to_return(
status: 200,
body: "converted file")

source = Tinify::Source.from_buffer("png file")
.convert(type: Tinify::Format::JXL)
assert_equal "converted file", source.to_buffer
end

it "should serialize multiple formats" do
stub_request(:post, "https://api.tinify.com/some/location")
.with(
basic_auth: ['api', 'valid'],
body: '{"convert":{"type":["image/jxl","image/webp"]}}')
.to_return(
status: 200,
body: "converted file")

source = Tinify::Source.from_buffer("png file")
.convert(type: [Tinify::Format::JXL, Tinify::Format::WEBP])
assert_equal "converted file", source.to_buffer
end
end
end
Loading