mini_magick 3.2 → 3.2.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of mini_magick might be problematic. Click here for more details.

data/README.rdoc CHANGED
@@ -87,6 +87,14 @@ For more on the format command see
87
87
  http://www.imagemagick.org/script/command-line-options.php#format
88
88
 
89
89
 
90
+ == Using GraphicsMagick
91
+
92
+ Simply set
93
+
94
+ MiniMagick.processor = :gm
95
+
96
+ And you are sorted.
97
+
90
98
  == Requirements
91
99
 
92
100
  You must have ImageMagick or GraphicsMagick installed.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.2
1
+ 3.2.1
data/lib/mini_magick.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'tempfile'
2
2
  require 'subexec'
3
+ require 'stringio'
3
4
  require 'pathname'
4
5
 
5
6
  module MiniMagick
@@ -21,14 +22,14 @@ module MiniMagick
21
22
  end
22
23
  end
23
24
 
24
- MOGRIFY_COMMANDS = %w{adaptive-blur adaptive-resize adaptive-sharpen adjoin affine alpha annotate antialias append authenticate auto-gamma auto-level auto-orient background bench iterations bias black-threshold blue-primary point blue-shift factor blur border bordercolor brightness-contrast caption string cdl filename channel type charcoal radius chop clip clamp clip-mask filename clip-path id clone index clut contrast-stretch coalesce colorize color-matrix colors colorspace type combine comment string compose operator composite compress type contrast convolve coefficients crop cycle amount decipher filename debug events define format:option deconstruct delay delete index density depth despeckle direction type display server dispose method distort type coefficients dither method draw string edge radius emboss radius encipher filename encoding type endian type enhance equalize evaluate operator evaluate-sequence operator extent extract family name fft fill filter type flatten flip floodfill flop font name format string frame function name fuzz distance fx expression gamma gaussian-blur geometry gravity type green-primary point help identify ifft implode amount insert index intent type interlace type interline-spacing interpolate method interword-spacing kerning label string lat layers method level limit type linear-stretch liquid-rescale log format loop iterations mask filename mattecolor median radius modulate monitor monochrome morph morphology method kernel motion-blur negate noise radius normalize opaque ordered-dither NxN orient type page paint radius ping pointsize polaroid angle posterize levels precision preview type print string process image-filter profile filename quality quantizespace quiet radial-blur angle raise random-threshold low,high red-primary point regard-warnings region remap filename render repage resample resize respect-parentheses roll rotate degrees sample sampling-factor scale scene seed segments selective-blur separate sepia-tone threshold set attribute shade degrees shadow sharpen shave shear sigmoidal-contrast size sketch solarize threshold splice spread radius strip stroke strokewidth stretch type style type swap indexes swirl degrees texture filename threshold thumbnail tile filename tile-offset tint transform transparent transparent-color transpose transverse treedepth trim type type undercolor unique-colors units type unsharp verbose version view vignette virtual-pixel method wave weight type white-point point white-threshold write filename}
25
+ MOGRIFY_COMMANDS = %w{adaptive-blur adaptive-resize adaptive-sharpen adjoin affine alpha annotate antialias append authenticate auto-gamma auto-level auto-orient background bench iterations bias black-threshold blue-primary point blue-shift factor blur border bordercolor brightness-contrast caption string cdl filename channel type charcoal radius chop clip clamp clip-mask filename clip-path id clone index clut contrast-stretch coalesce colorize color-matrix colors colorspace type combine comment string compose operator composite compress type contrast convolve coefficients crop cycle amount decipher filename debug events define format:option deconstruct delay delete index density depth despeckle direction type display server dispose method distort type coefficients dither method draw string edge radius emboss radius encipher filename encoding type endian type enhance equalize evaluate operator evaluate-sequence operator extent extract family name fft fill filter type flatten flip floodfill flop font name format string frame function name fuzz distance fx expression gamma gaussian-blur geometry gravity type green-primary point help identify ifft implode amount insert index intent type interlace type interline-spacing interpolate method interword-spacing kerning label string lat layers method level limit type linear-stretch liquid-rescale log format loop iterations mask filename mattecolor median radius modulate monitor monochrome morph morphology method kernel motion-blur negate noise radius normalize opaque ordered-dither NxN orient type page paint radius ping pointsize polaroid angle posterize levels precision preview type print string process image-filter profile filename quality quantize quiet radial-blur angle raise random-threshold low,high red-primary point regard-warnings region remap filename render repage resample resize respect-parentheses roll rotate degrees sample sampling-factor scale scene seed segments selective-blur separate sepia-tone threshold set attribute shade degrees shadow sharpen shave shear sigmoidal-contrast size sketch solarize threshold splice spread radius strip stroke strokewidth stretch type style type swap indexes swirl degrees texture filename threshold thumbnail tile filename tile-offset tint transform transparent transparent-color transpose transverse treedepth trim type type undercolor unique-colors units type unsharp verbose version view vignette virtual-pixel method wave weight type white-point point white-threshold write filename}
25
26
 
