Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Version 0.6.0

Added to_blob function
  • Loading branch information...
commit 39422fcac0bd5361dd8eb6e377accbd174442a46 1 parent 485646c
@aseldawy authored
View
3  README
@@ -176,6 +176,9 @@ QuickMagick supports also ImageList s
i.draw_text(30, 30, "Hello world!", :rotate=>45)
i.save 'hello.jpg'
+... you can then convert it to blob using
+ i.to_blob
+
For more information on drawing API visit:
http://www.imagemagick.org/Usage/draw/
View
2  Rakefile
@@ -2,7 +2,7 @@ require 'rubygems'
require 'rake'
require 'echoe'
-Echoe.new('quick_magick', '0.5.4') do |p|
+Echoe.new('quick_magick', '0.6.0') do |p|
p.description = "QuickMagick allows you to access ImageMagick command line functions using Ruby interface."
p.url = "http://quickmagick.rubyforge.org/"
p.author = "Ahmed ElDawy"
View
2  lib/quick_magick.rb
@@ -56,7 +56,7 @@ module QuickMagick
vertical verticalbricks verticalleftshingle verticalrightshingle verticalsaw}
- class <<self
+ class << self
# Generate a random string of specified length.
# Used to generate random names for temp files
def random_string(length=10)
View
20 lib/quick_magick/image.rb
@@ -3,7 +3,7 @@
module QuickMagick
class Image
- class <<self
+ class << self
# create an array of images from the given blob data
def from_blob(blob, &proc)
@@ -392,6 +392,24 @@ def save!
alias write! save!
alias mogrify! save!
+
+ def to_blob
+ tmp_file = Tempfile.new(QuickMagick::random_string)
+ if command_line =~ /-format\s(\S+)\s/
+ # use format set up by user
+ blob_format = $1
+ elsif !@pseudo_image
+ # use original image format
+ blob_format = self.format
+ else
+ # default format is jpg
+ blob_format = 'jpg'
+ end
+ save "#{blob_format}:#{tmp_file.path}"
+ blob = nil
+ File.open(tmp_file.path, 'rb') { |f| blob = f.read}
+ blob
+ end
# image file format
def format
View
30 test/image_test.rb
@@ -228,6 +228,34 @@ def test_lines_with_colors
File.delete(out_filename) if out_filename && File.exists?(out_filename)
end
+ def test_to_blob_default_format
+ i = QuickMagick::Image.solid(100, 100)
+ i.draw_line(0,0,20,20)
+ i.draw_circle(30,30,20,20)
+ blob = i.to_blob
+ i2 = QuickMagick::Image.from_blob(blob).first
+ assert_equal 100, i2.width
+ assert_equal 100, i2.height
+ assert_equal 'JPEG', i2.format
+ end
+
+ def test_to_blob_with_custom_format
+ i = QuickMagick::Image.solid(100, 100)
+ i.draw_line(0,0,20,20)
+ i.draw_circle(30,30,20,20)
+ i.format = 'gif'
+ blob = i.to_blob
+ i2 = QuickMagick::Image.from_blob(blob).first
+ assert_equal 'GIF', i2.format
+ end
+
+ def test_to_blob_with_original_format
+ i = QuickMagick::Image.read(@logo_filename).first
+ blob = i.to_blob
+ i2 = QuickMagick::Image.from_blob(blob).first
+ assert_equal 'PNG', i2.format
+ end
+
def test_colors
assert_equal "#00ff00ff", QuickMagick::rgb_color(0, 255, 0)
assert_equal "#00007fff", QuickMagick::rgb_color(0, 0, 0.5)
@@ -252,6 +280,8 @@ def test_revert
i2 = QuickMagick::Image.read(out_filename).first
assert_equal 640, i2.width
assert_equal 480, i2.height
+ ensure
+ File.delete out_filename if File.exists?(out_filename)
end
def test_image_converted_from_pseudo_to_normal
Please sign in to comment.
Something went wrong with that request. Please try again.