lib/image_voodoo.rb in image_voodoo-0.8.7 vs lib/image_voodoo.rb in image_voodoo-0.8.8
- old
+ new
@@ -1,5 +1,14 @@
+# Before we load image_voodoo we can specify whether we want it to load full
+# AWT ala http://www.oracle.com/technetwork/articles/javase/headless-136834.html
+# Most users are using image_voodoo as a library for manipulation and do not
+# want a full peer window popping up.
+unless defined? ImageVoodoo::NEEDS_HEAD
+ java.lang.System.set_property 'java.awt.headless', 'true'
+end
+
+
##
#
# = ImageVoodoo
# == Description
#
@@ -42,13 +51,12 @@
require 'image_voodoo/gae'
rescue
require 'image_voodoo/awt'
end
- def initialize(src, format=nil)
- @src = src
- @format = format
+ def initialize(io, src, format=nil)
+ @io, @src, @format = io, src, format
@quality = nil # nil means no specific quality ever specified
end
##
#
@@ -81,11 +89,22 @@
java_bytes = guard { bytes_impl(format) }
String.from_java_bytes java_bytes
end
##
+ # If current image was taken by a phone it might save the orientation
+ # in format it was physically taken and added IFD0 Orientation information
+ # instead of rotating it. This method will perform that rotation based
+ # on Orientation metadata.
#
+ def correct_orientation
+ target = guard { correct_orientation_impl }
+ block_given? ? yield(target) : target
+ end
+
+ ##
+ #
# Creates a square thumbnail of the image cropping the longest edge to
# match the shortest edge, resizes to size, and yields/returns the new image.
#
def cropped_thumbnail(size)
l, t, r, b, half = 0, 0, width, height, (width - height).abs / 2
@@ -124,10 +143,18 @@
end
alias_method :grayscale, :greyscale
##
#
+ # Extracts metadata from an image.
+ #
+ def metadata
+ guard { metadata_impl }
+ end
+
+ ##
+ #
# Creates a negative and yields/returns the new image.
#
def negative
target = guard { negative_impl }
block_given? ? yield(target) : target
@@ -159,10 +186,19 @@
raise ArgumentError, ne.message
end
##
#
+ # Rotates the image by angle (specified in degrees).
+ #
+ def rotate(angle)
+ target = guard { rotate_impl(angle) }
+ block_given? ? yield(target) : target
+ end
+
+ ##
+ #
# Saves the image out to path. Changing the file extension will convert
# the file type to the appropriate format.
#
def save(file)
format = File.extname(file)
@@ -206,10 +242,10 @@
##
#
# A top-level image loader opens path and then yields/returns the image.
#
def self.with_image(path)
- raise ArgumentError, "file does not exist" unless File.file?(path)
+ raise ArgumentError, "file does not exist: #{path}" unless File.file?(path)
image = guard { with_image_impl(JFile.new(path)) }
image && block_given? ? yield(image) : image
end
##