26
27
  class Error < RuntimeError; end
27
28
  class Invalid < StandardError; end
28
29
 
29
30
  class Image
30
31
  # @return [String] The location of the current working file
31
- attr :path
32
+ attr_accessor :path
32
33
 
33
34
  # Class Methods
34
35
  # -------------
@@ -62,6 +63,34 @@ module MiniMagick
62
63
  create(ext) { |f| f.write(blob) }
63
64
  end
64
65
 
66
+ # Creates an image object from a binary string blob which contains raw pixel data (i.e. no header data).
67
+ #
68
+ # === Returns
69
+ #
70
+ # * [Image] The loaded image.
71
+ #
72
+ # === Parameters
73
+ #
74
+ # * [blob] <tt>String</tt> -- Binary string blob containing raw pixel data.
75
+ # * [columns] <tt>Integer</tt> -- Number of columns.
76
+ # * [rows] <tt>Integer</tt> -- Number of rows.
77
+ # * [depth] <tt>Integer</tt> -- Bit depth of the encoded pixel data.
78
+ # * [map] <tt>String</tt> -- A code for the mapping of the pixel data. Example: 'gray' or 'rgb'.
79
+ # * [format] <tt>String</tt> -- The file extension of the image format to be used when creating the image object. Defaults to 'png'.
80
+ #
81
+ def import_pixels(blob, columns, rows, depth, map, format="png")
82
+ # Create an image object with the raw pixel data string:
83
+ image = create(".dat", validate = false) { |f| f.write(blob) }
84
+ # Use ImageMagick to convert the raw data file to an image file of the desired format:
85
+ converted_image_path = image.path[0..-4] + format
86
+ argument = "-size #{columns}x#{rows} -depth #{depth} #{map}:#{image.path} #{converted_image_path}"
87
+ cmd = CommandBuilder.new("convert", argument) #Example: convert -size 256x256 -depth 16 gray:blob.dat blob.png
88
+ image.run(cmd)
89
+ # Update the image instance with the path of the properly formatted image, and return:
90
+ image.path = converted_image_path
91
+ image
92
+ end
93
+
65
94
  # Opens a specific image file either on the local file system or at a URI.
66
95
  #
67
96
  # Use this if you don't want to overwrite the image file.
@@ -73,12 +102,14 @@ module MiniMagick
73
102
  # @param file_or_url [String] Either a local file path or a URL that open-uri can read
74
103
  # @param ext [String] Specify the extension you want to read it as
75
104
  # @return [Image] The loaded image
76
- def open(file_or_url, ext = File.extname(file_or_url))
105
+ def open(file_or_url, ext = nil)
77
106
  file_or_url = file_or_url.to_s # Force it to be a String... hell or highwater
78
107
  if file_or_url.include?("://")
79
108
  require 'open-uri'
109
+ ext ||= File.extname(URI.parse(file_or_url).path)
80
110
  self.read(Kernel::open(file_or_url), ext)
81
111
  else
112
+ ext ||= File.extname(file_or_url)
82
113
  File.open(file_or_url, "rb") do |f|
83
114
  self.read(f, ext)
84
115
  end
@@ -97,9 +128,10 @@ module MiniMagick
97
128
  # by both #open and #read to create a new object! Ensures we have a good tempfile!
98
129
  #
99
130
  # @param ext [String] Specify the extension you want to read it as
131
+ # @param validate [Boolean] If false, skips validation of the created image. Defaults to true.
100
132
  # @yield [IOStream] You can #write bits to this object to create the new Image
101
133
  # @return [Image] The created image
102
- def create(ext = nil, &block)
134
+ def create(ext = nil, validate = true, &block)
103
135
  begin
104
136
  tempfile = Tempfile.new(['mini_magick', ext.to_s])
