Returning just an image via a controller

Is is possible to just return an image via a controller?

I’m not really sure this will even work but I’m hoping that it will. In
my
CSS I have specified a background image for my header. I want to build
in
some logic that randomizes the header so instead of setting the image in
the
CSS directly I was hoping to send it to a controller named /headers.

What I want is for the headers controller to randomly pick an image from
a
folder on disk and return the image.

Any thoughts? Is this even possible?

Kyle H.
[email protected]

On 11/17/05, Kyle H. [email protected] wrote:

folder on disk and return the image.

Is there any reason this couldn’t simply be done with a helper method?

e.g.

I think that would be the simplest way to do what you want, unless I’m
misunderstanding you.

Gabe

That could work, certainly. Right now the background image is defined in
the
CSS file but you propose a pretty simple workaround. Thanks.

I’d still be interested in how to return an image via a controller so
that I
can apply that to other things in the future. There must be a way to
change
the content-type that is returned and return something of that type.

Kyle H.
[email protected]

I’d still be interested in how to return an image via a controller so that
I
can apply that to other things in the future. There must be a way to
change
the content-type that is returned and return something of that type.

You probably want to do

response.headers[“Content-Type”] = “image/jpeg”

or similar, followed by a

render :text => image_content_here

although I haven’t tried this personally and I’m not sure at the moment
if
line ending issues will bite you. Hopefully that’s enough to get you
going
in the right direction.

Cheers,
/Nick

Is is possible to just return an image via a controller?

Try putting this in your controller method of choice:

send_data(content, :type => “some/mime-type”, :disposition => “inline”)

Where content is your image data.

Andy

You may also use the send_file("/images/some.jpg") method in your
controller. I usually use send_data if I’m sending data from a blob
in my database, and send_file when sending a file from the filesystem.

Cheers,
Mark

On 11/17/05, Andy S. [email protected] wrote:

Is is possible to just return an image via a controller?

Try putting this in your controller method of choice:

send_data(content, :type => “some/mime-type”, :disposition => “inline”)

Where content is your image data.

Nevermind my suggestion, Andy’s is much better.

On 18.11.2005, at 8.14, Mark Dillon wrote:

You may also use the send_file("/images/some.jpg") method in your
controller. I usually use send_data if I’m sending data from a blob
in my database, and send_file when sending a file from the filesystem.

You can combine this with routes so that the url of that given action
looks like an image url:

map.connect ‘mycontroller/random-image.jpg’, :controller =>
‘foo’, :action => ‘random_image’

//jarkko

jarkko wrote:

On 18.11.2005, at 8.14, Mark Dillon wrote:

You may also use the send_file("/images/some.jpg") method in your
controller. I usually use send_data if I’m sending data from a blob
in my database, and send_file when sending a file from the filesystem.

You can combine this with routes so that the url of that given action
looks like an image url:

map.connect ‘mycontroller/random-image.jpg’, :controller =>
‘foo’, :action => ‘random_image’

//jarkko

wow!

I wanted to do something similar. My app has a thumbnail image and
“full”
image for each user, and I store them in the DB as a blob. I want to
return
them to the client through an URL (that is, other parts of my app can
just
put in the correct URL, without having to know much about how the images
are
stored or generated.)

Additionally, I wanted to to let the user upload various kinds of images
(JPEG, GIF, etc.), and I wanted the URLs to look “right” (e.g. have the
right extension, etc.)

Here’s the code I wrote to return images (in my User controller):

def image
#MES- Need security here?
@user = User.find(params[:id])
send_image(@user.image)
end

def thumbnail
#MES- Need security here?
@user = User.find(params[:id])
send_image(@user.thumbnail)
end

def send_image(img)
send_data(img.data, :filename => img.name http://img.name, :type =>
img.content_type, :disposition => ‘inline’)
end

The @user.image and @user.thumbnail members are basically the Picture
class
from the Agile book, with some small additions for turning full sized
images
into thumbnails, etc.

To make the URL for a thumbnail, I use code like this

image_tag(url_for(:controller => ‘user’, :action => ‘thumbnail’, :id =>
“#{
user.id http://user.id}#{user.thumbnail.extension}”))

and then in my routes, I map all extensions to the same method (e.g. a
request to /user/image/123.gif will call UserController::image with ID
123):

#MES- Map any file extension for user ‘image’ or ‘thumbnail’ to the
action
map.connect ‘user/image/:id’, :controller => ‘user’, :action => ‘image’,
:id
=> /^...$/
map.connect ‘user/thumbnail/:id’, :controller => ‘user’, :action =>
‘thumbnail’, :id => /^...$/