[Borges-users] Dynamically creating images with Borges

Eric Hodel drbrain at segment7.net
Mon May 3 17:17:11 EDT 2004


Michael Neumann (mneumann at ntecs.de) wrote:

> On Mon, May 03, 2004 at 10:31:00PM +0400, ??????? ???????? wrote:
> > >The problem is not the image generation, but how to send the image data
> > >back to the client (register a callback/url). 
> > 
> > Here's how I did it 5 minutes ago (this is from a controller's 
> > render_on(r)):
> > 
> > data = File.open('plasm.jpg') {|f| f.binmode; f.read} # we have some 
> > binary data
> > r.image r.url_for_document(data,"image/jpeg") # we make the image's 
> > URL point to this data, with a mime-type
> 
> Hm, this way the image is the same for all sessions, isn't it? And the
> URL will not be reclaimed (it will stay forever).

The image is stored in a DocumentHandler, but it could be subclassed to
expire with the session, after N seconds, etc.

> What I'd like to have is the following:
> 
>   a = proc {
>     # generate image
>     # and return image data to client
>   }
> 
>   r.image(r.url_for(&a), "my image")

A subclass of RequestHandler could do this:

class ProcHandler < Borges::DocumentHandler
  def initialize(mime_type, &block)
    @mime_type = mime_type
    @response_proc = block
  end

  def handle_request(req)
    GenericResponse.new @mime_type, @response_proc.call
  end

  def active?
    true # need more code here to expire when the session does.
  end
end

To use it you would do something like:

class Foo < Borges::Component
  attr_reader :image_url

  def initialize
    handler = ProcHandler.new do
      # build image ...
    end
    @image_url = Borges::Session.current_session.application.url_for_request_handler(handler)
  end

  def render_content_on(r)
    r.image @image_url
  end
end

(Of course, this is all untested)

> Furthermore, I'd like to interpret the URL sent by the browser for the
> image to generate. Because if the image is for example an imagemap, the
> browser appends the mouse position at the URL (e.g. ?10,10 if you click
> at x=10, y=10 with the mouse).

The GET/POST variables are not yet available directly.  I'm not certian
where exactly you have to go to extract them though.  I will look
tonight.

> Oh, Borges model is great, but it's a bit hard to understand... it's
> probably too magic :-)

Over the past week, I've checked in a ton of tests and RDoc for
RequestHandlers.

-- 
Eric Hodel - drbrain at segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E  7C11 332A 551C 796C 9F04

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url : http://rubyforge.org/pipermail/borges-users/attachments/20040503/9742a6d0/attachment.bin


More information about the Borges-users mailing list