mini_magick 3.8.0 → 4.1.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: 8921e089d8d63d1391e76876bc63b4e3ff953706
4
+ data.tar.gz: 38930195578ded9545d86b4225718671a9cd8b6a
5
5
  SHA512:
6
- metadata.gz: f1af9965e59c483a1566299d984f1657a2fc673c398a07e2fc07fb8e3d821eb9ec4f4f6fbcbcbb2f4497714f703f410ab64bc2b02ded74cb952b019171663ef7
7
- data.tar.gz: efe209dfdd7bedd32e7d44dc49cdda24e339a68e49fe4cfbfa900edb60c7a7b9c9627316df330d8ab90a6054e03f6db213c6360313d4ce26d35c925094437a11
6
+ metadata.gz: 39cc0fccd098a964a02be8e2cde7565d492ab9a5831df89a2f1b90d751d67368c13584c05ef3253aab51f8aecf107a52ca489a6849bd32e80a334869500f4628
7
+ data.tar.gz: 9c620a81d8b8e601a172d3b8333f4020048e2b7d385ce40fe85caff7f6df7480f1e08c8046d8fa10d66164eb264efe06d9303f111444f55c6193546ca34ab0f2
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,122 @@
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
+ else
29
+ raw(value)
30
+ end
31
+ end
32
+
33
+ def clear
34
+ @info.clear
35
+ end
36
+
37
+ private
38
+
39
+ def cheap_info(value)
40
+ @info.fetch(value) do
41
+ format, width, height, size = self["%m %w %h %b"].split(" ")
42
+
43
+ @info.update(
44
+ "format" => format,
45
+ "width" => Integer(width),
46
+ "height" => Integer(height),
47
+ "dimensions" => [Integer(width), Integer(height)],
48
+ "size" => size.to_i,
49
+ )
50
+
51
+ @info.fetch(value)
52
+ end
53
+ end
54
+
55
+ def colorspace
56
+ @info.fetch("colorspace") do
57
+ @info["colorspace"] = self["%r"]
58
+ end
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.fetch("exif") do
79
+ output = self["%[EXIF:*]"]
80
+ pairs = output.gsub(/^exif:/, "").split("\n").map { |line| line.split("=") }
81
+ exif = 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
+ @info["exif"] = exif
91
+ end
92
+ end
93
+
94
+ def raw(value)
95
+ key = "raw:#{value}"
96
+ @info.fetch(key) do
97
+ @info[key] = identify { |b| b.format(value) }
98
+ end
99
+ end
100
+
101
+ def signature
102
+ @info.fetch("signature") do
103
+ @info["signature"] = self["%#"]
104
+ end
105
+ end
106
+
107
+ def identify
108
+ MiniMagick::Tool::Identify.new do |builder|
109
+ yield builder if block_given?
110
+ builder << "#{@path}[0]"
111
+ end
112
+ end
113
+
114
+ def decode_comma_separated_ascii_characters(encoded_value)
115
+ return encoded_value unless encoded_value.include?(',')
116
+
117
+ encoded_value.scan(/\d+/).map(&:to_i).map(&:chr).join
118
+ end
119
+
120
+ end
121
+ end
122
+ end