simple_magick 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8721a16bca3964f1c9b62ecff896d1c3b51bfaf3
4
+ data.tar.gz: 7b5e87b257feca6d8ad365d463ab99b8cb361539
5
+ SHA512:
6
+ metadata.gz: 19a617f50e2f1583c82d4f22c95069b0dc4402c2eac8e128f900eb54c8eb2d6fcbbe9b1f4151f323db10fe92d7ff06b01c1da32292201055ab9cd214ca89b5e3
7
+ data.tar.gz: e28ac9c440dcd50b3ea36b1c1a8f62cf8884a68bb4ae8ac677d1e0719a55ca17875fb868fb65b42b452aec2d458e7ecf1aa0a28b25315ca42eb0f4e5d02df4bd
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .idea
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+ bench/tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_magick.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 sugamasao
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,87 @@
1
+ # SimpleMagick
2
+
3
+ SimpleMagick is Ultra Simple ImageMagick Wrapper.
4
+
5
+ ## Installation(Dependency)
6
+
7
+ requirements ImageMagick(use mogrify command)
8
+
9
+ ```
10
+ % brew install imagemagick
11
+ ```
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ gem 'simple_magick', github: 'sugamasao/simple_magick'
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ ## Supported versions
24
+
25
+ - Ruby 2.0.0 or later
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ require 'simple_magick'
31
+
32
+ if SimpleMagick.imagemagick_installed?
33
+ image = SimpleMagick::Image.new('/path/to/src_image.jpg')
34
+ image.resize '150x150'
35
+ image.convert! '/path/to/dest_image.jpg'
36
+ end
37
+ ```
38
+
39
+ using another option?
40
+
41
+ ```ruby
42
+ image = SimpleMagick::Image.new('/path/to/src_image.jpg')
43
+ image.additional_option 'type', 'Grayscale' # mogrify -type Grayscale /path/to/src_image.jpg
44
+ ```
45
+
46
+ ## Benchmark
47
+
48
+ ### Benchmark Spec
49
+
50
+ - OSX 10.8.5
51
+ - CPU: 2.53 GHz Intel Core i5
52
+ - Memory: 8GB
53
+
54
+ ### Version
55
+
56
+ - ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-darwin12.0]
57
+ - mini_magick (3.7.0)
58
+ - simple_magick (0.0.2)
59
+ - ImageMagick 6.8.7-7
60
+
61
+ ### How To Benchmark
62
+
63
+ Setup.
64
+
65
+ ```
66
+ % cd benchmark
67
+ % brew install imagemagick
68
+ % bundle install
69
+ ```
70
+
71
+ exec.
72
+
73
+ ```sh
74
+ % bundle exec ruby benchmark.rb
75
+ user system total real
76
+ simple_magick 0.080000 0.270000 28.530000 ( 29.687068)
77
+ mini_magick 0.410000 0.790000 31.300000 ( 37.115507)
78
+ ImageMagick 0.030000 0.170000 28.620000 ( 29.094316)
79
+ ```
80
+
81
+ ## Contributing
82
+
83
+ 1. Fork it ( http://github.com/<my-github-username>/simple_magick/fork )
84
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
85
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
86
+ 4. Push to the branch (`git push origin my-new-feature`)
87
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/benchmark/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem 'simple_magick', github: 'sugamasao/simple_magick'
4
+ gem 'mini_magick'
@@ -0,0 +1,43 @@
1
+ require 'benchmark'
2
+ require 'fileutils'
3
+ require 'simple_magick'
4
+ require 'mini_magick'
5
+
6
+ simple_magick_dir = File.join(__dir__, 'tmp', 'simple_magick')
7
+ mini_magick_dir = File.join(__dir__, 'tmp', 'mini_magick')
8
+ image_magick_dir = File.join(__dir__, 'tmp', 'image_magick')
9
+
10
+ [mini_magick_dir, simple_magick_dir, image_magick_dir].each do |dir|
11
+ FileUtils.mkdir_p dir unless File.directory? dir
12
+ end
13
+
14
+ sample_image = File.join('..', 'spec', 'assets', 'sample.jpg')
15
+
16
+ n = 100
17
+ Benchmark.bm(15) do |x|
18
+ x.report('simple_magick') {
19
+ n.times do |count|
20
+ image = SimpleMagick::Image.new(sample_image)
21
+ image.resize '150x150'
22
+ image.quality '50'
23
+ image.convert! File.join(simple_magick_dir, "image_#{count}.jpg")
24
+ end
25
+ }
26
+
27
+ x.report('mini_magick') {
28
+ n.times do |count|
29
+ image = MiniMagick::Image.open(sample_image)
30
+ image.resize '150x150'
31
+ image.quality '50'
32
+ image.write File.join(mini_magick_dir, "image_#{count}.jpg")
33
+ end
34
+ }
35
+
36
+ x.report('ImageMagick') {
37
+ n.times do |count|
38
+ result_image = File.join(image_magick_dir, "image_#{count}.jpg")
39
+ FileUtils.cp(sample_image, result_image)
40
+ `mogrify -resize 150x150 -quality 50 #{result_image}`
41
+ end
42
+ }
43
+ end
@@ -0,0 +1,107 @@
1
+ require 'simple_magick/version'
2
+ require 'fileutils'
3
+ require 'open3'
4
+ require 'shellwords'
5
+
6
+ module SimpleMagick
7
+ class ConvertError < StandardError;end
8
+
9
+ # mogrify is ImageMagick family tool.
10
+ # @note Windows Not Supported
11
+ # @return [Boolean] find mogrify command is true
12
+ def self.imagemagick_installed?
13
+ !`which mogrify`.split("\n").first.nil?
14
+ end
15
+
16
+ # Convert Image Class.
17
+ # @example Usage
18
+ # image = SimpleMagick::Image.new('/path/to/org.jpg')
19
+ # image.resize '150x'
20
+ # image.convert!('/path/to/thumb.jpg')
21
+ class Image
22
+
23
+ # @param [String] source_path Target Image File Path
24
+ # @param [Boolean] verbose print detailed information
25
+ def initialize(source_path, verbose = false)
26
+ @source_path = source_path
27
+ @verbose = verbose
28
+ @command = ['mogrify']
29
+ end
30
+
31
+ # @param [String] size resize size
32
+ def resize(size)
33
+ @command << "-resize #{command_escape(size)}"
34
+ end
35
+
36
+ # @param [String] num image quality
37
+ def quality(num)
38
+ @command << "-quality #{command_escape(num)}"
39
+ end
40
+
41
+ # @param [String] string define string.
42
+ def define(string)
43
+ @command << "-define #{command_escape(string)}"
44
+ end
45
+
46
+ # use other options.
47
+ # @param [String] option option name
48
+ # @param [String] value option value. default = ''
49
+ def additional_option(option, value = '')
50
+ @command << "-#{command_escape(option)} #{command_escape(value)}".strip
51
+ end
52
+
53
+ # run ImageMagick.
54
+ # @param [String] destination_path output image path
55
+ # @return [Void]
56
+ # @raise [SimpleMagick::ConvertError] mogrify command fail.
57
+ def convert!(destination_path)
58
+ file_copy(destination_path)
59
+ command = create_command(@command, destination_path)
60
+
61
+ # ex. % mogrify -resize 90 /path/to/resize_image.jpg
62
+ begin
63
+ puts "[INFO] #{command}" if @verbose
64
+ stdout, stderr, status = Open3.capture3(command)
65
+ rescue SystemCallError => e
66
+ raise ConvertError.new("mogrify error. exec command => [#{command}], Error Detail => [#{e.message}]")
67
+ end
68
+
69
+ unless status.success?
70
+ raise ConvertError.new("mogrify error. exec command => [#{command}], stdout => [#{stdout}], stderr => [#{stderr}]")
71
+ end
72
+ end
73
+
74
+ private
75
+
76
+ # copy source_path to destination_path
77
+ # @param [String] destination_path
78
+ # @return [Void]
79
+ # @raise [SimpleMagick::ConvertError] @source_path not found.
80
+ def file_copy(destination_path)
81
+ unless File.file? @source_path
82
+ raise ConvertError.new("Not Found input file. input file => [#{@source_path}]")
83
+ end
84
+
85
+ dest_dir = File.dirname(destination_path)
86
+ FileUtils.mkdir_p(dest_dir)
87
+ puts "[INFO] cp #{@source_path} #{destination_path}" if @verbose
88
+ FileUtils.cp(@source_path, destination_path)
89
+ end
90
+
91
+ # create exec command line
92
+ # @param [Array] command mogrify command
93
+ # @param [String] destination_path create image path
94
+ # @return [String] command String.
95
+ def create_command(command, destination_path)
96
+ command << Shellwords.escape(destination_path)
97
+ command.join(' ')
98
+ end
99
+
100
+ # option string to CLI Escape.
101
+ # @return [String] escaped string
102
+ def command_escape(value)
103
+ string_value = value.to_s.strip
104
+ Shellwords.escape(string_value) unless string_value.empty?
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleMagick
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simple_magick/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "simple_magick"
8
+ spec.version = SimpleMagick::VERSION
9
+ spec.authors = ["sugamasao"]
10
+ spec.email = ["sugamasao@gmail.com"]
11
+ spec.summary = %q{Simple ImageMagick Wrapper.}
12
+ spec.description = %q{Simple ImageMagick Wrapper.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "yard"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "simplecov"
26
+ end
Binary file
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+ require 'fileutils'
3
+
4
+ describe SimpleMagick do
5
+
6
+ describe '.imagemagick_installed?' do
7
+ it 'installed ImageMagick' do
8
+ expect(SimpleMagick).to be_imagemagick_installed
9
+ end
10
+ end
11
+
12
+ describe SimpleMagick::Image do
13
+ describe '#additional_command' do
14
+ context 'command and value' do
15
+ let(:asset_path) { File.join(__dir__, 'assets', 'sample.jpg') }
16
+
17
+ it 'set option and value' do
18
+ image = SimpleMagick::Image.new(asset_path)
19
+ expect(image.additional_option('type', 'Grayscale').last).to eq '-type Grayscale'
20
+ end
21
+
22
+ it 'set only option' do
23
+ image = SimpleMagick::Image.new(asset_path)
24
+ expect(image.additional_option('version').last).to eq '-version'
25
+ end
26
+ end
27
+ end
28
+
29
+ describe '#convert!' do
30
+ let(:asset_path) { File.join(__dir__, 'assets', 'sample.jpg') }
31
+ let(:result_path) { File.join(__dir__, 'assets', 'result', 'resize.jpg') }
32
+
33
+ before do
34
+ result_dir = File.dirname(result_path)
35
+ FileUtils.rm_r(result_dir) if File.directory?(result_dir)
36
+ end
37
+
38
+ context 'Resize Image' do
39
+ it 'Resize Success' do
40
+ image = SimpleMagick::Image.new(asset_path)
41
+ image.resize '150x'
42
+ image.quality '60'
43
+ image.convert! result_path
44
+ expect(File).to be_file(result_path)
45
+ end
46
+ end
47
+
48
+ context 'Not Found assets_path' do
49
+ let(:not_found_path) { asset_path + '.not_found_ext' }
50
+ it 'raise SimpleMagick::ConvertError' do
51
+ image = SimpleMagick::Image.new(not_found_path)
52
+ image.resize '150x'
53
+ image.quality '60'
54
+ expect {
55
+ image.convert! result_path
56
+ }.to raise_error(SimpleMagick::ConvertError, /Not Found/)
57
+ end
58
+ end
59
+
60
+ end
61
+ end
62
+
63
+ end
@@ -0,0 +1,22 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ require 'simple_magick'
5
+
6
+ # This file was generated by the `rspec --init` command. Conventionally, all
7
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
8
+ # Require this file using `require "spec_helper"` to ensure that it is only
9
+ # loaded once.
10
+ #
11
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
12
+ RSpec.configure do |config|
13
+ config.treat_symbols_as_metadata_keys_with_true_values = true
14
+ config.run_all_when_everything_filtered = true
15
+ config.filter_run :focus
16
+
17
+ # Run specs in random order to surface order dependencies. If you find an
18
+ # order dependency and want to debug it, you can fix the order by providing
19
+ # the seed, which is printed after each run.
20
+ # --seed 1234
21
+ config.order = 'random'
22
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_magick
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - sugamasao
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: yard
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Simple ImageMagick Wrapper.
84
+ email:
85
+ - sugamasao@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - benchmark/Gemfile
97
+ - benchmark/benchmark.rb
98
+ - lib/simple_magick.rb
99
+ - lib/simple_magick/version.rb
100
+ - simple_magick.gemspec
101
+ - spec/assets/sample.jpg
102
+ - spec/simple_magick_spec.rb
103
+ - spec/spec_helper.rb
104
+ homepage: ''
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.2.0
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Simple ImageMagick Wrapper.
128
+ test_files:
129
+ - spec/assets/sample.jpg
130
+ - spec/simple_magick_spec.rb
131
+ - spec/spec_helper.rb
132
+ has_rdoc: