views:

588

answers:

8

I have written a CGI script that creates an image dynamically using GET data. To include this image in my webpage, I am using the following code:

<img src="image.py?text=xxxxxxxxxxxxxx">

The problem is that I expect in the future the "text" field will get very long and the URL will become too large. From Googling around there doesn't seem to be a fixed limit on URL length (ie. depends on the browser, server, proxy, etc.) Is there a better way to do this?

If it matters, I am working with Django and Python and I cannot use any client-side scripting (ie. JavaScript).

Cheers, Ben

A: 

img's use GET. You'll have to come up with another mechanism. How about calling the same functionality in image.py and saving the file as a temp file which you ref in the img tag? Or how about saving the value of text in a db row during the rendering of this img tag and using the row_id as what you pass into the image.py script?

mspmsp
+5  A: 

Store the text somewhere (e.g. a database) and then pass through the primary key.

Greg
+1  A: 

This will get you an Image as the result of a POST -- you may not like it

  1. Put an iFrame where you want the image and size it and remove scrollbars
  2. Set the src to a form with hidden inputs set to your post parameters and the action set to the URL that will generate the image
  3. submit the form automatically with JavaScript in the body.onload of the iFrame's HTML

    Then, either:

  4. Serve back an content-type set to an image and stream the image bytes

    or:

  5. store the post parameters somewhere and generate a small id

  6. serve back HTML with an img tag using the id in the url -- on the server look up the post parameters

    or:

  7. generate a page with an image tag with an embedded image

    http://danielmclaren.net/2008/03/embedding-base64-image-data-into-a-webpage

Lou Franco
+1  A: 

Putting together what has already been said, how about creating two pages. First page sends a POST request when the form is submitted (lets say to create_img.py) with a text=xxxxxxx... parameter. Then create_img.py takes the text parameter and creates an image with it and inserts it (or a filesystem reference) into the db, then when rendering the second page, generate img tags like <img src="render_img.py?row_id=0122">. At this point, render_img.py simply queries the db for the given image. Before creating the image you can check to see if its already in the database therefore reusing/recycling previous images with the same text parameter.

fuentesjr
A: 

You may be able to mitigate the problem by compressing the text in the get parameter.

John Montgomery
A: 

From the link below it looks like you'll be fine for a while ;)

http://www.boutell.com/newfaq/misc/urllength.html

J.D. Fitz.Gerald
A: 

If you're using django, maybe you can do this via a template tag instead?

Something like:

<img src="{% create_image "This is the text that will be displayed" %}">

The create_image function would create the image with a dummy/random/generated filename, and return the path.

This avoids having to GET or POST to the script, and the images will have manageable filenames.

I can see some potential issues with this approach, I'm just tossing the idea out there ;)

A: 

OK, I'm a bit late to the party, but you could use a mix of MHTML (for IE7 and below) and the data URI scheme (for all other modern browsers). It does require a bit of work on both client and server but you can ultimately end up with

newimg.src = 'blah';

The write-up on how to do this is at http://gingerbbm.com/?p=127.

gingerbbm