Hi, I have a photo item described below: CREATE TABLE photos ( id int unsigned NOT NULL AUTO_INCREMENT default ''0'', user_id int unsigned NOT NULL default ''0'', photo mediumblob NOT NULL default '''', photo_width int unsigned NOT NULL default ''0'', photo_height int unsigned NOT NULL default ''0'', thumbnail mediumblob NOT NULL default '''', thumb_width int unsigned NOT NULL default ''0'', thumb_height int unsigned NOT NULL default ''0'', original mediumblob NOT NULL default '''', original_width int unsigned NOT NULL default ''0'', original_height int unsigned NOT NULL default ''0'', status tinyint unsigned NOT NULL default ''0'', created_on timestamp(14) NOT NULL, updated_on timestamp(14) NOT NULL, PRIMARY KEY (id) ) TYPE=MyISAM; I want to display the photo, the thumbnail, and the original photo as images in the list page but I don''t know how to do this. I know how to display just one of the images using the send_data like below: def show @photo = Photo.find(@params[:id]) send_data @photo.original, :filename => ''photo.jpg'', :type => ''image/jpeg'', :disposition => ''inline'' end Any help would be appreciated. Thanks, Frank
Ben Bleything
2005-Apr-28 02:12 UTC
Re: noob question: how do you display blob image data?
On Wed, Apr 27, 2005, Frank Kim wrote:> Hi, > I have a photo item described below: > > CREATE TABLE photos ( > id int unsigned NOT NULL AUTO_INCREMENT default ''0'', > user_id int unsigned NOT NULL default ''0'', > photo mediumblob NOT NULL default '''', > photo_width int unsigned NOT NULL default ''0'', > photo_height int unsigned NOT NULL default ''0'', > thumbnail mediumblob NOT NULL default '''', > thumb_width int unsigned NOT NULL default ''0'', > thumb_height int unsigned NOT NULL default ''0'', > original mediumblob NOT NULL default '''', > original_width int unsigned NOT NULL default ''0'', > original_height int unsigned NOT NULL default ''0'', > status tinyint unsigned NOT NULL default ''0'', > created_on timestamp(14) NOT NULL, > updated_on timestamp(14) NOT NULL, > PRIMARY KEY (id) > ) TYPE=MyISAM; > > I want to display the photo, the thumbnail, and the original photo as > images in the list page but I don''t know how to do this. I know how > to display just one of the images using the send_data like below: > > def show > @photo = Photo.find(@params[:id]) > > send_data @photo.original, :filename => ''photo.jpg'', :type => > ''image/jpeg'', :disposition => ''inline'' > end > > Any help would be appreciated.Seems to me you could create specialized actions for each (show, thumb, info, what-have-you) and then use regular img tags (or the equivalent Rails method) to embed them in the page. If your actions are serving up raw image data with a content type, shouldn''t that work? I''m a bit of a noob myself, so it''s just a guess :)> Thanks, > Frank
Frank Kim
2005-Apr-28 02:16 UTC
noob question: why are there three different ways of accessing fields?
Forgive the apparently dumb question but why does it seem like there are three ways to access a field: @user.send(''username'') @user.username @user[''username''] Are all the three ways the same? If not, what is the difference? Thanks, Frank
Jeremy Kemper
2005-Apr-28 03:08 UTC
Re: noob question: why are there three different ways of accessing fields?
Frank Kim wrote:> Forgive the apparently dumb question but why does it seem like there > are three ways to access a field: > > @user.send(''username'') > @user.username > @user[''username'']and @user.send(:read_attribute, ''username'')> Are all the three ways the same? If not, what is the difference?@user.send(''username'') is identical to @user.username. With send you can do convenient things like iterate over a list of method names and send each one to the object: [:bark, :woof, :beg].each { |method| @dog.send method } is equivalent to @dog.bark; @dog.woof, @dog.beg @user.username calls the username method if you''ve defined it and falls back to @user[''username''] otherwise. The fallback is implemented using another sweet Ruby gem, method_missing. If you define method_missing on your class you can catch method calls that you haven''t defined! class Dog def method_missing(method, *arguments) puts "#{method}!" arguments.each { |arg| puts " #{arg}" } end end @dog.bark(''arf'', ''aRf'', ''ARF'') bark! arf aRf ARF @user[''username''] calls @user.read_attribute(''username''), a private method that does the ''raw'' attribute lookup. Since it''s a private method, you can''t invoke it directly. But a bit of @user.send(:read_attribute, ''username'') magic can open any door. The short answer is: use @user.username whenever possible. This way you don''t have to worry whether it''s a custom method or just @user[''username''] -- it just works. Best, jeremy
http://manuals.rubyonrails.com/read/chapter/56 http://wiki.rubyonrails.com/rails/show/HowtoUploadFiles HowToUploadFiles used to have the relavant information, but may still be useful. The first link is "Sending and Receiving Files" and looks to be what you need. Essentially You''ll want a controller with something like the following: def ImagesController < ApplicationController caches_page :photo def photo mod_time = Image.get_mod_time @params[''id''] min_time = Time.rfc2822(@request.env["HTTP_IF_MODIFIED_SINCE"]) rescue nil if min_time and mod_time > min_time render_text '''', ''304 Not Modified'' else image = Image.find @params[''id''] send_data image.photo, :filename => image.filename, :type =>image.mime, :disposition=>''inline'' end end end and then the view would have something like: <%=image_tag url_for(:controller=>''images'',:action=>''photo'',:id=>3),:alt=>whatever%> Another tip: Use custom sql to avoid loading the blob when you''re just reading the metadata on the image (i.e. to create the image tag with alt, title, etc if they''re in the db). I have three thumb sizes, and I have a custom finder for each one to avoid loading potentially large db records to just display a thumbnail. I can distill some more code if you need it, but hopefully this should give you some direction. Brian
Heikki Tunkelo
2005-Apr-28 09:10 UTC
Re: noob question: how do you display blob image data?
You have to remember to send correct headers: Something like this: def show photo = photo.find(@params[''id'']) -sVi5de4ipzTkBw0ZPqwsow@public.gmane.org[''Pragma''] = '' '' -sVi5de4ipzTkBw0ZPqwsow@public.gmane.org[''Cache-Control''] = '' '' -sVi5de4ipzTkBw0ZPqwsow@public.gmane.org[''Content-type''] = ''image/jpg'' -sVi5de4ipzTkBw0ZPqwsow@public.gmane.org[''Content-Disposition''] = "attachment; filename=#{photo.name}" -sVi5de4ipzTkBw0ZPqwsow@public.gmane.org[''Accept-Ranges''] = ''bytes'' -sVi5de4ipzTkBw0ZPqwsow@public.gmane.org[''Content-Length''] = photo.data.length -sVi5de4ipzTkBw0ZPqwsow@public.gmane.org[''Content-Transfer-Encoding''] = ''binary'' -sVi5de4ipzTkBw0ZPqwsow@public.gmane.org[''Content-Description''] = ''Image data'' render_text photo.data rescue render_text "X" end On 4/28/05, Brian L. <zorander-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> http://manuals.rubyonrails.com/read/chapter/56 > http://wiki.rubyonrails.com/rails/show/HowtoUploadFiles > > HowToUploadFiles used to have the relavant information, but may still > be useful. The first link is "Sending and Receiving Files" and looks > to be what you need. > > Essentially You''ll want a controller with something like the following: > > def ImagesController < ApplicationController > caches_page :photo > def photo > mod_time = Image.get_mod_time @params[''id''] > min_time = Time.rfc2822(@request.env["HTTP_IF_MODIFIED_SINCE"]) rescue nil > if min_time and mod_time > min_time > render_text '''', ''304 Not Modified'' > else > image = Image.find @params[''id''] > send_data image.photo, :filename => image.filename, :type > =>image.mime, :disposition=>''inline'' > end > end > end > > and then the view would have something like: > <%=image_tag url_for(:controller=>''images'',:action=>''photo'',:id=>3),:alt=>whatever%> > > Another tip: Use custom sql to avoid loading the blob when you''re just > reading the metadata on the image (i.e. to create the image tag with > alt, title, etc if they''re in the db). I have three thumb sizes, and I > have a custom finder for each one to avoid loading potentially large > db records to just display a thumbnail. > > I can distill some more code if you need it, but hopefully this should > give you some direction. > > Brian > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- // heikki tunkelo
Raymond Brigleb
2005-May-03 03:29 UTC
Re: noob question: how do you display blob image data?
Frank, By any chance can you give me an example of how you''re getting the image data *to* the database? I''m completely stuck on this and I''ve tried every example I could find online. Would really appreciate seeing some working code. Kindest regards, Raymond On Apr 27, 2005, at 7:02 PM, Frank Kim wrote:> Hi, > I have a photo item described below: > > CREATE TABLE photos ( > id int unsigned NOT NULL AUTO_INCREMENT > default ''0'', > user_id int unsigned NOT NULL default ''0'', > photo mediumblob NOT NULL default '''', > photo_width int unsigned NOT NULL default ''0'', > photo_height int unsigned NOT NULL default ''0'', > thumbnail mediumblob NOT NULL default '''', > thumb_width int unsigned NOT NULL default ''0'', > thumb_height int unsigned NOT NULL default ''0'', > original mediumblob NOT NULL default '''', > original_width int unsigned NOT NULL default ''0'', > original_height int unsigned NOT NULL default ''0'', > status tinyint unsigned NOT NULL default ''0'', > created_on timestamp(14) NOT NULL, > updated_on timestamp(14) NOT NULL, > PRIMARY KEY (id) > ) TYPE=MyISAM; > > I want to display the photo, the thumbnail, and the original photo as > images in the list page but I don''t know how to do this. I know how > to display just one of the images using the send_data like below: > > def show > @photo = Photo.find(@params[:id]) > > send_data @photo.original, :filename => ''photo.jpg'', :type => > ''image/jpeg'', :disposition => ''inline'' > end > > Any help would be appreciated. > > Thanks, > Frank > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On 5/2/05, Raymond Brigleb <ray-THGPwszTed5CpjqP0VxSwUEOCMrvLtNR@public.gmane.org> wrote:> By any chance can you give me an example of how you''re getting the > image data *to* the database? I''m completely stuck on this and I''ve > tried every example I could find online.Hi Raymond - My database schema is a little different, but you should get the idea. (I''ve snipped out the extraneous stuff in the code below) Here''s the HTML form used to upload a new image: <!-- note the enctype attribute must be set as shown --> <form method="post" action="/fotos/create" enctype="multipart/form-data"> <!-- File input for the image thumbnail --> <input type="file" name="foto[thumb]" id="foto_thumb"/> <!-- file input for the image itself --> <input type="file" name="foto[image]" id="foto_image"/> </form> Here''s the action in the controller to which that form submits: def create # create the foto object with the basic parameters like name, description, etc. @foto = Foto.new(@params[''foto'']) # read the encoded image data from the form @foto.image = @params[''foto''][''image''].read @foto.thumb = @params[''foto''][''thumb''].read if @foto.save flash[''notice''] = ''Foto was successfully created.'' redirect_to :action => ''list'' else render_action ''new'' end end Here''s the controller''s action for retrieving the image for display in a page. You''d use this like <img src="/foto/5/img" width=... def img @foto = Foto.find(@params[''id'']) send_data @foto.image, :filename => "#{@foto.id}#{@foto.mime_type.extension}", :type => @foto.mime_type.content_type end And here''s an erb snippet for displaying one of these images (assuming there''s a Foto object in @foto) <img id="focusImage" src="<%= url_for :controller => ''fotos'', :action => ''img'', :id => @foto.id %>" alt="<%= h(@foto.caption) %>"> HTH... - Sean
On 5/3/05, Sean Santry <santry-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You''d use this like <img src="/foto/5/img" width=...Oops. Ignore that bit. That was a typo that I meant to delete. - Sean