mini_magick 3.8.0 → 4.0.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.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/lib/mini_gmagick.rb +2 -1
  3. data/lib/mini_magick/configuration.rb +136 -0
  4. data/lib/mini_magick/image/info.rb +122 -0
  5. data/lib/mini_magick/image.rb +408 -343
  6. data/lib/mini_magick/logger.rb +40 -0
  7. data/lib/mini_magick/shell.rb +48 -0
  8. data/lib/mini_magick/tool/animate.rb +14 -0
  9. data/lib/mini_magick/tool/compare.rb +14 -0
  10. data/lib/mini_magick/tool/composite.rb +14 -0
  11. data/lib/mini_magick/tool/conjure.rb +14 -0
  12. data/lib/mini_magick/tool/convert.rb +14 -0
  13. data/lib/mini_magick/tool/display.rb +14 -0
  14. data/lib/mini_magick/tool/identify.rb +14 -0
  15. data/lib/mini_magick/tool/import.rb +14 -0
  16. data/lib/mini_magick/tool/mogrify.rb +14 -0
  17. data/lib/mini_magick/tool/montage.rb +14 -0
  18. data/lib/mini_magick/tool/stream.rb +14 -0
  19. data/lib/mini_magick/tool.rb +250 -0
  20. data/lib/mini_magick/utilities.rb +25 -26
  21. data/lib/mini_magick/version.rb +15 -1
  22. data/lib/mini_magick.rb +43 -79
  23. data/spec/fixtures/animation.gif +0 -0
  24. data/spec/fixtures/default.jpg +0 -0
  25. data/spec/fixtures/exif.jpg +0 -0
  26. data/spec/fixtures/image.psd +0 -0
  27. data/spec/fixtures/not_an_image.rb +1 -0
  28. data/spec/lib/mini_magick/configuration_spec.rb +66 -0
  29. data/spec/lib/mini_magick/image_spec.rb +460 -0
  30. data/spec/lib/mini_magick/shell_spec.rb +66 -0
  31. data/spec/lib/mini_magick/tool_spec.rb +107 -0
  32. data/spec/lib/mini_magick/utilities_spec.rb +17 -0
  33. data/spec/lib/mini_magick_spec.rb +39 -0
  34. data/spec/spec_helper.rb +21 -0
  35. data/spec/support/helpers.rb +37 -0
  36. metadata +50 -52
  37. data/lib/mini_magick/command_builder.rb +0 -110
  38. data/lib/mini_magick/errors.rb +0 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7708f032a0b010ff31ed437bd5f28c5bdf4b269b
4
- data.tar.gz: 866153c83f7e87dfe3d1564ca32821221240d6a4
3
+ metadata.gz: 65e1d54473e9a429c269d44123e8a6977bf489fe
4
+ data.tar.gz: 7b2b4762e6ee9d6d47ade148b65df993c3b9e08e
5
5
  SHA512:
6
- metadata.gz: f1af9965e59c483a1566299d984f1657a2fc673c398a07e2fc07fb8e3d821eb9ec4f4f6fbcbcbb2f4497714f703f410ab64bc2b02ded74cb952b019171663ef7
7
- data.tar.gz: efe209dfdd7bedd32e7d44dc49cdda24e339a68e49fe4cfbfa900edb60c7a7b9c9627316df330d8ab90a6054e03f6db213c6360313d4ce26d35c925094437a11
6
+ metadata.gz: 4852bcf9522da52f7603a2ef96635312b91fef2e72dbcaa77dbec94dddd8e949e4c34d3e6ccc3da8588e42efe6c43b01cace0166d840f40c478966252f0390d0
7
+ data.tar.gz: 77bd9eac7c1a015ad7e6cc6de77657a210fc6b7bbb907928aafb08a0bb0b0c237e85a2ca4cd7ecd75cdfda7b55732352ebda096f46adb699ea62b52695385feb
data/lib/mini_gmagick.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require 'mini_magick'
2
- MiniMagick.processor = :gm
2
+
3
+ MiniMagick.processor = :graphicsmagick
@@ -0,0 +1,136 @@
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
+ # If set to `true`, it will `identify` every newly created image, and raise
50
+ # `MiniMagick::Invalid` if the image is not valid. Useful for validating
51
+ # user input, although it adds a bit of overhead. Defaults to `true`.
52
+ #
53
+ # @return [Boolean]
54
+ #
55
+ attr_accessor :validate_on_create
56
+ ##
57
+ # If set to `true`, it will `identify` every image that gets written (with
58
+ # {MiniMagick::Image#write}), and raise `MiniMagick::Invalid` if the image
59
+ # is not valid. Useful for validating that processing was sucessful,
60
+ # although it adds a bit of overhead. Defaults to `true`.
61
+ #
62
+ # @return [Boolean]
63
+ #
64
+ attr_accessor :validate_on_write
65
+
66
+ def self.extended(base)
67
+ base.validate_on_create = true
68
+ base.validate_on_write = true
69
+ end
70
+
71
+ ##
72
+ # @yield [self]
73
+ # @example
74
+ # MiniMagick.configure do |config|
75
+ # config.cli = :graphicsmagick
76
+ # config.timeout = 5
77
+ # end
78
+ #
79
+ def configure
80
+ yield self
81
+ end
82
+
83
+ def processor
84
+ @processor ||= ["mogrify", "gm"].detect do |processor|
85
+ MiniMagick::Utilities.which(processor)
86
+ end
87
+ end
88
+
89
+ def processor=(processor)
90
+ @processor = processor.to_s
91
+
92
+ unless ["mogrify", "gm"].include?(@processor)
93
+ raise ArgumentError,
94
+ "processor has to be set to either \"mogrify\" or \"gm\"" \
95
+ ", was set to #{@processor.inspect}"
96
+ end
97
+
98
+ reload_tools
99
+ end
100
+
101
+ def cli
102
+ @cli ||
103
+ case processor.to_s
104
+ when "mogrify" then :imagemagick
105
+ when "gm" then :graphicsmagick
106
+ end
107
+ end
108
+
109
+ def cli=(value)
110
+ @cli = value
111
+
112
+ unless [:imagemagick, :graphicsmagick].include?(@cli)
113
+ raise ArgumentError,
114
+ "CLI has to be set to either :imagemagick or :graphicsmagick" \
115
+ ", was set to #{@cli.inspect}"
116
+ end
117
+
118
+ reload_tools
119
+ end
120
+
121
+ def cli_path
122
+ @cli_path || @processor_path
123
+ end
124
+
125
+ def logger
126
+ @logger || MiniMagick::Logger.new($stdout)
127
+ end
128
+
129
+ private
130
+
131
+ def reload_tools
132
+ MiniMagick::Tool::OptionMethods.instances.each(&:reload_methods)
133
+ end
134
+
135
+ end
136
+ 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