mini_magick 1.2.0 → 1.2.3

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.
data/History.txt ADDED
@@ -0,0 +1,11 @@
1
+ == 1.2.2 / 2007-06-01
2
+
3
+ # 1.) all image commands return the image object (The output of the last command is saved in @output)
4
+ # 2.) identify doesn't trip over strangley named files
5
+ # 3.) TempFile uses file extention now (Thanks http://marsorange.com/archives/of-mogrify-ruby-tempfile-dynamic-class-definitions)
6
+ # 4.) identify commands escape output path correctly
7
+
8
+ == 1.2.3 / 2007-06-15
9
+
10
+ # 1.) Image::from_file doesn't drop the file extension anymore, it and use the image_temp_file correctly
11
+ # 4.) TempFiles are stored as an instance variable in Image instances so they don't get cleaned up prematurely via garbage collection
data/Manifest.txt ADDED
@@ -0,0 +1,14 @@
1
+ MIT-LICENSE
2
+ History.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ init.rb
7
+ lib/image_temp_file.rb
8
+ lib/mini_magick.rb
9
+ test/actually_a_gif.jpg
10
+ test/not_an_image.php
11
+ test/simple.gif
12
+ test/test_image_temp_file.rb
13
+ test/test_mini_magick_test.rb
14
+ test/trogdor.jpg
@@ -1,10 +1,13 @@
1
- MiniMagick
2
- ==========
1
+ mini_magick
2
+ by Coery Johnson
3
+ FIX (url)
3
4
 
5
+ == DESCRIPTION:
6
+
4
7
  A ruby wrapper for ImageMagick command line.
5
8
 
6
- Why?
7
- ---
9
+ - Why?
10
+
8
11
  I was using RMagick and loving it, but it was eating up huge amounts of memory. A simple script like this...
9
12
 
10
13
  Magick::read("image.jpg") do |f|
@@ -13,14 +16,16 @@ end
13
16
 
14
17
  ...would use over 100 Megs of Ram. On my local machine this wasn't a problem, but on my hosting server the ruby apps would crash because of their 100 Meg memory limit.
15
18
 
16
- Solution!
17
- ---------
19
+ - Solution!
20
+
18
21
  Using MiniMagick the ruby processes memory remains small (it spawns ImageMagick's command line program mogrify which takes up some memory as well, but is much smaller compared to RMagick)
19
22
 
23
+
24
+ == FEATURES/PROBLEMS:
25
+
20
26
  MiniMagick gives you access to all the commandline options ImageMagick has (Found here http://www.imagemagick.org/script/mogrify.php)
21
27
 
22
- Examples
23
- --------
28
+ == SYNOPSIS:
24
29
 
25
30
  Want to make a thumbnail from a file...
26
31
 
@@ -55,12 +60,13 @@ image[:width] # will get the width (you can also use :height and :format)
55
60
  image["EXIF:BitsPerSample"] # It also can get all the EXIF tags
56
61
  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
62
 
58
- Requirements
59
- ------------
63
+
64
+ == REQUIREMENTS:
65
+
60
66
  You must have ImageMagick installed.
61
67
 
62
- How To Install
63
- --------------
68
+ == INSTALL:
69
+
64
70
  If you downloaded the plugin version, just drop the plugin into RAILS_ROOT/plugins/
65
71
 
66
72
  If you installed this as a gem, then to get it to work add <require "mini_magick"> to RAILS_ROOT/config/environment.rb
@@ -69,4 +75,27 @@ If you have just downloaded this files then copy the mini_magick.rb file into yo
69
75
 
70
76
  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.
71
77
 
78
+ == LICENSE:
79
+
80
+ (The MIT License)
81
+
82
+ Copyright (c) 2007 FIX
83
+
84
+ Permission is hereby granted, free of charge, to any person obtaining
85
+ a copy of this software and associated documentation files (the
86
+ 'Software'), to deal in the Software without restriction, including
87
+ without limitation the rights to use, copy, modify, merge, publish,
88
+ distribute, sublicense, and/or sell copies of the Software, and to
89
+ permit persons to whom the Software is furnished to do so, subject to
90
+ the following conditions:
91
+
92
+ The above copyright notice and this permission notice shall be
93
+ included in all copies or substantial portions of the Software.
72
94
 
95
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
96
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
97
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
98
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
99
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
100
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
101
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile CHANGED
@@ -1,68 +1,17 @@
1
- require 'rake'
2
- require 'rake/testtask'
3
- require 'rake/rdoctask'
4
- require 'rake/packagetask'
5
- require 'rake/gempackagetask'
6
-
7
- $:.unshift(File.dirname(__FILE__) + "/lib")
8
- require 'mini_magick'
9
-
10
- PKG_NAME = 'mini_magick'
11
- PKG_VERSION = MiniMagick::VERSION
12
- PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
13
-
14
- desc 'Default: run unit tests.'
15
- task :default => :test
16
-
17
- desc "Clean generated files"
18
- task :clean do
19
- rm FileList['test/output/*.png']
20
- rm_rf 'pkg'
21
- rm_rf 'doc'
22
- end
23
-
24
- desc 'Test the mini_magick plugin.'
25
- Rake::TestTask.new(:test) do |t|
26
- t.libs << 'lib'
27
- t.pattern = 'test/**/*_test.rb'
28
- t.verbose = true
29
- end
30
-
31
- desc 'Generate documentation for the mini_magick plugin.'
32
- Rake::RDocTask.new(:rdoc) do |rdoc|
33
- rdoc.rdoc_dir = 'rdoc'
34
- rdoc.title = 'MiniMagick'
35
- rdoc.options << '--line-numbers --inline-source'
36
- rdoc.rdoc_files.include('README')
37
- rdoc.rdoc_files.include('lib/**/*.rb')
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/mini_magick.rb'
6
+
7
+ Hoe.new('mini_magick', MiniMagick::VERSION) do |p|
8
+ p.rubyforge_name = 'mini_magick'
9
+ p.author = 'Corey Johnson'
10
+ p.email = 'probablycorey+ruby@gmail.com'
11
+ p.summary = 'A simple image manipulation library based on ImageMagick.'
12
+ p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
13
+ #p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
14
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
38
15
  end
39
16
 
40
-
41
- # Create compressed packages
42
- spec = Gem::Specification.new do |s|
43
- s.platform = Gem::Platform::RUBY
44
- s.name = PKG_NAME
45
- s.summary = "Manipulate images with minimal use of memory."
46
- s.description = %q{Uses command-line ImageMagick tools to resize, rotate, and mogrify images.}
47
- s.version = PKG_VERSION
48
-
49
- s.author = "Corey Johnson"
50
- s.email = "probablycorey@gmail.com"
51
- s.rubyforge_project = PKG_NAME
52
- s.homepage = "http://gleepglop.com"
53
-
54
- s.has_rdoc = true
55
- s.requirements << 'none'
56
- s.require_path = 'lib'
57
- s.autorequire = 'mini_magick'
58
-
59
- s.files = [ "Rakefile", "README", "MIT-LICENSE" ]
60
- s.files = s.files + Dir.glob( "lib/**/*" ).delete_if { |item| item.include?( "\.svn" ) }
61
- s.files = s.files + Dir.glob( "test/**/*" ).delete_if { |item| item.include?( "\.svn" ) || item.include?("\.png") }
62
- end
63
-
64
- Rake::GemPackageTask.new(spec) do |p|
65
- p.gem_spec = spec
66
- p.need_tar = false
67
- p.need_zip = true
68
- end
17
+ # vim: syntax=Ruby
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'mini_magick'
2
+ require 'image_temp_file'
@@ -0,0 +1,13 @@
1
+ require "tempfile"
2
+
3
+ module MiniMagick
4
+ class ImageTempFile < Tempfile
5
+ def make_tmpname(basename, n)
6
+ # force tempfile to use basename's extension if provided
7
+ ext = File.extname(basename)
8
+
9
+ # force hyphens instead of periods in name
10
+ sprintf('%s%d-%d%s', File.basename(basename, ext), $$, n, ext)
11
+ end
12
+ end
13
+ end
data/lib/mini_magick.rb CHANGED
@@ -1,52 +1,58 @@
1
1
  require "open-uri"
2
- require "tempfile"
3
2
  require "stringio"
4
3
  require "fileutils"
5
4
 
5
+ require File.join(File.dirname(__FILE__), '/image_temp_file')
6
+
6
7
  module MiniMagick
7
- class MiniMagickError < Exception; end
8
-
9
- VERSION = '1.2.0'
10
-
8
+ class MiniMagickError < RuntimeError; end
9
+
10
+ VERSION = '1.2.3'
11
+
11
12
  class Image
12
13
  attr :path
14
+ attr :tempfile
15
+ attr :output
13
16
 
14
17
  # Class Methods
15
18
  # -------------
16
19
  class <<self
17
- def from_blob(blob)
20
+ def from_blob(blob, extension=nil)
18
21
  begin
19
- tmp = Tempfile.new("minimagic")
20
- tmp.binmode
21
- tmp.write(blob)
22
+ tempfile = ImageTempFile.new("minimagick#{extension}")
23
+ tempfile.binmode
24
+ tempfile.write(blob)
22
25
  ensure
23
- tmp.close
24
- end
25
- return self.new(tmp.path)
26
+ tempfile.close
27
+ end
28
+
29
+ return self.new(tempfile.path, tempfile)
26
30
  end
27
-
31
+
28
32
  # Use this if you don't want to overwrite the image file
29
- def from_file(image_path)
33
+ def from_file(image_path)
30
34
  File.open(image_path, "rb") do |f|
31
- self.from_blob(f.read)
35
+ self.from_blob(f.read, File.extname(image_path))
32
36
  end
33
37
  end
34
38
  end
35
-
39
+
36
40
  # Instance Methods
37
- # ----------------
38
- def initialize(input_path)
41
+ # ----------------
42
+ def initialize(input_path, tempfile=nil)
39
43
  @path = input_path
40
-
44
+ @tempfile = tempfile # ensures that the tempfile will stick around until this image is garbage collected.
45
+
41
46
  # Ensure that the file is an image
42
- run_command("identify #{@path}")
47
+ run_command("identify", @path)
43
48
  end
44
-
49
+
50
+ # For reference see http://www.imagemagick.org/script/command-line-options.php#format
45
51
  def [](value)
46
- # Why do I go to the trouble of putting in newline chars? Because otherwise animated gifs screw everything up
52
+ # Why do I go to the trouble of putting in newlines? Because otherwise animated gifs screw everything up
47
53
  case value.to_s
48
54
  when "format"
49
- run_command("identify", "-format", format_option("%m"), @path).split("\n")[0]
55
+ run_command("identify", "-format", format_option("%m"), @path).split("\n")[0]
50
56
  when "height"
51
57
  run_command("identify", "-format", format_option("%h"), @path).split("\n")[0].to_i
52
58
  when "width"
@@ -55,87 +61,87 @@ module MiniMagick
55
61
  # Get the EXIF original capture as a Time object
56
62
  Time.local(*self["EXIF:DateTimeOriginal"].split(/:|\s+/)) rescue nil
57
63
  when /^EXIF\:/i
58
- run_command('identify', '-format', "\"%[#{value}]\"", @path).chop
64
+ run_command('identify', '-format', "\"%[#{value}]\"", @path).chop
59
65
  else
60
- run_command('identify', '-format', "\"#{value}\"", @path)
66
+ run_command('identify', '-format', "\"#{value}\"", @path).split("\n")[0]
61
67
  end
62
68
  end
63
-
69
+
64
70
  # This is a 'special' command because it needs to change @path to reflect the new extension
65
71
  def format(format)
66
72
  run_command("mogrify", "-format", format, @path)
67
- @path = @path.sub(/\.\w+$/, ".#{format}")
73
+ @path = @path.sub(/(\.\w+)?$/, ".#{format}")
68
74
 
69
75
  raise "Unable to format to #{format}" unless File.exists?(@path)
70
76
  end
71
-
77
+
72
78
  # Writes the temporary image that we are using for processing to the output path
73
- def write(output_path)
79
+ def write(output_path)
74
80
  FileUtils.copy_file @path, output_path
75
- run_command "identify #{output_path}" # Verify that we have a good image
81
+ run_command "identify", output_path # Verify that we have a good image
76
82
  end
77
-
83
+
84
+ # Give you raw data back
85
+ def to_blob
86
+ File.read @path
87
+ end
88
+
78
89
  # If an unknown method is called then it is sent through the morgrify program
79
90
  # Look here to find all the commands (http://www.imagemagick.org/script/mogrify.php)
80
91
  def method_missing(symbol, *args)
81
92
  args.push(@path) # push the path onto the end
82
93
  run_command("mogrify", "-#{symbol}", *args)
94
+ self
83
95
  end
84
-
96
+
85
97
  # You can use multiple commands together using this method
86
- def combine_options(&block)
98
+ def combine_options(&block)
87
99
  c = CommandBuilder.new
88
100
  block.call c
89
101
  run_command("mogrify", *c.args << @path)
90
102
  end
91
-
92
- # Private (Don't look in here!)
93
- # -----------------------------
94
- private
95
-
96
- # Check to see if we are running on win32 -- we need to escape things differently
103
+
104
+ # Check to see if we are running on win32 -- we need to escape things differently
97
105
  def windows?
98
- !(RUBY_PLATFORM =~ /win32/).nil?
99
- end
100
-
101
- # Outputs a carriage-return delimited format string for Unix and Windows
106
+ !(RUBY_PLATFORM =~ /win32/).nil?
107
+ end
108
+
109
+ # Outputs a carriage-return delimited format string for Unix and Windows
102
110
  def format_option(format)
103
- if windows?
104
- format = "#{format}\\n"
105
- else
106
- format = "#{format}\\\\n"
107
- end
108
- end
109
-
111
+ windows? ? "#{format}\\n" : "#{format}\\\\n"
112
+ end
113
+
110
114
  def run_command(command, *args)
111
- args = args.collect do |a|
112
- a = a.to_s
113
- unless a[0,1] == '-' # don't quote switches
114
- "\"#{a}\"" # values quoted because they can contain characters like '>'
115
- else
116
- a
117
- end
115
+ args.collect! do |arg|
116
+ arg = arg.to_s
117
+ arg = %|"#{arg}"| unless arg[0] == ?- # values quoted because they can contain characters like '>', but don't quote switches
118
+ arg
118
119
  end
119
-
120
- output = `#{command} #{args.join(' ')}`
121
-
120
+
121
+ @output = `#{command} #{args.join(' ')}`
122
+
122
123
  if $? != 0
123
124
  raise MiniMagickError, "ImageMagick command (#{command} #{args.join(' ')}) failed: Error Given #{$?}"
124
125
  else
125
- return output
126
- end
126
+ @output
127
+ end
127
128
  end
128
129
  end
129
130
 
130
131
  class CommandBuilder
131
132
  attr :args
132
-
133
+
133
134
  def initialize
134
135
  @args = []
135
136
  end
136
-
137
+
137
138
  def method_missing(symbol, *args)
138
- @args += ["-#{symbol}"] + args
139
- end
139
+ @args << "-#{symbol}"
140
+ @args += args
141
+ end
142
+
143
+ def +(value)
144
+ @args << "+#{value}"
145
+ end
140
146
  end
141
147
  end