mini_magick 3.8.0 → 4.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7708f032a0b010ff31ed437bd5f28c5bdf4b269b
4
- data.tar.gz: 866153c83f7e87dfe3d1564ca32821221240d6a4
3
+ metadata.gz: 21f140f463e39c25ff85d153dde2633ae64253fc
4
+ data.tar.gz: 09fcec5741441ab2b4d7f8346ddfa2d6d5c40a51
5
5
  SHA512:
6
- metadata.gz: f1af9965e59c483a1566299d984f1657a2fc673c398a07e2fc07fb8e3d821eb9ec4f4f6fbcbcbb2f4497714f703f410ab64bc2b02ded74cb952b019171663ef7
7
- data.tar.gz: efe209dfdd7bedd32e7d44dc49cdda24e339a68e49fe4cfbfa900edb60c7a7b9c9627316df330d8ab90a6054e03f6db213c6360313d4ce26d35c925094437a11
6
+ metadata.gz: 6daee1a467de3117d0278a79d6006446c35bcf64afc746fa7ae04a45ee53b26334c171d7f73e58e0de97792496d82e4a00c336511e62427c55bb97e67f278f40
7
+ data.tar.gz: 87a1ccb4e52bbfd6fcfe4f4251f4d6711dd79aaa4d935b10d35c04db27ff11236dfca5422efb67c704c6953e54939751d4419cf1ca9d0bc63d91787b473ad3d3
data/lib/mini_gmagick.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require 'mini_magick'
2
+
2
3
  MiniMagick.processor = :gm
