lib/prawn/images.rb in prawn-0.2.1 vs lib/prawn/images.rb in prawn-0.2.2
- old
+ new
@@ -13,18 +13,18 @@
# add the image at filename to the current page. Currently only
# JPG and PNG files are supported.
#
# Arguments:
- # <tt>filename</tt>:: the path to the file to be embedded
+ # <tt>file</tt>:: path to file or an object that responds to #read
#
# Options:
# <tt>:at</tt>:: the location of the top left corner of the image.
# <tt>:position</tt>:: One of (:left, :center, :right) or an x-offset
# <tt>:height</tt>:: the height of the image [actual height of the image]
# <tt>:width</tt>:: the width of the image [actual width of the image]
- # <tt>:scale</tt>:: scale the dimensions of the image proportionally
+ # <tt>:scale</tt>:: scale the dimensions of the image proportionally
#
# Prawn::Document.generate("image2.pdf", :page_layout => :landscape) do
# pigs = "#{Prawn::BASEDIR}/data/images/pigs.jpg"
# image pigs, :at => [50,450], :width => 450
#
@@ -34,13 +34,27 @@
#
# If only one of :width / :height are provided, the image will be scaled
# proportionally. When both are provided, the image will be stretched to
# fit the dimensions without maintaining the aspect ratio.
#
+ # If instead of an explicit filename, an object with a read method is
+ # passed as +file+, you can embed images from IO objects and things
+ # that act like them (including Tempfiles and open-uri objects).
+ #
+ # require "open-uri"
+ #
+ # Prawn::Document.generate("remote_images.pdf") do
+ # image open("http://prawn.majesticseacreature.com/media/prawn_logo.png")
+ # end
+ #
+ # This method returns an image info object which can be used to check the
+ # dimensions of an image object if needed.
+ # (See also: Prawn::Images::PNG , Prawn::Images::JPG)
+ #
def image(file, options={})
Prawn.verify_options [:at,:position, :height, :width, :scale], options
- if file.kind_of?(IO)
+ if file.respond_to?(:read)
image_content = file.read
else
raise ArgumentError, "#{file} not found" unless File.file?(file)
image_content = File.read_binary(file)
end