# File lib/pdf/writer/graphics.rb, line 648
648:   def image(image, options = {})
649:     width   = options[:width]
650:     pad     = options[:pad]           || 5
651:     resize  = options[:resize]
652:     just    = options[:justification] || :left
653:     border  = options[:border]
654:     link    = options[:link]
655: 
656:     if image.kind_of?(PDF::Writer::External::Image)
657:       info        = image.image_info
658:       image_data  = image
659:     else
660:       if image.respond_to?(:read)
661:         image_data = image.read
662:       else
663:         image_data = open(image, "rb") { |file| file.read }
664:       end
665:       info = PDF::Writer::Graphics::ImageInfo.new(image_data)
666:     end
667: 
668:     raise "Unsupported Image Type" unless %w(JPEG PNG).include?(info.format)
669: 
670:     width   = info.width if width.nil?
671:     aspect  = info.width.to_f / info.height.to_f
672: 
673:       # Get the maximum width of the image on insertion.
674:     if @columns_on
675:       max_width = @columns[:width] - (pad * 2)
676:     else
677:       max_width = @page_width - (pad * 2) - @left_margin - @right_margin
678:     end
679: 
680:     if resize == :full or resize == :width or width > max_width
681:       width = max_width
682:     end
683: 
684:       # Keep the height in an appropriate aspect ratio of the width.
685:     height = (width / aspect.to_f)
686: 
687:       # Resize the image.
688:     if resize.kind_of?(Numeric)
689:       width   *= resize
690:       height  *= resize
691:     end
692: 
693:       # Resize the image *again*, if it is wider than what is available.
694:     if width > max_width
695:       height = (width / aspect.to_f)
696:     end
697: 
698:       # If the height is greater than the available space:
699:     havail = @y - @bottom_margin - (pad * 2)
700:     if height > havail
701:         # If the image is to be resized to :full (remaining space
702:         # available), adjust the image size appropriately. Otherwise, start
703:         # a new page and flow to the next page.
704:       if resize == :full
705:         height = havail
706:         width = (height * aspect)
707:       else
708:         start_new_page
709:       end
710:     end
711: 
712:       # Find the x and y positions.
713:     y = @y - pad - height
714:     x = @left_margin + pad
715: 
716:     if (width < max_width)
717:       case just
718:       when :center
719:         x += (max_width - width) / 2.0
720:       when :right
721:         x += (max_width - width)
722:       end
723:     end
724: 
725:     image_obj = add_image(image_data, x, y, width, height, info)
726: 
727:     if border
728:       border = {} if true == border
729:       border[:color]  ||= Color::RGB::Grey50
730:       border[:style]  ||= PDF::Writer::StrokeStyle::DEFAULT
731: 
732:       save_state
733:       stroke_color border[:color] 
734:       stroke_style border[:style] 
735:       rectangle(x, y - pad, width, height - pad).stroke
736:       restore_state
737:     end
738: 
739:     if link
740:       case link[:type]
741:       when :internal
742:         add_internal_link(link[:target], x, y - pad, x + width, y + height - pad)
743:       when :external
744:         add_link(link[:target], x, y - pad, x + width, y + height - pad)
745:       end
746:     end
747: 
748:     @y = @y - pad - height
749: 
750:     image_obj
751:   end