png_quantizator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
4
+
5
+ before_install:
6
+ - wget http://pngquant.org/pngquant_1.7.1-1_i386.deb
7
+ - sudo dpkg -i pngquant_1.7.1-1_i386.deb
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+ gem 'rake'
3
+
4
+ # Specify your gem's dependencies in png_quantizator.gemspec
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Roger Campos
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # PngQuantizator
2
+ [![Build Status](https://secure.travis-ci.org/rogercampos/png_quantizator.png)](http://travis-ci.org/rogercampos/png_quantizator)
3
+
4
+
5
+ PngQuantizator is a little wrapper around [ pngquant ](http://pngquant.org/).
6
+ Gives you a nice API to interact with the binary and meaningful exceptions in
7
+ the ruby world if `pngquant` throws an error.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'png_quantizator'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install png_quantizator
22
+
23
+ ## Usage
24
+
25
+ ```
26
+ file = PngQuantizator::Image.new("/path/to/image.png")
27
+
28
+ # Quantize to a new file
29
+ file.quantize_to("/path/to/destination.png")
30
+
31
+ # Quantize in place
32
+ file.quantize!
33
+ ```
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it
38
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
39
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
40
+ 4. Push to the branch (`git push origin my-new-feature`)
41
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,3 @@
1
+ module PngQuantizator
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,60 @@
1
+ require "png_quantizator/version"
2
+ require 'open3'
3
+ require 'fileutils'
4
+ require 'securerandom'
5
+ require 'tempfile'
6
+
7
+ module PngQuantizator
8
+ class PngQuantError < StandardError; end
9
+
10
+ class Image
11
+ def initialize(file_path)
12
+ raise ArgumentError, "could not find #{file_path}" unless File.file?(file_path)
13
+
14
+ @file_path = file_path
15
+ end
16
+
17
+ def quantize!
18
+ Tempfile.open([SecureRandom.hex(16), ".png"]) do |temp_path|
19
+ res = quantize_to(temp_path)
20
+ FileUtils.cp(temp_path, @file_path)
21
+
22
+ temp_path.close
23
+ temp_path.delete
24
+ res
25
+ end
26
+ end
27
+
28
+ def quantize_to(destination_path)
29
+ raise PngQuantError, "pngquant not found" unless which("pngquant")
30
+
31
+ exit_code, err_msg = Open3.popen3("pngquant 256") do |stdin, stdout, stderr, wait_thr|
32
+ stdin.write(File.read(@file_path))
33
+
34
+ File.open(destination_path, "w") do |f|
35
+ f.write stdout.gets(nil)
36
+ f.flush
37
+ end
38
+
39
+ [wait_thr.value, stderr.gets(nil)]
40
+ end
41
+
42
+ raise(PngQuantError, err_msg) if exit_code != 0
43
+ true
44
+ end
45
+
46
+ private
47
+
48
+ def which(cmd)
49
+ exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
50
+ ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
51
+ exts.each { |ext|
52
+ exe = File.join(path, "#{cmd}#{ext}")
53
+ return exe if File.executable? exe
54
+ }
55
+ end
56
+
57
+ nil
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'png_quantizator/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "png_quantizator"
8
+ gem.version = PngQuantizator::VERSION
9
+ gem.authors = ["Roger Campos"]
10
+ gem.email = ["roger@itnig.net"]
11
+ gem.description = %q{Small wrapper around pngquant}
12
+ gem.summary = %q{Small wrapper around pngquant}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency 'rspec'
21
+ end
Binary file
Binary file
@@ -0,0 +1,50 @@
1
+ require 'png_quantizator'
2
+
3
+ describe PngQuantizator::Image do
4
+ let(:test_jpg) { File.join(File.dirname(__FILE__), "../data/sample.jpg") }
5
+ let(:test_png) { File.join(File.dirname(__FILE__), "../data/sample.png") }
6
+
7
+ describe "#quantize_to" do
8
+ it "should not work with jpg files" do
9
+ quantized = PngQuantizator::Image.new(test_jpg)
10
+ expect { quantized.quantize_to("/tmp/cucota.png") }.to raise_error(PngQuantizator::PngQuantError)
11
+ end
12
+
13
+ it "should work with png files" do
14
+ quantized = PngQuantizator::Image.new(test_png)
15
+ quantized.quantize_to("/tmp/cucota.png").should be_true
16
+
17
+ File.file?("/tmp/cucota.png").should be_true
18
+ File.size("/tmp/cucota.png").should < File.size(test_png)
19
+
20
+ FileUtils.rm_rf("/tmp/cucota.png")
21
+ end
22
+ end
23
+
24
+ describe "#quantize!" do
25
+ let(:original_file_path) { "/tmp/abracadabra.png" }
26
+
27
+ before do
28
+ FileUtils.cp(test_png, original_file_path)
29
+ end
30
+
31
+ after do
32
+ FileUtils.rm_rf(original_file_path)
33
+ end
34
+
35
+ it "should pngquantize in place" do
36
+ foo = File.size(original_file_path)
37
+ PngQuantizator::Image.new(original_file_path).quantize!
38
+
39
+ File.file?(original_file_path).should be_true
40
+ File.size(original_file_path).should < foo
41
+ end
42
+
43
+ it "should not leave residual files in tmp" do
44
+ foo = `ls -1 /tmp/*.png | wc -l`
45
+ PngQuantizator::Image.new(original_file_path).quantize!
46
+ foo.should == `ls -1 /tmp/*.png | wc -l`
47
+ end
48
+ end
49
+ end
50
+
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: png_quantizator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Roger Campos
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Small wrapper around pngquant
31
+ email:
32
+ - roger@itnig.net
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - .travis.yml
39
+ - Gemfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - lib/png_quantizator.rb
44
+ - lib/png_quantizator/version.rb
45
+ - png_quantizator.gemspec
46
+ - spec/data/sample.jpg
47
+ - spec/data/sample.png
48
+ - spec/png_quantizator/image_spec.rb
49
+ homepage: ''
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ segments:
62
+ - 0
63
+ hash: -2814703
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ segments:
71
+ - 0
72
+ hash: -2814703
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 1.8.24
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Small wrapper around pngquant
79
+ test_files:
80
+ - spec/data/sample.jpg
81
+ - spec/data/sample.png
82
+ - spec/png_quantizator/image_spec.rb