105
137
  tempfile.binmode
@@ -108,7 +140,7 @@ module MiniMagick
108
140
 
109
141
  image = self.new(tempfile.path, tempfile)
110
142
 
111
- if !image.valid?
143
+ if validate and !image.valid?
112
144
  raise MiniMagick::Invalid
113
145
  end
114
146
  return image
@@ -130,9 +162,9 @@ module MiniMagick
130
162
  @path = input_path
131
163
  @tempfile = tempfile # ensures that the tempfile will stick around until this image is garbage collected.
132
164
  end
133
-
165
+
134
166
  def escaped_path
135
- Pathname.new(@path).to_s.gsub(" ", "\\ ")
167
+ "\"#{Pathname.new(@path).to_s}\""
136
168
  end
137
169
 
138
170
  # Checks to make sure that MiniMagick can read the file and understand it.
@@ -409,7 +441,7 @@ module MiniMagick
409
441
  end
410
442
 
411
443
  def +(*options)
412
- push(@args.pop.gsub /^-/, '+')
444
+ push(@args.pop.gsub(/^-/, '+'))
413
445
  if options.any?
414
446
  options.each do |o|
415
447
  push "\"#{ o }\""
data/test/image_test.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'test/unit'
3
3
  require 'pathname'
4
- require 'stringio'
5
4
  require File.expand_path('../../lib/mini_magick', __FILE__)
6
5
 
7
6
  #MiniMagick.processor = :gm
@@ -13,7 +12,7 @@ class ImageTest < Test::Unit::TestCase
13
12
 
14
13
  SIMPLE_IMAGE_PATH = CURRENT_DIR + "simple.gif"
15
14
  MINUS_IMAGE_PATH = CURRENT_DIR + "simple-minus.gif"
16
- TIFF_IMAGE_PATH = CURRENT_DIR + "leaves spaced.tiff"
15
+ TIFF_IMAGE_PATH = CURRENT_DIR + "leaves (spaced).tiff"
17
16
  NOT_AN_IMAGE_PATH = CURRENT_DIR + "not_an_image.php"
18
17
  GIF_WITH_JPG_EXT = CURRENT_DIR + "actually_a_gif.jpg"
19
18
  EXIF_IMAGE_PATH = CURRENT_DIR + "trogdor.jpg"
@@ -36,6 +35,7 @@ class ImageTest < Test::Unit::TestCase
36
35
  def test_image_io_reading
37
36
  buffer = StringIO.new(File.read(SIMPLE_IMAGE_PATH))
38
37
  image = Image.read(buffer)
38
+ assert image.valid?
39
39
  image.destroy!
40
40
  end
41
41
 
@@ -53,7 +53,13 @@ class ImageTest < Test::Unit::TestCase
53
53
 
54
54
  def test_remote_image
55
55
  image = Image.open("http://www.google.com/images/logos/logo.png")
56
- image.valid?
56
+ assert image.valid?
57
+ image.destroy!
58
+ end
59
+
60
+ def test_remote_image_with_complex_url
61
+ image = Image.open("http://a0.twimg.com/a/1296609216/images/fronts/logo_withbird_home.png?extra=foo&plus=bar")
62
+ assert image.valid?
57
63
  image.destroy!
58
64
  end
59
65
 
@@ -253,6 +259,25 @@ class ImageTest < Test::Unit::TestCase
253
259
  FileUtils.rm("test.gif")
254
260
  end
255
261
 
262
+ # https://github.com/probablycorey/mini_magick/issues/37
263
+ def test_nonstandard_locale
264
+ original_lang = ENV["LANG"]
265
+ ENV["LANG"] = "fr_FR.UTF-8"
266
+
267
+ # This test should break
268
+ test_throw_on_openining_not_an_image
269
+ ensure
270
+ ENV["LANG"] = original_lang
271
+ end
272
+
273
+ def test_poop
274
+ img = MiniMagick::Image.open(SIMPLE_IMAGE_PATH)
275
+ img.gravity "Center"
276
+ img.crop "480x480"
277
+ img.resize "250x250"
278
+ img.write CURRENT_DIR + "output.png"
279
+ end
280
+
256
281
  def test_throw_format_error
257
282
  image = Image.open(SIMPLE_IMAGE_PATH)
258
283
  assert_raise MiniMagick::Error do
