Module: Prawn::Images

Included in:
Document
Defined in:
lib/prawn/images.rb,
lib/prawn/images/jpg.rb,
lib/prawn/images/png.rb

Defined Under Namespace

Classes: JPG, PNG

Instance Method Summary collapse

Instance Method Details

#image(filename, options = {}) ⇒ Object

add the image at filename to the current page. Currently only JPG and PNG files are supported.

Arguments:

filename

the path to the file to be embedded

Options:

:at

the location of the top left corner of the image.

:position

One of (:left, :center, :right) or an x-offset

:height

the height of the image [actual height of the image]

:width

the width of the image [actual width of the image]

:scale

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

  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.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/prawn/images.rb', line 40

def image(filename, options={})
  Prawn.verify_options [:at,:position, :height, :width, :scale], options

  if NKF.guess(filename) == NKF::BINARY
    image_content = filename
  else
    raise ArgumentError, "#{filename} not found" unless File.file?(filename)

    image_content =  File.read_binary(filename)
  end

  image_sha1 = Digest::SHA1.hexdigest(image_content)

  # register the fact that the current page uses images
  proc_set :ImageC

  # if this image has already been embedded, just reuse it
  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
    # build the image object and embed the raw data
    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

  # find where the image will be placed and how big it will be
  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

  # add a reference to the image object to the current page
  # resource list and give it a label
  label = "I#{next_image_id}"
  page_xobjects.merge!( label => image_obj )

  # add the image to the current page
  instruct = "\nq\n%.3f 0 0 %.3f %.3f %.3f cm\n/%s Do\nQ"
  add_content instruct % [ w, h, x, y - h, label ]
end