vendor/gems/prawn-0.3.0/lib/prawn/images.rb
# add the image at filename to the current page. Currently only
# JPG and PNG files are supported.
#
# Arguments:
# <tt>file</tt>:: path to file or an object that responds to #read
#
# Options:
# <tt>:at</tt>:: an array [x,y] with the location of the top left corner of the image.
# <tt>:position</tt>::  One of (:left, :center, :right) or an x-offset
# <tt>:vposition</tt>::  One of (:top, :center, :center) or an y-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>:fit</tt>:: scale the dimensions of the image proportionally to fit inside [width,height]
# 
#   Prawn::Document.generate("image2.pdf", :page_layout => :landscape) do     
#     pigs = "#{Prawn::BASEDIR}/data/images/pigs.jpg" 
#     image pigs, :at => [50,450], :width => 450                                      
#
#     dice = "#{Prawn::BASEDIR}/data/images/dice.png"
#     image dice, :at => [50, 450], :scale => 0.75 
#   end   
#
# 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 :at is provided, the image will be place in the current page but
# the text position will not be changed.
#
#
# 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)
# 
add the image at filename to the current page. Currently only
JPG and PNG files are supported.

file:: path to file or an object that responds to #read

:at:: an array [x,y] with the location of the top left corner of the image.





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 :at is provided, the image will be place in the current page but
the text position will not be changed.

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"



This method returns an image info object which can be used to check the
dimensions of an image object if needed.
add the image at filename to the current page . Currently only JPG and PNG files are supported . file :: path to file or an object that responds to #read : at :: an array [ x y ] with the location of the top left corner of the image . 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 : at is provided the image will be place in the current page but the text position will not be changed . 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 " This method returns an image info object which can be used to check the dimensions of an image object if needed .
def image(file, options={})
      Prawn.verify_options [:at, :position, :vposition, :height, 
                            :width, :scale, :fit], options
      if file.respond_to?(:read)
        image_content = file.read
      else      
        raise ArgumentError, "#{file} not found" unless File.file?(file)  
        image_content =  File.binread(file)
      end
      image_sha1 = Digest::SHA1.hexdigest(image_content)
      proc_set :ImageC
      image_obj = image_registry[image_sha1]
      if image_registry[image_sha1]
        info = image_registry[image_sha1][:info]
        image_obj = image_registry[image_sha1][:obj]
      else
        image_obj = case detect_image_format(image_content)
        when :jpg then
          info = Prawn::Images::JPG.new(image_content)
          build_jpg_object(image_content, info)
        when :png then
          info = Prawn::Images::PNG.new(image_content)
          build_png_object(image_content, info)
        end
        image_registry[image_sha1] = {:obj => image_obj, :info => info}
      end
      w,h = calc_image_dimensions(info, options)
      if options[:at]     
        x,y = translate(options[:at]) 
      else                  
        x,y = image_position(w,h,options) 
        move_text_position h   
      end
      label = "I#{next_image_id}"
      page_xobjects.merge!( label => image_obj )
      instruct = "\nq\n%.3f 0 0 %.3f %.3f %.3f cm\n/%s Do\nQ"
      add_content instruct % [ w, h, x, y - h, label ]
      return info
    end
def image ( file , options = { } ) Prawn . verify_options [ :at , :position , :vposition , :height , :width , :scale , :fit ] , options if file . respond_to? ( :read ) image_content = file . read else raise ArgumentError , "#{file} not found" unless File . file? ( file ) image_content = File . binread ( file ) end image_sha1 = Digest :: SHA1 . hexdigest ( image_content ) proc_set :ImageC image_obj = image_registry [ image_sha1 ] if image_registry [ image_sha1 ] info = image_registry [ image_sha1 ] [ :info ] image_obj = image_registry [ image_sha1 ] [ :obj ] else image_obj = case detect_image_format ( image_content ) when :jpg then info = Prawn :: Images :: JPG . new ( image_content ) build_jpg_object ( image_content , info ) when :png then info = Prawn :: Images :: PNG . new ( image_content ) build_png_object ( image_content , info ) end image_registry [ image_sha1 ] = { :obj => image_obj , :info => info } end w , h = calc_image_dimensions ( info , options ) if options [ :at ] x , y = translate ( options [ :at ] ) else x , y = image_position ( w , h , options ) move_text_position h end label = "I#{next_image_id}" page_xobjects . merge! ( label => image_obj ) instruct = "\nq\n%.3f 0 0 %.3f %.3f %.3f cm\n/%s Do\nQ" add_content instruct % [ w , h , x , y - h , label ] return info end
add the image at filename to the current page.
add the image at filename to the current page .
# register the fact that the current page uses images # if this image has already been embedded, just reuse it # build the image object and embed the raw data # find where the image will be placed and how big it will be   # add a reference to the image object to the current page # resource list and give it a label # add the image to the current page
file
<not_specific>
options
<not_specific>