@@ -262,4 +287,36 @@ class ImageTest < Test::Unit::TestCase
262
287
  end
263
288
  image.destroy!
264
289
  end
290
+
291
+ def test_import_pixels_default_format
292
+ columns = 325
293
+ rows = 200
294
+ depth = 16 # 16 bits (2 bytes) per pixel
295
+ map = 'gray'
296
+ pixels = Array.new(columns*rows) {|i| i}
297
+ blob = pixels.pack("S*") # unsigned short, native byte order
298
+ image = Image.import_pixels(blob, columns, rows, depth, map)
299
+ assert image.valid?
300
+ assert_equal "png", image[:format].downcase
301
+ assert_equal columns, image[:width]
302
+ assert_equal rows, image[:height]
303
+ image.write(CURRENT_DIR + "imported_pixels_image.png")
304
+ end
305
+
306
+ def test_import_pixels_custom_format
307
+ columns = 325
308
+ rows = 200
309
+ depth = 16 # 16 bits (2 bytes) per pixel
310
+ map = 'gray'
311
+ format = 'jpeg'
312
+ pixels = Array.new(columns*rows) {|i| i}
313
+ blob = pixels.pack("S*") # unsigned short, native byte order
314
+ image = Image.import_pixels(blob, columns, rows, depth, map, format)
315
+ assert image.valid?
316
+ assert_equal format, image[:format].downcase
317
+ assert_equal columns, image[:width]
318
+ assert_equal rows, image[:height]
319
+ image.write(CURRENT_DIR + "imported_pixels_image." + format)
320
+ end
321
+
265
322
  end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_magick
3
3
  version: !ruby/object:Gem::Version
4
- hash: 3
5
4
  prerelease: false
6
5
  segments:
7
6
  - 3
8
7
  - 2
9
- version: "3.2"
8
+ - 1
9
+ version: 3.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Corey Johnson
@@ -16,18 +16,16 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-01-11 00:00:00 +00:00
19
+ date: 2011-04-28 00:00:00 +01:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
23
23
  name: subexec
24
24
  prerelease: false
25
25
  requirement: &id001 !ruby/object:Gem::Requirement
26
- none: false
27
26
  requirements:
28
27
  - - ~>
29
28
  - !ruby/object:Gem::Version
30
- hash: 23
31
29
  segments:
32
30
  - 0
33
31
  - 0
@@ -53,16 +51,6 @@ files:
53
51
  - Rakefile
54
52
  - lib/mini_gmagick.rb
55
53
  - lib/mini_magick.rb
56
- - test/actually_a_gif.jpg
57
- - test/animation.gif
58
- - test/command_builder_test.rb
59
- - test/composited.jpg
60
- - test/image_test.rb
61
- - test/leaves spaced.tiff
62
- - test/not_an_image.php
63
- - test/simple-minus.gif
64
- - test/simple.gif
65
- - test/trogdor.jpg
66
54
  has_rdoc: true
67
55
  homepage: http://github.com/probablycorey/mini_magick
68
56
  licenses: []
@@ -73,27 +61,23 @@ rdoc_options: []
73
61
  require_paths:
74
62
  - lib
75
63
  required_ruby_version: !ruby/object:Gem::Requirement
76
- none: false
77
64
  requirements:
78
65
  - - ">="
79
66
  - !ruby/object:Gem::Version
80
- hash: 3
81
67
  segments:
82
68
  - 0
83
69
  version: "0"
84
70
  required_rubygems_version: !ruby/object:Gem::Requirement
85
- none: false
86
71
  requirements:
87
72
  - - ">="
88
73
  - !ruby/object:Gem::Version
89
- hash: 3
90
74
  segments:
91
75
  - 0
92
76
  version: "0"
93
77
  requirements: []
94
78
 
95
79
  rubyforge_project:
96
- rubygems_version: 1.3.7
80
+ rubygems_version: 1.3.6
97
81
  signing_key:
98
82
  specification_version: 3
99
83
  summary: Manipulate images with minimal use of memory via ImageMagick / GraphicsMagick
@@ -103,7 +87,7 @@ test_files:
103
87
  - test/command_builder_test.rb
104
88
  - test/composited.jpg
105
89
  - test/image_test.rb
106
- - test/leaves spaced.tiff
90
+ - test/leaves (spaced).tiff
107
91
  - test/not_an_image.php
108
92
  - test/simple-minus.gif
109
93
  - test/simple.gif