mini_magick 1.0.1 → 1.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.

Potentially problematic release.


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

data/README CHANGED
@@ -48,16 +48,24 @@ Want to manipulate an image at its source (You won't have to write it out becaus
48
48
  image = MiniMagick::Image.new("input.jpg")
49
49
  image.resize "100x100"
50
50
 
51
+ Want to get some meta-information out?
52
+
53
+ image = MiniMagick::Image.from_file("input.jpg")
54
+ image[:width] # will get the width (you can also use :height and :format)
55
+ image["EXIF:BitsPerSample"] # It also can get all the EXIF tags
56
+ image["%m:%f %wx%h"] # Or you can use one of the many options of the format command found here http://www.imagemagick.org/script/command-line-options.php#format
57
+
51
58
  Requirements
52
59
  ------------
53
60
  You must have ImageMagick installed.
54
61
 
55
-
56
62
  How To Install
57
63
  --------------
58
- (I've only tested this on OS X and Linux)
64
+ If you downloaded the plugin version, just drop the plugin into RAILS_ROOT/plugins/
65
+
66
+ If you installed this as a gem, then to get it to work add <require "mini_magick"> to RAILS_ROOT/config/environment.rb
59
67
 
60
- I've packaged up MiniMagick as a rails plugin. Just unzip the file to the /vendor/plugins directory of your rails folder and you have access to all the magick.
68
+ If you have just downloaded this files then copy the mini_magick.rb file into your RAILS_ROOT/lib directory and add <require "mini-magick"> to RAILS_ROOT/config/environment.rb
61
69
 
62
70
  MiniMagick does NOT require rails though. All the code you need to use MiniMagick is located in the mini_magick/lib/mini_magick.rb file.
63
71
 
data/lib/mini_magick.rb CHANGED
@@ -3,35 +3,20 @@ require "tempfile"
3
3
  require "stringio"
4
4
 
5
5
  module MiniMagick
6
+ class MiniMagickError < Exception; end
6
7
 
7
- VERSION = '1.0.1'
8
-
9
- class MiniMagickError < Exception
10
- end
8
+ VERSION = '1.1.0'
11
9
 
12
10
  class Image
13
11
  attr :path
14
12
 
15
- # Attributes
16
- # ----------
17
- def width
18
- info[:width]
19
- end
20
-
21
- def height
22
- info[:height]
23
- end
24
-
25
- def format
26
- info[:format]
27
- end
28
-
29
13
  # Class Methods
30
14
  # -------------
31
15
  class <<self
32
16
  def from_blob(blob)
33
17
  begin
34
18
  tmp = Tempfile.new("minimagic")
19
+ tmp.binmode
35
20
  tmp.write(blob)
36
21
  ensure
37
22
  tmp.close
@@ -42,7 +27,7 @@ module MiniMagick
42
27
 
43
28
  # Use this if you don't want to overwrite the image file
44
29
  def from_file(image_path)
45
- File.open(image_path, "r") do |f|
30
+ File.open(image_path, "rb") do |f|
46
31
  self.from_blob(f.read)
47
32
  end
48
33
  end
@@ -50,24 +35,49 @@ module MiniMagick
50
35
 
51
36
  # Instance Methods
52
37
  # ----------------
53
- def initialize(input_path)
54
-
38
+ def initialize(input_path)
55
39
  @path = input_path
56
40
 
57
41
  # Ensure that the file is an image
58
42
  run_command("identify #{@path}")
59
43
  end
44
+
45
+ def [](value)
46
+ # Why do I go to the trouble of putting in newline chars? Because otherwise animated gifs screw everything up
47
+ case value.to_s
48
+ when "format"
49
+ run_command("identify", "-format", "%m\\\\n", @path).split("\n")[0]
50
+ when "height"
51
+ run_command("identify", "-format", "%h\\\\n", @path).split("\n")[0].to_i
52
+ when "width"
53
+ run_command("identify", "-format", "%w\\\\n", @path).split("\n")[0].to_i
54
+ when "original_at"
55
+ # Get the EXIF original capture as a Time object
56
+ Time.local(*self["EXIF:DateTimeOriginal"].split(/:|\s+/)) rescue nil
57
+ when /^EXIF\:/i
58
+ run_command('identify', '-format', "\"%[#{value}]\"", @path).chop
59
+ else
60
+ run_command('identify', '-format', "\"#{value}\"", @path)
61
+ end
62
+ end
63
+
64
+ # This is a 'special' command because it needs to change @path to reflect the new extension
65
+ def format(format)
66
+ run_command("mogrify", "-format", format, @path)
67
+ @path = @path.sub(/\.\w+$/, ".#{format}")
68
+
69
+ raise "Unable to format to #{format}" unless File.exists?(@path)
70
+ end
60
71
 
61
- def write(output_path)
62
- open(output_path, "w") do |output_file|
63
- open(@path) do |image_file|
72
+ def write(output_path)
73
+ open(output_path, "wb") do |output_file|
74
+ open(@path, "rb") do |image_file|
64
75
  output_file.write(image_file.read)
65
76
  end
66
- end
77
+ end
67
78
  end
68
79
 
69
- # Any message that sent that is unknown is sent through morgrify
70
- #
80
+ # If an unknown method is called then it is sent through the morgrify program
71
81
  # Look here to find all the commands (http://www.imagemagick.org/script/mogrify.php)
72
82
  def method_missing(symbol, *args)
73
83
  args.push(@path) # push the path onto the end
@@ -84,19 +94,11 @@ module MiniMagick
84
94
  # Private (Don't look in here!)
85
95
  # -----------------------------
86
96
  private
87
-
88
- def info
89
- info_array = run_command("identify", @path).split
90
- info = {
91
- :format => info_array[1],
92
- :width => info_array[2].match(/^\d+/)[0].to_i,
93
- :height => info_array[2].match(/\d+$/)[0].to_i
94
- }
95
- end
96
-
97
+
97
98
  def run_command(command, *args)
98
99
  args = args.collect {|a| a.to_s}
99
100
  output = `#{command} #{args.join(' ')}`
101
+
100
102
  if $? != 0
101
103
  raise MiniMagickError, "ImageMagick command (#{command} #{args.join(' ')}) failed: Error Given #{$?}"
102
104
  else
Binary file
@@ -1,12 +1,5 @@
1
- begin
2
- # Rails context
3
- require File.dirname(__FILE__) + '/../../../../test/test_helper'
4
- rescue LoadError => e
5
- # Normal Ruby context
6
- $:.unshift(File.dirname(__FILE__) + "/../lib/")
7
- require 'test/unit'
8
- require 'mini_magick'
9
- end
1
+ require 'test/unit'
2
+ require File.join(File.dirname(__FILE__), '../lib/mini_magick')
10
3
 
11
4
  class ImageTest < Test::Unit::TestCase
12
5
  include MiniMagick
@@ -15,6 +8,8 @@ class ImageTest < Test::Unit::TestCase
15
8
 
16
9
  SIMPLE_IMAGE_PATH = CURRENT_DIR + "simple.gif"
17
10
  NOT_AN_IMAGE_PATH = CURRENT_DIR + "not_an_image.php"
11
+ GIF_WITH_JPG_EXT = CURRENT_DIR + "actually_a_gif.jpg"
12
+ EXIF_IMAGE_PATH = CURRENT_DIR + "trogdor.jpg"
18
13
 
19
14
  def test_image_from_blob
20
15
  File.open(SIMPLE_IMAGE_PATH, "r") do |f|
@@ -47,21 +42,26 @@ class ImageTest < Test::Unit::TestCase
47
42
  image = Image.new(NOT_AN_IMAGE_PATH)
48
43
  end
49
44
  end
50
-
51
- def test_image_info
45
+
46
+ def test_image_meta_info
52
47
  image = Image.new(SIMPLE_IMAGE_PATH)
53
- assert_equal 150, image.width
54
- assert_equal 55, image.height
55
- assert_match(/^gif$/i, image.format)
48
+ assert_equal 150, image[:width]
49
+ assert_equal 55, image[:height]
50
+ assert_match(/^gif$/i, image[:format])
51
+ end
52
+
53
+ def test_gif_with_jpg_format
54
+ image = Image.new(GIF_WITH_JPG_EXT)
55
+ assert_equal "gif", image[:format].downcase
56
56
  end
57
57
 
58
58
  def test_image_resize
59
59
  image = Image.from_file(SIMPLE_IMAGE_PATH)
60
60
  image.resize "20x30!"
61
61
 
62
- assert_equal 20, image.width
63
- assert_equal 30, image.height
64
- assert_match(/^gif$/i, image.format)
62
+ assert_equal 20, image[:width]
63
+ assert_equal 30, image[:height]
64
+ assert_match(/^gif$/i, image[:format])
65
65
  end
66
66
 
67
67
  def test_image_combine_options_resize_blur
@@ -71,10 +71,24 @@ class ImageTest < Test::Unit::TestCase
71
71
  c.blur 50
72
72
  end
73
73
 
74
- assert_equal 20, image.width
75
- assert_equal 30, image.height
76
- assert_match(/^gif$/i, image.format)
77
- end
74
+ assert_equal 20, image[:width]
75
+ assert_equal 30, image[:height]
76
+ assert_match(/^gif$/i, image[:format])
77
+ end
78
+
79
+ def test_exif
80
+ image = Image.from_file(EXIF_IMAGE_PATH)
81
+ assert_equal('0220', image["exif:ExifVersion"])
82
+ image = Image.from_file(SIMPLE_IMAGE_PATH)
83
+ assert_equal('', image["EXIF:ExifVersion"])
84
+ end
85
+
86
+ def test_original_at
87
+ image = Image.from_file(EXIF_IMAGE_PATH)
88
+ assert_equal(Time.local('2005', '2', '23', '23', '17', '24'), image[:original_at])
89
+ image = Image.from_file(SIMPLE_IMAGE_PATH)
90
+ assert_nil(image[:original_at])
91
+ end
78
92
  end
79
93
 
80
94
  class CommandBuilderTest < Test::Unit::TestCase
data/test/trogdor.jpg ADDED
Binary file
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: mini_magick
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.0.1
7
- date: 2006-02-25 00:00:00 -05:00
6
+ version: 1.1.0
7
+ date: 2006-04-16 00:00:00 -04:00
8
8
  summary: Manipulate images with minimal use of memory.
9
9
  require_paths:
10
10
  - lib
@@ -32,9 +32,11 @@ files:
32
32
  - README
33
33
  - MIT-LICENSE
34
34
  - lib/mini_magick.rb
35
+ - test/actually_a_gif.jpg
35
36
  - test/mini_magick_test.rb
36
37
  - test/not_an_image.php
37
38
  - test/simple.gif
39
+ - test/trogdor.jpg
38
40
  test_files: []
39
41
 
40
42
  rdoc_options: []