@@ -0,0 +1,158 @@
1
+ require 'mini_magick/utilities'
2
+
3
+ module MiniMagick
4
+ module Configuration
5
+
6
+ ##
7
+ # Set whether you want to use [ImageMagick](http://www.imagemagick.org) or
8
+ # [GraphicsMagick](http://www.graphicsmagick.org).
9
+ #
10
+ # @return [Symbol] `:imagemagick` or `:minimagick`
11
+ #
12
+ attr_accessor :cli
13
+ # @private (for backwards compatibility)
14
+ attr_accessor :processor
15
+
16
+ ##
17
+ # If you don't have the CLI tools in your PATH, you can set the path to the
18
+ # executables.
19
+ #
20
+ # @return [String]
21
+ #
22
+ attr_accessor :cli_path
23
+ # @private (for backwards compatibility)
24
+ attr_accessor :processor_path
25
+
26
+ ##
27
+ # If you don't want commands to take too long, you can set a timeout (in
28
+ # seconds).
29
+ #
30
+ # @return [Integer]
31
+ #
32
+ attr_accessor :timeout
33
+ ##
34
+ # When set to `true`, it outputs each command to STDOUT in their shell
35
+ # version.
36
+ #
37
+ # @return [Boolean]
38
+ #
39
+ attr_accessor :debug
40
+ ##
41
+ # Logger for {#debug}, default is `MiniMagick::Logger.new(STDOUT)`, but
42
+ # you can override it, for example if you want the logs to be written to
43
+ # a file.
44
+ #
45
+ # @return [Logger]
46
+ #
47
+ attr_accessor :logger
48
+
49
+ ##
50
+ # If set to `true`, it will `identify` every newly created image, and raise
51
+ # `MiniMagick::Invalid` if the image is not valid. Useful for validating
52
+ # user input, although it adds a bit of overhead. Defaults to `true`.
53
+ #
54
+ # @return [Boolean]
55
+ #
56
+ attr_accessor :validate_on_create
57
+ ##
58
+ # If set to `true`, it will `identify` every image that gets written (with
59
+ # {MiniMagick::Image#write}), and raise `MiniMagick::Invalid` if the image
60
+ # is not valid. Useful for validating that processing was sucessful,
61
+ # although it adds a bit of overhead. Defaults to `true`.
62
+ #
63
+ # @return [Boolean]
64
+ #
65
+ attr_accessor :validate_on_write
66
+
67
+ ##
68
+ # If set to `false`, it will not raise errors when ImageMagick returns
69
+ # status code different than 0. Defaults to `true`.
70
+ #
71
+ # @return [Boolean]
72
+ #
73
+ attr_accessor :whiny
74
+
75
+ ##
76
+ # Instructs MiniMagick how to execute the shell commands. Available
77
+ # APIs are "open3" (default) and "posix-spawn" (requires the "posix-spawn"
78
+ # gem).
79
+ #
80
+ # @return [String]
81
+ #
82
+ attr_accessor :shell_api
83
+
84
+ def self.extended(base)
85
+ base.validate_on_create = true
86
+ base.validate_on_write = true
87
+ base.whiny = true
88
+ base.shell_api = "open3"
89
+ end
90
+
91
+ ##
92
+ # @yield [self]
93
+ # @example
94
+ # MiniMagick.configure do |config|
95
+ # config.cli = :graphicsmagick
96
+ # config.timeout = 5
97
+ # end
98
+ #
99
+ def configure
100
+ yield self
101
+ end
102
+
103
+ def processor
104
+ @processor ||= ["mogrify", "gm"].detect do |processor|
105
+ MiniMagick::Utilities.which(processor)
106
+ end
107
+ end
108
+
109
+ def processor=(processor)
110
+ @processor = processor.to_s
111
+
112
+ unless ["mogrify", "gm"].include?(@processor)
113
+ raise ArgumentError,
114
+ "processor has to be set to either \"mogrify\" or \"gm\"" \
115
+ ", was set to #{@processor.inspect}"
116
+ end
117
+
118
+ reload_tools
119
+ end
120
+
121
+ def cli
122
+ @cli ||
123
+ case processor.to_s
124
+ when "mogrify" then :imagemagick
125
+ when "gm" then :graphicsmagick
126
+ else
127
+ raise MiniMagick::Error, "ImageMagick/GraphicsMagick is not installed"
128
+ end
129
+ end
130
+
131
+ def cli=(value)
132
+ @cli = value
133
+
134
+ unless [:imagemagick, :graphicsmagick].include?(@cli)
135
+ raise ArgumentError,
136
+ "CLI has to be set to either :imagemagick or :graphicsmagick" \
137
+ ", was set to #{@cli.inspect}"
138
+ end
139
+
140
+ reload_tools
141
+ end
142
+
143
+ def cli_path
144
+ @cli_path || @processor_path
145
+ end
146
+
147
+ def logger
148
+ @logger || MiniMagick::Logger.new($stdout)
149
+ end
150
+
151
+ private
152
+
153
+ def reload_tools
154
+ MiniMagick::Tool::OptionMethods.instances.each(&:reload_methods)
155
+ end
156
+
157
+ end
158
+ end
@@ -0,0 +1,139 @@
1
+ module MiniMagick
2
+ class Image
3
+ # @private
4
+ class Info
5
+ ASCII_ENCODED_EXIF_KEYS = %w[ExifVersion FlashPixVersion]
6
+
7
+ def initialize(path)
8
+ @path = path
9
+ @info = {}
10
+ end
11
+
12
+ def [](value, *args)
13
+ case value
14
+ when "format", "width", "height", "dimensions", "size"
15
+ cheap_info(value)
16
+ when "colorspace"
17
+ colorspace
18
+ when "mime_type"
19
+ mime_type
20
+ when "resolution"
21
+ resolution(*args)
22
+ when "signature"
23
+ signature
24
+ when /^EXIF\:/i
25
+ raw_exif(value)
26
+ when "exif"
27
+ exif
28
+ when "details"
29
+ details
30
+ else
31
+ raw(value)
32
+ end
33
+ end
34
+
35
+ def clear
36
+ @info.clear
37
+ end
38
+
39
+ private
40
+
41
+ def cheap_info(value)
42
+ @info.fetch(value) do
43
+ format, width, height, size = self["%m %w %h %b"].split(" ")
44
+
45
+ @info.update(
46
+ "format" => format,
47
+ "width" => Integer(width),
48
+ "height" => Integer(height),
49
+ "dimensions" => [Integer(width), Integer(height)],
50
+ "size" => size.to_i,
51
+ )
52
+
53
+ @info.fetch(value)
54
+ end
55
+ end
56
+
57
+ def colorspace
58
+ @info["colorspace"] ||= self["%r"]
59
+ end
60
+
61
+ def mime_type
62
+ "image/#{self["format"].downcase}"
63
+ end
64
+
65
+ def resolution(unit = nil)
66
+ output = identify do |b|
67
+ b.units unit if unit
68
+ b.format "%x %y"
69
+ end
70
+ output.split(" ").map(&:to_i)
71
+ end
72
+
73
+ def raw_exif(value)
74
+ self["%[#{value}]"]
75
+ end
76
+
77
+ def exif
78
+ @info["exif"] ||= (
79
+ output = self["%[EXIF:*]"]
80
+ pairs = output.gsub(/^exif:/, "").split("\n").map { |line| line.split("=") }
81
+ Hash[pairs].tap do |hash|
82
+ ASCII_ENCODED_EXIF_KEYS.each do |key|
83
+ next unless hash.has_key?(key)
84
+
85
+ value = hash[key]
86
+ hash[key] = decode_comma_separated_ascii_characters(value)
87
+ end
88
+ end
89
+ )
90
+ end
91
+
92
+ def raw(value)
93
+ @info["raw:#{value}"] ||= identify { |b| b.format(value) }
94
+ end
95
+
96
+ def signature
97
+ @info["signature"] ||= self["%#"]
98
+ end
99
+
100
+ def details
101
+ @info["details"] ||= (
102
+ details_string = identify(&:verbose)
103
+ details_string.each_line.with_object([]).inject({}) do |details_hash, (line, key_stack)|
104
+ level = line[/^\s*/].length / 2 - 1
105
+ next details_hash if level == -1 # we ignore the root level
106
+ hash = key_stack.inject(details_hash) { |hash, key| hash.fetch(key) }
107
+ key, value = line.split(":", 2).map(&:strip)
108
+
109
+ if level == key_stack.size
110
+ if value.empty?
111
+ hash[key] = {}
112
+ key_stack.push key
113
+ else
114
+ hash[key] = value
115
+ end
116
+ elsif level < key_stack.size
117
+ key_stack.pop
118
+ end
119
+
120
+ details_hash
121
+ end
122
+ )
123
+ end
124
+
125
+ def identify
126
+ MiniMagick::Tool::Identify.new do |builder|
127
+ yield builder if block_given?
128
+ builder << "#{@path}[0]"
129
+ end
130
+ end
131
+
132
+ def decode_comma_separated_ascii_characters(encoded_value)
133
+ return encoded_value unless encoded_value.include?(',')
134
+ encoded_value.scan(/\d+/).map(&:to_i).map(&:chr).join
135
+ end
136
+
137
+ end
138
+ end
139
+ end