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 Heon kheon-Wuw85uim5zDR7s880joybQ@public.gmane.org _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 11/17/05, Kyle Heon <kheon-Wuw85uim5zDR7s880joybQ@public.gmane.org> wrote:> > > 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 Heon > kheon-Wuw85uim5zDR7s880joybQ@public.gmane.org > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >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
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 Heon kheon-Wuw85uim5zDR7s880joybQ@public.gmane.org -----Original Message----- From: Gabe Boyer [mailto:gboyer-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org] Sent: Thursday, November 17, 2005 10:17 PM To: kheon-Wuw85uim5zDR7s880joybQ@public.gmane.org; rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] Returning just an image via a controller On 11/17/05, Kyle Heon <kheon-Wuw85uim5zDR7s880joybQ@public.gmane.org> wrote:> > > 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 controllernamed /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 Heon > kheon-Wuw85uim5zDR7s880joybQ@public.gmane.org > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >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
>> 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
> > 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 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 11/17/05, Andy Smith <andy-7ko9+6roZFNFio9eMEw3nA@public.gmane.org> 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. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
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 Smith <andy-7ko9+6roZFNFio9eMEw3nA@public.gmane.org> 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. > > Andy > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
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 Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
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'' > > //jarkkowow! -- Posted via http://www.ruby-forum.com/.
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 => /^.*\..*$/ On 11/18/05, mikkel <mikkel-zZeBJi6nFjBknbxzx/v8hQ@public.gmane.org> wrote:> > 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! > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails