remvee-mini_magick 1.2.3.1
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/MIT-LICENSE +21 -0
- data/README.rdoc +90 -0
- data/Rakefile +29 -0
- data/lib/image_temp_file.rb +13 -0
- data/lib/mini_magick.rb +178 -0
- metadata +57 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2005 Corey Johnson probablycorey@gmail.com
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
data/README.rdoc
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
= MiniMagick
|
2
|
+
|
3
|
+
A ruby wrapper for ImageMagick command line.
|
4
|
+
|
5
|
+
|
6
|
+
== Why?
|
7
|
+
|
8
|
+
I was using RMagick and loving it, but it was eating up huge amounts
|
9
|
+
of memory. A simple script like this...
|
10
|
+
|
11
|
+
Magick::read("image.jpg") do |f|
|
12
|
+
f.write("manipulated.jpg")
|
13
|
+
end
|
14
|
+
|
15
|
+
...would use over 100 Megs of Ram. On my local machine this wasn't a
|
16
|
+
problem, but on my hosting server the ruby apps would crash because of
|
17
|
+
their 100 Meg memory limit.
|
18
|
+
|
19
|
+
|
20
|
+
== Solution!
|
21
|
+
|
22
|
+
Using MiniMagick the ruby processes memory remains small (it spawns
|
23
|
+
ImageMagick's command line program mogrify which takes up some memory
|
24
|
+
as well, but is much smaller compared to RMagick)
|
25
|
+
|
26
|
+
MiniMagick gives you access to all the commandline options ImageMagick
|
27
|
+
has (Found here http://www.imagemagick.org/script/mogrify.php)
|
28
|
+
|
29
|
+
|
30
|
+
== Examples
|
31
|
+
|
32
|
+
Want to make a thumbnail from a file...
|
33
|
+
|
34
|
+
image = MiniMagick::Image.from_file("input.jpg")
|
35
|
+
image.resize "100x100"
|
36
|
+
image.write("output.jpg")
|
37
|
+
|
38
|
+
Want to make a thumbnail from a blob...
|
39
|
+
|
40
|
+
image = MiniMagick::Image.from_blob(blob)
|
41
|
+
image.resize "100x100"
|
42
|
+
image.write("output.jpg")
|
43
|
+
|
44
|
+
Need to combine several options?
|
45
|
+
|
46
|
+
image = MiniMagick::Image.from_file("input.jpg")
|
47
|
+
image.combine_options do |c|
|
48
|
+
c.sample "50%"
|
49
|
+
c.rotate "-90>"
|
50
|
+
end
|
51
|
+
image.write("output.jpg")
|
52
|
+
|
53
|
+
Want to manipulate an image at its source (You won't have to write it
|
54
|
+
out because the transformations are done on that file)
|
55
|
+
|
56
|
+
image = MiniMagick::Image.new("input.jpg")
|
57
|
+
image.resize "100x100"
|
58
|
+
|
59
|
+
Want to get some meta-information out?
|
60
|
+
|
61
|
+
image = MiniMagick::Image.from_file("input.jpg")
|
62
|
+
image[:width] # will get the width (you can also use :height and :format)
|
63
|
+
image["EXIF:BitsPerSample"] # It also can get all the EXIF tags
|
64
|
+
image["%m:%f %wx%h"] # Or you can use one of the many options of the format command
|
65
|
+
|
66
|
+
For more on the format command see
|
67
|
+
http://www.imagemagick.org/script/command-line-options.php#format
|
68
|
+
|
69
|
+
|
70
|
+
== Requirements
|
71
|
+
|
72
|
+
You must have ImageMagick installed.
|
73
|
+
|
74
|
+
|
75
|
+
== How To Install
|
76
|
+
|
77
|
+
If you downloaded the plugin version, just drop the plugin into
|
78
|
+
RAILS_ROOT/plugins/
|
79
|
+
|
80
|
+
If you installed this as a gem, then to get it to work add <require
|
81
|
+
"mini_magick"> to RAILS_ROOT/config/environment.rb
|
82
|
+
|
83
|
+
If you have just downloaded this files then copy the mini_magick.rb
|
84
|
+
file into your RAILS_ROOT/lib directory and add <require
|
85
|
+
"mini-magick"> to RAILS_ROOT/config/environment.rb
|
86
|
+
|
87
|
+
MiniMagick does NOT require rails though. All the code you need to use
|
88
|
+
MiniMagick is located in the mini_magick/lib/mini_magick.rb file.
|
89
|
+
|
90
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'rake/packagetask'
|
5
|
+
|
6
|
+
desc 'Default: run unit tests.'
|
7
|
+
task :default => :test
|
8
|
+
|
9
|
+
desc "Clean generated files"
|
10
|
+
task :clean => :clobber_rdoc do
|
11
|
+
rm FileList['test/output/*.png']
|
12
|
+
end
|
13
|
+
|
14
|
+
desc 'Test the mini_magick plugin.'
|
15
|
+
Rake::TestTask.new(:test) do |t|
|
16
|
+
t.libs << 'lib'
|
17
|
+
t.pattern = 'test/**/*_test.rb'
|
18
|
+
t.verbose = true
|
19
|
+
end
|
20
|
+
|
21
|
+
desc 'Generate documentation for the mini_magick plugin.'
|
22
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
23
|
+
rdoc.rdoc_dir = 'rdoc'
|
24
|
+
rdoc.title = 'MiniMagick'
|
25
|
+
rdoc.options << '--line-numbers'
|
26
|
+
rdoc.options << '--inline-source'
|
27
|
+
rdoc.rdoc_files.include('README.rdoc')
|
28
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
29
|
+
end
|
@@ -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
ADDED
@@ -0,0 +1,178 @@
|
|
1
|
+
require "open-uri"
|
2
|
+
require "stringio"
|
3
|
+
require "fileutils"
|
4
|
+
|
5
|
+
require File.join(File.dirname(__FILE__), '/image_temp_file')
|
6
|
+
|
7
|
+
module MiniMagick
|
8
|
+
class MiniMagickError < RuntimeError; end
|
9
|
+
|
10
|
+
VERSION = '1.2.3.1'
|
11
|
+
|
12
|
+
class Image
|
13
|
+
attr :path
|
14
|
+
attr :tempfile
|
15
|
+
attr :output
|
16
|
+
|
17
|
+
# Class Methods
|
18
|
+
# -------------
|
19
|
+
class << self
|
20
|
+
def from_blob(blob, extension=nil)
|
21
|
+
begin
|
22
|
+
tempfile = ImageTempFile.new("minimagick.#{extension}")
|
23
|
+
tempfile.binmode
|
24
|
+
tempfile.write(blob)
|
25
|
+
ensure
|
26
|
+
tempfile.close
|
27
|
+
end
|
28
|
+
|
29
|
+
return self.new(tempfile.path, tempfile)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Use this if you don't want to overwrite the image file
|
33
|
+
def open(image_path)
|
34
|
+
File.open(image_path, "rb") do |f|
|
35
|
+
self.from_blob(f.read, File.extname(image_path))
|
36
|
+
end
|
37
|
+
end
|
38
|
+
alias_method :from_file, :open
|
39
|
+
end
|
40
|
+
|
41
|
+
# Instance Methods
|
42
|
+
# ----------------
|
43
|
+
def initialize(input_path, tempfile=nil)
|
44
|
+
@path = input_path
|
45
|
+
@tempfile = tempfile # ensures that the tempfile will stick around until this image is garbage collected.
|
46
|
+
|
47
|
+
# Ensure that the file is an image
|
48
|
+
run_command("identify", @path)
|
49
|
+
end
|
50
|
+
|
51
|
+
# For reference see http://www.imagemagick.org/script/command-line-options.php#format
|
52
|
+
def [](value)
|
53
|
+
# Why do I go to the trouble of putting in newlines? Because otherwise animated gifs screw everything up
|
54
|
+
case value.to_s
|
55
|
+
when "format"
|
56
|
+
run_command("identify", "-format", format_option("%m"), @path).split("\n")[0]
|
57
|
+
when "height"
|
58
|
+
run_command("identify", "-format", format_option("%h"), @path).split("\n")[0].to_i
|
59
|
+
when "width"
|
60
|
+
run_command("identify", "-format", format_option("%w"), @path).split("\n")[0].to_i
|
61
|
+
when "size"
|
62
|
+
File.size(@path) # Do this because calling identify -format "%b" on an animated gif fails!
|
63
|
+
when "original_at"
|
64
|
+
# Get the EXIF original capture as a Time object
|
65
|
+
Time.local(*self["EXIF:DateTimeOriginal"].split(/:|\s+/)) rescue nil
|
66
|
+
when /^EXIF\:/i
|
67
|
+
run_command('identify', '-format', "\"%[#{value}]\"", @path).chop
|
68
|
+
else
|
69
|
+
run_command('identify', '-format', "\"#{value}\"", @path).split("\n")[0]
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Sends raw commands to imagemagick's mogrify command. The image path is automatically appended to the command
|
74
|
+
def <<(*args)
|
75
|
+
run_command("mogrify", *args << @path)
|
76
|
+
end
|
77
|
+
|
78
|
+
# This is a 'special' command because it needs to change @path to reflect the new extension
|
79
|
+
# Formatting an animation into a non-animated type will result in ImageMagick creating multiple
|
80
|
+
# pages (starting with 0). You can choose which page you want to manipulate. We default to the
|
81
|
+
# first page.
|
82
|
+
def format(format, page=0)
|
83
|
+
run_command("mogrify", "-format", format, @path)
|
84
|
+
|
85
|
+
old_path = @path.dup
|
86
|
+
@path.sub!(/(\.\w+)?$/, ".#{format}")
|
87
|
+
File.unlink(old_path) unless old_path == @path
|
88
|
+
|
89
|
+
unless File.exists?(@path)
|
90
|
+
begin
|
91
|
+
FileUtils.copy_file(@path.sub(".#{format}", "-#{page}.#{format}"), @path)
|
92
|
+
rescue Errno::ENOENT
|
93
|
+
raise MiniMagickError, "Page #{page} not found"
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
raise MiniMagickError, "Unable to format to #{format}" unless File.exist?(@path)
|
98
|
+
ensure
|
99
|
+
Dir[@path.sub(/(\.\w+)?$/, "-[0-9]*.#{format}")].each do |fname|
|
100
|
+
File.unlink(fname)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
|
105
|
+
# Writes the temporary image that we are using for processing to the output path
|
106
|
+
def write(output_path)
|
107
|
+
FileUtils.copy_file @path, output_path
|
108
|
+
run_command "identify", output_path # Verify that we have a good image
|
109
|
+
end
|
110
|
+
|
111
|
+
# Give you raw data back
|
112
|
+
def to_blob
|
113
|
+
f = File.new @path
|
114
|
+
f.binmode
|
115
|
+
f.read
|
116
|
+
ensure
|
117
|
+
f.close
|
118
|
+
end
|
119
|
+
|
120
|
+
# If an unknown method is called then it is sent through the morgrify program
|
121
|
+
# Look here to find all the commands (http://www.imagemagick.org/script/mogrify.php)
|
122
|
+
def method_missing(symbol, *args)
|
123
|
+
args.push(@path) # push the path onto the end
|
124
|
+
run_command("mogrify", "-#{symbol}", *args)
|
125
|
+
self
|
126
|
+
end
|
127
|
+
|
128
|
+
# You can use multiple commands together using this method
|
129
|
+
def combine_options(&block)
|
130
|
+
c = CommandBuilder.new
|
131
|
+
block.call c
|
132
|
+
run_command("mogrify", *c.args << @path)
|
133
|
+
end
|
134
|
+
|
135
|
+
# Check to see if we are running on win32 -- we need to escape things differently
|
136
|
+
def windows?
|
137
|
+
!(RUBY_PLATFORM =~ /win32/).nil?
|
138
|
+
end
|
139
|
+
|
140
|
+
# Outputs a carriage-return delimited format string for Unix and Windows
|
141
|
+
def format_option(format)
|
142
|
+
windows? ? "#{format}\\n" : "#{format}\\\\n"
|
143
|
+
end
|
144
|
+
|
145
|
+
def run_command(command, *args)
|
146
|
+
args.collect! do |arg|
|
147
|
+
arg = arg.to_s
|
148
|
+
arg = %|"#{arg}"| unless arg[0] == ?- # values quoted because they can contain characters like '>', but don't quote switches
|
149
|
+
arg
|
150
|
+
end
|
151
|
+
|
152
|
+
@output = `#{command} #{args.join(' ')}`
|
153
|
+
|
154
|
+
if $? != 0
|
155
|
+
raise MiniMagickError, "ImageMagick command (#{command} #{args.join(' ')}) failed: Error Given #{$?}"
|
156
|
+
else
|
157
|
+
@output
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
class CommandBuilder
|
163
|
+
attr :args
|
164
|
+
|
165
|
+
def initialize
|
166
|
+
@args = []
|
167
|
+
end
|
168
|
+
|
169
|
+
def method_missing(symbol, *args)
|
170
|
+
@args << "-#{symbol}"
|
171
|
+
@args += args
|
172
|
+
end
|
173
|
+
|
174
|
+
def +(value)
|
175
|
+
@args << "+#{value}"
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: remvee-mini_magick
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.3.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Corey Johnson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-08-13 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Uses command-line ImageMagick tools to resize, rotate, and mogrify images.
|
17
|
+
email: probablycorey@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- Rakefile
|
26
|
+
- README.rdoc
|
27
|
+
- MIT-LICENSE
|
28
|
+
- lib/image_temp_file.rb
|
29
|
+
- lib/mini_magick.rb
|
30
|
+
has_rdoc: true
|
31
|
+
homepage: http://mini-magick.rubyforge.org/
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: "0"
|
42
|
+
version:
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
requirements:
|
50
|
+
- none
|
51
|
+
rubyforge_project: mini_magick
|
52
|
+
rubygems_version: 1.2.0
|
53
|
+
signing_key:
|
54
|
+
specification_version: 2
|
55
|
+
summary: Manipulate images with minimal use of memory.
|
56
|
+
test_files: []
|
57
|
+
|