Forum: Ruby on Rails Returning just an image via a controller

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
kheon (Guest)
on 2005-11-18 04:04
(Received via mailing list)
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.
removed_email_address@domain.invalid
gboyer (Guest)
on 2005-11-18 05:17
(Received via mailing list)
On 11/17/05, Kyle H. <removed_email_address@domain.invalid> wrote:
> folder on disk and return the image.
>
>
>

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

e.g. <div id="header" style="background-image: url(<%= random_image
%>)">...</div>

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

Gabe
kheon (Guest)
on 2005-11-18 06:14
(Received via mailing list)
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.
removed_email_address@domain.invalid
andy (Guest)
on 2005-11-18 06:53
(Received via mailing list)
>> 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
nicksieger (Guest)
on 2005-11-18 06:53
(Received via mailing list)
>
> 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
nicksieger (Guest)
on 2005-11-18 06:53
(Received via mailing list)
On 11/17/05, Andy S. <removed_email_address@domain.invalid> 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.
mdillon (Guest)
on 2005-11-18 08:14
(Received via mailing list)
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
Jarkko L. (Guest)
on 2005-11-18 10:03
(Received via mailing list)
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
mikkel (Guest)
on 2005-11-18 11:45
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!
smedberg (Guest)
on 2005-11-18 19:10
(Received via mailing list)
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 => /^.*\..*$/
This topic is locked and can not be replied to.