image_tinify 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +27 -0
- data/LICENSE.txt +21 -0
- data/README.md +40 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/image_tinify +67 -0
- data/image_tinify.gemspec +25 -0
- data/lib/command.rb +38 -0
- data/lib/image_tinify.rb +94 -0
- data/lib/image_tinify/version.rb +3 -0
- data/lib/key_manager.rb +15 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 462c5a064c5fd1e96ad7e6116576061e42dc9018e68ab3ca0e93dd4c77b25ea6
|
4
|
+
data.tar.gz: 332835bbcebbc1413a43cd4ec6592a2e0d66a91d78579ff8add7b3d3cb2baefe
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: aa2730445fea293aa9b3657fd63d56c66bea010eeedd60ee4e528d253c22df26045df87412b6c1ded789741b6ebf2d8e1956c26c6c17faad1d90223cb104751b
|
7
|
+
data.tar.gz: 2314974dfff07209c8957a2b0c336a265a5b643664a5616d774b98a93f4e821b3954fb6993c60a11657a82d8432adea47f690e8e45f4f08feaeb6c73ae1ddd6e
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
image_tinify (1.0.0)
|
5
|
+
chunky_png (~> 1.3.11)
|
6
|
+
claide (~> 1.0.3)
|
7
|
+
tinify (~> 1.5.0)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
chunky_png (1.3.11)
|
13
|
+
claide (1.0.3)
|
14
|
+
httpclient (2.8.3)
|
15
|
+
rake (12.3.2)
|
16
|
+
tinify (1.5.0)
|
17
|
+
httpclient (~> 2.6)
|
18
|
+
|
19
|
+
PLATFORMS
|
20
|
+
ruby
|
21
|
+
|
22
|
+
DEPENDENCIES
|
23
|
+
image_tinify!
|
24
|
+
rake (~> 12.0)
|
25
|
+
|
26
|
+
BUNDLED WITH
|
27
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 jiangzhuoyi
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# ImageTinify
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/image_tinify`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'image_tinify'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install image_tinify
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/image_tinify.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "image_tinify"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/exe/image_tinify
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'image_tinify'
|
4
|
+
require 'claide'
|
5
|
+
|
6
|
+
argv = CLAide::ARGV.new(ARGV)
|
7
|
+
|
8
|
+
ImageTinify::show_help(argv)
|
9
|
+
|
10
|
+
ImageTinify::show_version(argv)
|
11
|
+
|
12
|
+
should_list = argv.arguments.include?('list')
|
13
|
+
should_compress = argv.arguments.include?('compress')
|
14
|
+
|
15
|
+
ImageTinify::show_help if !(should_list || should_compress)
|
16
|
+
|
17
|
+
key = argv.option('key')
|
18
|
+
key ||= ImageTinify::random_key if should_compress
|
19
|
+
|
20
|
+
root = File.expand_path(argv.option('root') || '.')
|
21
|
+
|
22
|
+
image_tinify = ImageTinify::ImageTinify.new(key)
|
23
|
+
image_tinify.validate! if should_compress
|
24
|
+
image_tinify.root_only = argv.flag?('root-only') || false
|
25
|
+
|
26
|
+
# 所有图片
|
27
|
+
images = []
|
28
|
+
# 未压缩的图片
|
29
|
+
uncompressed_images = []
|
30
|
+
# 本次压缩的图片
|
31
|
+
compress_images = []
|
32
|
+
|
33
|
+
# 获取所有图片
|
34
|
+
image_tinify.find_all_images root, proc { |path|
|
35
|
+
images << path
|
36
|
+
}
|
37
|
+
|
38
|
+
# 获取所有未压缩图片
|
39
|
+
image_tinify.find_uncompressed_images(images) { |path|
|
40
|
+
uncompressed_images << path
|
41
|
+
}
|
42
|
+
|
43
|
+
begin
|
44
|
+
uncompressed_images.each { |path|
|
45
|
+
if should_compress
|
46
|
+
size_before = File.stat(path).size
|
47
|
+
if image_tinify.compress_image(path)
|
48
|
+
compress_images << path
|
49
|
+
image_tinify.mark_compressed(path)
|
50
|
+
size_after = File.stat(path).size
|
51
|
+
puts " 压缩成功, [#{size_before}] => [#{size_after}]"
|
52
|
+
else
|
53
|
+
puts " 压缩失败"
|
54
|
+
end
|
55
|
+
else
|
56
|
+
puts path
|
57
|
+
end
|
58
|
+
}
|
59
|
+
rescue Exception => e
|
60
|
+
puts e.to_s
|
61
|
+
ensure
|
62
|
+
puts ""
|
63
|
+
puts "- Completed."
|
64
|
+
puts " 图片总数: #{images.count}"
|
65
|
+
puts " 未压缩: #{uncompressed_images.count}"
|
66
|
+
puts " 本次压缩: #{compress_images.count}" if should_compress
|
67
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative 'lib/image_tinify/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "image_tinify"
|
5
|
+
spec.version = ImageTinify::VERSION
|
6
|
+
spec.authors = ["jiangzhuoyi"]
|
7
|
+
spec.email = ["jiangzhuoyi@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{Compress image.}
|
10
|
+
spec.description = %q{Compress image and indicates compressed.}
|
11
|
+
spec.homepage = "http://code.kaipao.cc/ios-team/image_tinify"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
16
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
end
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_runtime_dependency 'chunky_png', '~> 1.3.11'
|
23
|
+
spec.add_runtime_dependency 'tinify', '~> 1.5.0'
|
24
|
+
spec.add_runtime_dependency 'claide', '~> 1.0.3'
|
25
|
+
end
|
data/lib/command.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require "image_tinify/version"
|
2
|
+
|
3
|
+
module ImageTinify
|
4
|
+
|
5
|
+
def self.show_help(argv = nil)
|
6
|
+
if argv == nil || argv.flag?('help')
|
7
|
+
puts <<-HELP
|
8
|
+
|
9
|
+
使用方法:
|
10
|
+
|
11
|
+
$ image_tinify COMMAND [OPTIONS]
|
12
|
+
|
13
|
+
支持的操作命令:
|
14
|
+
|
15
|
+
+ list 查看所有未压缩的图片
|
16
|
+
+ compress 对未压缩的图片进行压缩,并标记为已压缩
|
17
|
+
|
18
|
+
选项:
|
19
|
+
|
20
|
+
--root 设置图片遍历的根目录,如 --root=~/foo/bar,默认当前目录
|
21
|
+
--key 设置 TinyPNG 的 key,如 --key=balabala
|
22
|
+
--root-only 只查找根目录
|
23
|
+
--version 查看版本号
|
24
|
+
--help 显示帮助
|
25
|
+
|
26
|
+
HELP
|
27
|
+
exit(0)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.show_version(argv)
|
32
|
+
if argv.flag?('version')
|
33
|
+
puts VERSION
|
34
|
+
exit(0)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
data/lib/image_tinify.rb
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
require "image_tinify/version"
|
2
|
+
require 'chunky_png'
|
3
|
+
require 'tinify'
|
4
|
+
|
5
|
+
require_relative 'command'
|
6
|
+
require_relative 'key_manager'
|
7
|
+
|
8
|
+
module ImageTinify
|
9
|
+
|
10
|
+
# 做标记的 Key
|
11
|
+
MARKUP_KEY = 'IMG_TINIFY'
|
12
|
+
|
13
|
+
# 做标记的 Value
|
14
|
+
MARKUP_VALUE = '1'
|
15
|
+
|
16
|
+
# 目标扩展名列表
|
17
|
+
TARGET_EXT = ['.png']
|
18
|
+
|
19
|
+
class ImageTinify
|
20
|
+
|
21
|
+
attr_accessor :root_only
|
22
|
+
|
23
|
+
def initialize(tiny_png_key = nil)
|
24
|
+
@validate = false
|
25
|
+
Tinify.key = tiny_png_key
|
26
|
+
end
|
27
|
+
|
28
|
+
def validate?
|
29
|
+
@validate
|
30
|
+
end
|
31
|
+
|
32
|
+
def validate!
|
33
|
+
@validate = Tinify.validate!
|
34
|
+
end
|
35
|
+
|
36
|
+
def relative_path(path)
|
37
|
+
path.gsub(@root_dir, '.')
|
38
|
+
end
|
39
|
+
|
40
|
+
# 查找所有图片
|
41
|
+
def _find_all_images(path, block)
|
42
|
+
if File.directory? path
|
43
|
+
return if @root_only && path != @root_dir
|
44
|
+
Dir.foreach(path)
|
45
|
+
.reject { |x| x.start_with? '.' }
|
46
|
+
.each { |name| _find_all_images File.join(path, name), block }
|
47
|
+
else
|
48
|
+
block.call path if TARGET_EXT.include? File.extname(path)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def find_all_images(path, block)
|
53
|
+
@root_dir = path
|
54
|
+
_find_all_images(path, block)
|
55
|
+
end
|
56
|
+
|
57
|
+
# 查找所有未压缩的图片
|
58
|
+
def find_uncompressed_images(images)
|
59
|
+
images.each { |file|
|
60
|
+
begin
|
61
|
+
stream = ChunkyPNG::Datastream.from_file(file)
|
62
|
+
|
63
|
+
compressed = stream.other_chunks
|
64
|
+
.select { |ck| ck.type.downcase == 'itxt' }
|
65
|
+
.reduce(false) { |m, ck|
|
66
|
+
m || (ck.keyword == MARKUP_KEY && ck.text == MARKUP_VALUE)
|
67
|
+
}
|
68
|
+
|
69
|
+
yield file if block_given? && !compressed
|
70
|
+
|
71
|
+
rescue => e
|
72
|
+
puts e.to_s
|
73
|
+
end
|
74
|
+
}
|
75
|
+
end
|
76
|
+
|
77
|
+
# 压缩图片
|
78
|
+
def compress_image(path)
|
79
|
+
validate! unless validate?
|
80
|
+
puts "- Compressing #{relative_path(path)}"
|
81
|
+
Tinify.from_file(path).to_file(path)
|
82
|
+
true
|
83
|
+
end
|
84
|
+
|
85
|
+
# 标记已压缩
|
86
|
+
def mark_compressed(path)
|
87
|
+
stream = ChunkyPNG::Datastream.from_file(path)
|
88
|
+
stream.other_chunks << ChunkyPNG::Chunk::InternationalText.new(MARKUP_KEY, MARKUP_VALUE)
|
89
|
+
stream.save(path)
|
90
|
+
true
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
end
|
data/lib/key_manager.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module ImageTinify
|
5
|
+
|
6
|
+
def self.random_key
|
7
|
+
uri = URI('http://code.kaipao.cc/snippets/8/raw')
|
8
|
+
resp = Net::HTTP.get_response(uri)
|
9
|
+
keys = JSON.parse(resp.body)
|
10
|
+
key = keys.sample
|
11
|
+
puts "Use [#{key['mail']}]'s key"
|
12
|
+
key['key']
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: image_tinify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- jiangzhuoyi
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-07-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: chunky_png
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.3.11
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.3.11
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: tinify
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.5.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.5.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: claide
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.0.3
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.0.3
|
55
|
+
description: Compress image and indicates compressed.
|
56
|
+
email:
|
57
|
+
- jiangzhuoyi@gmail.com
|
58
|
+
executables:
|
59
|
+
- image_tinify
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- Gemfile.lock
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- exe/image_tinify
|
72
|
+
- image_tinify.gemspec
|
73
|
+
- lib/command.rb
|
74
|
+
- lib/image_tinify.rb
|
75
|
+
- lib/image_tinify/version.rb
|
76
|
+
- lib/key_manager.rb
|
77
|
+
homepage: http://code.kaipao.cc/ios-team/image_tinify
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 2.3.0
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubygems_version: 3.1.2
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Compress image.
|
100
|
+
test_files: []
|