oximg 0.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +202 -0
- data/README.md +94 -0
- data/lib/oximg/binary.rb +116 -0
- data/lib/oximg/version.rb +9 -0
- data/lib/oximg.rb +167 -0
- metadata +51 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: db7c5148333d62798fb1d68546bef2da49d165e5fd5c0467da50e9c47ebc6b5d
|
|
4
|
+
data.tar.gz: b54875a223ad16a6ce69e666926425066d3d48d37d5991bba1d7b34d0c95ef0b
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: b4b66769221e10f85e3611f6a129bc813582abb40abf0f2be4098213eddf1e17a9de0c15bfa9e8e578008e0af9c8406030968c697b712c06a7772e755f88a2c7
|
|
7
|
+
data.tar.gz: 2727aa6fd56a760a65cec6e73f9f610f334eb0f418a963a90675d3d39da7fe04472f86a2f83802d45d6cbb63c0c4cd43ebb0931c2e27239e78673d077ca0cd9d
|
data/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
data/README.md
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# oximg (Ruby)
|
|
2
|
+
|
|
3
|
+
Ruby bindings for [oximg](https://github.com/oximg/oximg) — image
|
|
4
|
+
compression and resizing in Rust. JPEG, PNG, WebP and AVIF, resized in
|
|
5
|
+
linear light at measurably higher output quality than the usual stack
|
|
6
|
+
(see the [benchmarks](https://github.com/oximg/oximg/blob/main/BENCH.md)).
|
|
7
|
+
|
|
8
|
+
The point of the gem: **no libvips, no ImageMagick**. Platform gems
|
|
9
|
+
bundle the executable, so `bundle install` is the whole installation —
|
|
10
|
+
no `apt-get install libvips42`, no ImageMagick policy file, no native
|
|
11
|
+
extension compiled against a system library that the next base-image
|
|
12
|
+
bump moves.
|
|
13
|
+
|
|
14
|
+
```ruby
|
|
15
|
+
gem "oximg"
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
```ruby
|
|
19
|
+
Oximg.resize("in.jpg", "out.jpg", width: 750)
|
|
20
|
+
Oximg.resize("in.jpg", "out.webp", width: 750, height: 500, quality: 80)
|
|
21
|
+
Oximg.probe("in.jpg")
|
|
22
|
+
#=> {content_type: "image/jpeg", format: :jpeg, width: 4000, height: 3000}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Resizing
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
Oximg.resize(source, destination, width: 0, height: 0,
|
|
29
|
+
quality: nil, format: nil, preset: nil)
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The source is fitted within `width` x `height` and **never enlarged**. A
|
|
33
|
+
zero axis is unconstrained, so `width: 750` alone is width-only — what
|
|
34
|
+
an `srcset` `w` descriptor means — and the default `0 x 0` re-encodes at
|
|
35
|
+
the source's own size, which is how you ask for compression without a
|
|
36
|
+
resize:
|
|
37
|
+
|
|
38
|
+
```ruby
|
|
39
|
+
Oximg.resize("photo.jpg", "smaller.jpg", quality: 70)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
| Option | Meaning |
|
|
43
|
+
|---|---|
|
|
44
|
+
| `width` / `height` | Non-negative Integers; `0` is an unconstrained axis |
|
|
45
|
+
| `quality` | 1–100, default 80. JPEG quality |
|
|
46
|
+
| `format` | `:jpg`, `:jpeg`, `:png`, `:webp`, `:avif`. Defaults to the destination's extension, else the source's own format |
|
|
47
|
+
| `preset` | `:jpegli` (default — maximum quality per byte), `:fast`, `:small` |
|
|
48
|
+
|
|
49
|
+
`resize` returns the destination path and raises `Oximg::ProcessingError`
|
|
50
|
+
on failure, carrying the binary's own message and exit status. Paths are
|
|
51
|
+
expanded before they reach the CLI, so an uploaded file called `-q.jpg`
|
|
52
|
+
cannot be read as a flag.
|
|
53
|
+
|
|
54
|
+
Everything the server tunes through `OXIMG_*` environment variables
|
|
55
|
+
applies here too, and is validated the same fail-closed way: a typo'd
|
|
56
|
+
knob is a startup error, never a silent default.
|
|
57
|
+
|
|
58
|
+
## Where the binary comes from
|
|
59
|
+
|
|
60
|
+
`Oximg.executable` resolves, first hit wins:
|
|
61
|
+
|
|
62
|
+
1. `Oximg.executable = "/path/to/oximg"`, or `OXIMG_BIN` — an explicit
|
|
63
|
+
path always wins, and a wrong one raises instead of falling through
|
|
64
|
+
to something else.
|
|
65
|
+
2. The binary bundled in this gem (platform gems ship one).
|
|
66
|
+
3. `oximg` on PATH — Homebrew, `cargo install oximg`, or a Docker image.
|
|
67
|
+
|
|
68
|
+
```ruby
|
|
69
|
+
Oximg.available? #=> true
|
|
70
|
+
Oximg.executable #=> "/…/gems/oximg-0.10.1-arm64-darwin/exe/oximg"
|
|
71
|
+
Oximg.version #=> "0.10.1" # the binary's version, not the gem's
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
The gem shells out to that binary — one process per image, which is
|
|
75
|
+
noise next to the encode itself and invisible inside the background job
|
|
76
|
+
where variants are generated anyway. An in-process native extension is a
|
|
77
|
+
later option; it would not change the API above.
|
|
78
|
+
|
|
79
|
+
## Rails
|
|
80
|
+
|
|
81
|
+
Rails and ActiveStorage integration — and URL building for a remote
|
|
82
|
+
oximg **server**, if you run one rather than processing locally — live
|
|
83
|
+
in the separate [`oximg-rails`](../oximg-rails) gem, mirroring how
|
|
84
|
+
`imgproxy` and `imgproxy-rails` are split.
|
|
85
|
+
|
|
86
|
+
## Versioning
|
|
87
|
+
|
|
88
|
+
The gem version tracks the oximg release it bundles, the same
|
|
89
|
+
convention the npm package uses. `Oximg.version` reports what the
|
|
90
|
+
resolved binary actually is, which can differ when it comes from PATH.
|
|
91
|
+
|
|
92
|
+
## License
|
|
93
|
+
|
|
94
|
+
Apache-2.0, same as oximg. See [LICENSE](LICENSE).
|
data/lib/oximg/binary.rb
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "open3"
|
|
4
|
+
require "rbconfig"
|
|
5
|
+
|
|
6
|
+
module Oximg
|
|
7
|
+
# Locates the oximg executable and runs it.
|
|
8
|
+
#
|
|
9
|
+
# Resolution order, first hit wins:
|
|
10
|
+
#
|
|
11
|
+
# 1. +Oximg.executable=+, or +OXIMG_BIN+ — an explicit path always
|
|
12
|
+
# wins, and a wrong one raises rather than falling through: a
|
|
13
|
+
# silent fallback is how a process ends up running a different
|
|
14
|
+
# build than the one it was configured with.
|
|
15
|
+
# 2. the binary bundled in this gem — platform gems
|
|
16
|
+
# (+oximg-x.y.z-arm64-darwin+ and friends) ship one, so
|
|
17
|
+
# +bundle install+ is the whole installation.
|
|
18
|
+
# 3. +oximg+ on PATH — a Homebrew, +cargo install+ or Docker-image
|
|
19
|
+
# binary. This is what the plain-Ruby gem resolves to.
|
|
20
|
+
#
|
|
21
|
+
# The gem deliberately does not declare +spec.executables+: RubyGems
|
|
22
|
+
# would generate a Ruby binstub around it, and this is a native
|
|
23
|
+
# binary, not a script. Ask for the path instead.
|
|
24
|
+
module Binary
|
|
25
|
+
EXE = "oximg#{RbConfig::CONFIG["EXEEXT"]}"
|
|
26
|
+
|
|
27
|
+
# Populated by the platform gems at package time; absent in the
|
|
28
|
+
# plain-Ruby gem.
|
|
29
|
+
BUNDLED = File.expand_path("../../exe/#{EXE}", __dir__)
|
|
30
|
+
|
|
31
|
+
NOT_FOUND = <<~MSG.tr("\n", " ").strip
|
|
32
|
+
The oximg executable was not found. Install a platform gem
|
|
33
|
+
(it bundles one), a release binary
|
|
34
|
+
(https://github.com/oximg/oximg/releases), `brew install
|
|
35
|
+
oximg/tap/oximg` or `cargo install oximg` — or point OXIMG_BIN at
|
|
36
|
+
the binary you already have.
|
|
37
|
+
MSG
|
|
38
|
+
|
|
39
|
+
class << self
|
|
40
|
+
attr_writer :path
|
|
41
|
+
|
|
42
|
+
def path
|
|
43
|
+
@path ||= discover || raise(ExecutableNotFound, NOT_FOUND)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# True when a binary can be found — for a boot-time check, or for
|
|
47
|
+
# code that falls back to another processor.
|
|
48
|
+
def available?
|
|
49
|
+
!path.nil?
|
|
50
|
+
rescue ExecutableNotFound
|
|
51
|
+
false
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# `oximg --version`, without the leading program name.
|
|
55
|
+
def version
|
|
56
|
+
@version ||= capture("--version").sub(/\Aoximg\s+/, "")
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Runs the binary with +args+ passed as a real argv — no shell, so
|
|
60
|
+
# nothing in a filename can be interpreted as syntax. Returns
|
|
61
|
+
# [stdout, stderr]; a non-zero exit raises with the binary's own
|
|
62
|
+
# stderr, which already names what it refused.
|
|
63
|
+
def run(*args)
|
|
64
|
+
out, err, status = Open3.capture3(path, *args)
|
|
65
|
+
unless status.success?
|
|
66
|
+
message = err.strip
|
|
67
|
+
message = "oximg exited #{status.exitstatus}" if message.empty?
|
|
68
|
+
raise ProcessingError.new(message, status.exitstatus)
|
|
69
|
+
end
|
|
70
|
+
[out, err]
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Resets the memoized lookup. For tests, and for a process that
|
|
74
|
+
# changes OXIMG_BIN after boot.
|
|
75
|
+
def reset!
|
|
76
|
+
@path = nil
|
|
77
|
+
@version = nil
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
def capture(*args)
|
|
83
|
+
out, err = run(*args)
|
|
84
|
+
out.strip.empty? ? err.strip : out.strip
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def discover
|
|
88
|
+
configured = ENV["OXIMG_BIN"]
|
|
89
|
+
unless configured.nil? || configured.strip.empty?
|
|
90
|
+
unless executable_file?(configured)
|
|
91
|
+
raise ExecutableNotFound, "OXIMG_BIN is set to #{configured.inspect}, which is not an executable file"
|
|
92
|
+
end
|
|
93
|
+
return configured
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
return BUNDLED if executable_file?(BUNDLED)
|
|
97
|
+
|
|
98
|
+
search_path
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def search_path
|
|
102
|
+
ENV["PATH"].to_s.split(File::PATH_SEPARATOR).each do |dir|
|
|
103
|
+
next if dir.empty?
|
|
104
|
+
|
|
105
|
+
candidate = File.join(dir, EXE)
|
|
106
|
+
return candidate if executable_file?(candidate)
|
|
107
|
+
end
|
|
108
|
+
nil
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def executable_file?(candidate)
|
|
112
|
+
File.file?(candidate) && File.executable?(candidate)
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Oximg
|
|
4
|
+
# The gem version tracks the oximg release whose URL grammar this
|
|
5
|
+
# client speaks — the same convention the npm package uses. A gem-only
|
|
6
|
+
# fix therefore waits for the next oximg release rather than shipping
|
|
7
|
+
# under a version that names a grammar it does not match.
|
|
8
|
+
VERSION = "0.10.1"
|
|
9
|
+
end
|
data/lib/oximg.rb
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "oximg/version"
|
|
4
|
+
require_relative "oximg/binary"
|
|
5
|
+
|
|
6
|
+
# Ruby bindings for oximg (https://github.com/oximg/oximg):
|
|
7
|
+
# high-performance image compression and resizing, in-process work for a
|
|
8
|
+
# Ruby app without libvips or ImageMagick on the box.
|
|
9
|
+
#
|
|
10
|
+
# Oximg.resize("in.jpg", "out.jpg", width: 750)
|
|
11
|
+
# Oximg.resize("in.jpg", "out.webp", width: 750, height: 500, quality: 80)
|
|
12
|
+
# Oximg.probe("in.jpg") #=> {content_type: "image/jpeg", format: :jpeg, ...}
|
|
13
|
+
#
|
|
14
|
+
# Rails/ActiveStorage integration, and URL building for a remote oximg
|
|
15
|
+
# server, live in the separate +oximg-rails+ gem.
|
|
16
|
+
module Oximg
|
|
17
|
+
class Error < StandardError; end
|
|
18
|
+
|
|
19
|
+
# No oximg executable could be found (or the configured one is not
|
|
20
|
+
# executable). See Binary for the resolution order.
|
|
21
|
+
class ExecutableNotFound < Error; end
|
|
22
|
+
|
|
23
|
+
# The executable ran and refused: an unreadable source, an unsupported
|
|
24
|
+
# format, a cap it was configured with. The message is the binary's
|
|
25
|
+
# own stderr, which already names what it refused.
|
|
26
|
+
class ProcessingError < Error
|
|
27
|
+
# Exit status: 2 is a usage or configuration error, 1 a processing
|
|
28
|
+
# failure.
|
|
29
|
+
attr_reader :status
|
|
30
|
+
|
|
31
|
+
def initialize(message, status = nil)
|
|
32
|
+
super(message)
|
|
33
|
+
@status = status
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Output formats the CLI accepts for -f/--format.
|
|
38
|
+
FORMATS = %i[jpg jpeg png webp avif].freeze
|
|
39
|
+
|
|
40
|
+
# Encoder profiles: jpegli maximizes quality per byte (the default),
|
|
41
|
+
# mozjpeg's fast/small trade differently.
|
|
42
|
+
PRESETS = %i[jpegli fast small].freeze
|
|
43
|
+
|
|
44
|
+
CONTENT_TYPE_FORMATS = {
|
|
45
|
+
"image/jpeg" => :jpeg,
|
|
46
|
+
"image/png" => :png,
|
|
47
|
+
"image/webp" => :webp,
|
|
48
|
+
"image/avif" => :avif
|
|
49
|
+
}.freeze
|
|
50
|
+
|
|
51
|
+
PROBE_LINE = /:\s+(\S+)\s+(\d+)x(\d+)\s+\(\d+\s+stored\s+pixels\)\s*\z/
|
|
52
|
+
|
|
53
|
+
class << self
|
|
54
|
+
# Absolute path to the executable in use; raises
|
|
55
|
+
# ExecutableNotFound if there is none.
|
|
56
|
+
def executable
|
|
57
|
+
Binary.path
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Pins the executable, overriding discovery and OXIMG_BIN.
|
|
61
|
+
def executable=(path)
|
|
62
|
+
Binary.reset!
|
|
63
|
+
Binary.path = path
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# True when an executable can be found — for a boot-time check, or
|
|
67
|
+
# for code that falls back to another processor.
|
|
68
|
+
def available?
|
|
69
|
+
Binary.available?
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Version of the executable, which is not necessarily this gem's
|
|
73
|
+
# VERSION when the binary comes from PATH.
|
|
74
|
+
def version
|
|
75
|
+
Binary.version
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Fits +source+ within +width+ x +height+ and writes the re-encoded
|
|
79
|
+
# image to +destination+. The source is never enlarged, and a zero
|
|
80
|
+
# axis is unconstrained — <tt>width: 750</tt> alone is width-only,
|
|
81
|
+
# and the default 0 x 0 re-encodes at the source's own size, which
|
|
82
|
+
# is how you ask for compression without a resize.
|
|
83
|
+
#
|
|
84
|
+
# The output format follows +format:+, else +destination+'s
|
|
85
|
+
# extension, else the source's own format. Returns the destination
|
|
86
|
+
# path.
|
|
87
|
+
def resize(source, destination, width: 0, height: 0, quality: nil, format: nil, preset: nil)
|
|
88
|
+
output = expand(destination, "destination")
|
|
89
|
+
Binary.run(*resize_argv(source, output,
|
|
90
|
+
width: width, height: height, quality: quality, format: format, preset: preset))
|
|
91
|
+
output
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Header-only inspection — no pixels are decoded. Returns
|
|
95
|
+
# <tt>{content_type:, format:, width:, height:}</tt>, where the
|
|
96
|
+
# dimensions are the stored ones (an EXIF rotation is not applied).
|
|
97
|
+
def probe(source)
|
|
98
|
+
out, = Binary.run("probe", expand(source, "source"))
|
|
99
|
+
match = out.match(PROBE_LINE)
|
|
100
|
+
raise ProcessingError, "unparsable probe output: #{out.inspect}" unless match
|
|
101
|
+
|
|
102
|
+
{
|
|
103
|
+
content_type: match[1],
|
|
104
|
+
format: CONTENT_TYPE_FORMATS[match[1]],
|
|
105
|
+
width: Integer(match[2]),
|
|
106
|
+
height: Integer(match[3])
|
|
107
|
+
}
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# The argv `resize` would run, minus the executable. Public because
|
|
111
|
+
# it is the honest way to see what a call turns into — and the way
|
|
112
|
+
# to test that without a binary present.
|
|
113
|
+
def resize_argv(source, destination, width: 0, height: 0, quality: nil, format: nil, preset: nil)
|
|
114
|
+
argv = [
|
|
115
|
+
"resize",
|
|
116
|
+
expand(source, "source"),
|
|
117
|
+
dimension(width, "width").to_s,
|
|
118
|
+
dimension(height, "height").to_s,
|
|
119
|
+
expand(destination, "destination")
|
|
120
|
+
]
|
|
121
|
+
argv.push("-q", bounded(quality, "quality", 1, 100).to_s) unless quality.nil?
|
|
122
|
+
argv.push("-f", token(format, FORMATS, "format")) unless format.nil?
|
|
123
|
+
argv.push("--preset", token(preset, PRESETS, "preset")) unless preset.nil?
|
|
124
|
+
argv
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
private
|
|
128
|
+
|
|
129
|
+
# Absolute paths throughout: a relative name beginning with "-"
|
|
130
|
+
# would otherwise reach the CLI's argument parser as a flag, and
|
|
131
|
+
# the caller rarely controls what an uploaded file is called.
|
|
132
|
+
def expand(path, name)
|
|
133
|
+
path = path.path if path.respond_to?(:path)
|
|
134
|
+
path = path.to_s
|
|
135
|
+
raise ArgumentError, "#{name} path is empty" if path.strip.empty?
|
|
136
|
+
|
|
137
|
+
File.expand_path(path)
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# No upper bound: the server's 8192 cap guards request shapes, and
|
|
141
|
+
# is not a limit of the pipeline the CLI drives.
|
|
142
|
+
def dimension(value, name)
|
|
143
|
+
unless value.is_a?(Integer) && value >= 0
|
|
144
|
+
raise ArgumentError, "#{name} must be a non-negative Integer, got #{value.inspect}"
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
value
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def bounded(value, name, min, max)
|
|
151
|
+
unless value.is_a?(Integer) && value >= min && value <= max
|
|
152
|
+
raise ArgumentError, "#{name} must be an Integer in #{min}..#{max}, got #{value.inspect}"
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
value
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def token(value, allowed, name)
|
|
159
|
+
token = value.to_s.downcase
|
|
160
|
+
unless allowed.include?(token.to_sym)
|
|
161
|
+
raise ArgumentError, "unknown #{name} #{value.inspect} (#{allowed.join("|")})"
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
token
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: oximg
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.10.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- oximg contributors
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 2026-08-06 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: 'Ruby bindings for oximg, the high-performance Rust image compression
|
|
13
|
+
and resizing engine: JPEG, PNG, WebP and AVIF, resized in linear light. Platform
|
|
14
|
+
gems bundle the executable, so there is no system library to install.'
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- LICENSE
|
|
20
|
+
- README.md
|
|
21
|
+
- lib/oximg.rb
|
|
22
|
+
- lib/oximg/binary.rb
|
|
23
|
+
- lib/oximg/version.rb
|
|
24
|
+
homepage: https://github.com/oximg/oximg
|
|
25
|
+
licenses:
|
|
26
|
+
- Apache-2.0
|
|
27
|
+
metadata:
|
|
28
|
+
homepage_uri: https://github.com/oximg/oximg
|
|
29
|
+
source_code_uri: https://github.com/oximg/oximg/tree/main/rubygem/oximg
|
|
30
|
+
changelog_uri: https://github.com/oximg/oximg/blob/main/CHANGELOG.md
|
|
31
|
+
bug_tracker_uri: https://github.com/oximg/oximg/issues
|
|
32
|
+
documentation_uri: https://github.com/oximg/oximg/blob/main/rubygem/oximg/README.md
|
|
33
|
+
rubygems_mfa_required: 'true'
|
|
34
|
+
rdoc_options: []
|
|
35
|
+
require_paths:
|
|
36
|
+
- lib
|
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
38
|
+
requirements:
|
|
39
|
+
- - ">="
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '3.1'
|
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
requirements: []
|
|
48
|
+
rubygems_version: 3.6.2
|
|
49
|
+
specification_version: 4
|
|
50
|
+
summary: Image compression and resizing for Ruby, without libvips or ImageMagick
|
|
51
|
+
test_files: []
|