Don''t know if anyone will find this useful, but it''s a little class that automatically resizes images on the fly. You need RMagick installed. In other words, you can go to a URL like /image/size/32-100 and it will return image #32 resized to 100 pixels wide. Or, /image/thumbnail/32 and it will return a thumbnail sized image #32. Ideas on how to improve this or comments on code style? class ImageController < ApplicationController def full redirect_to :action => "size", :id => @params["id"] end # Expects params of either "id" or "id-width". # Returns the specified picture (with the specified width if it''s given). def size input = @params["id"] id, width = input.split("-") db_image = Image.find_first(id) if width.nil? # No width given return_data = db_image.data else # Width given, resize! magick_image = Magick::Image.from_blob(db_image.data)[0] return_data = magick_image.change_geometry(width) do |w, h, img| img.resize(w,h) end.to_blob end send_data(return_data, :filename => db_image.filename, :type => db_image.mime, :disposition => "inline") end def thumbnail thumbnail_size = 100 redirect_to :action => "size", :id => "#{@params[''id'']}-#{thumbnail_size}" end end Image table: CREATE TABLE `images` ( `id` int(11) NOT NULL auto_increment, `filename` varchar(255) NOT NULL default '''', `data` longblob NOT NULL, `mime` varchar(20) NOT NULL default '''', `size` varchar(20) NOT NULL default '''', PRIMARY KEY (`id`) ) TYPE=MyISAM AUTO_INCREMENT=35 ;
I''ve actually got a question. When extracting images from the database, it sometimes seems to cache the image. So, when I use the below class, the function that returns an image will (sometimes) return the same image no matter what image id is passed in to it. Any ideas on how I can stop this? I''m in development mode with Webrick, caching appears off, latest stable gems. On Wed, 23 Feb 2005 04:49:51 -0800, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Don''t know if anyone will find this useful, but it''s a little class > that automatically resizes images on the fly. You need RMagick > installed. > > In other words, you can go to a URL like /image/size/32-100 and it > will return image #32 resized to 100 pixels wide. Or, > /image/thumbnail/32 and it will return a thumbnail sized image #32. > > Ideas on how to improve this or comments on code style? > > class ImageController < ApplicationController > > def full > redirect_to :action => "size", :id => @params["id"] > end > > # Expects params of either "id" or "id-width". > # Returns the specified picture (with the specified width if it''s given). > def size > input = @params["id"] > id, width = input.split("-") > > db_image = Image.find_first(id) > > if width.nil? > # No width given > return_data = db_image.data > else > # Width given, resize! > magick_image = Magick::Image.from_blob(db_image.data)[0] > return_data > magick_image.change_geometry(width) do |w, h, img| > img.resize(w,h) > end.to_blob > end > > send_data(return_data, > :filename => db_image.filename, > :type => db_image.mime, > :disposition => "inline") > end > > def thumbnail > thumbnail_size = 100 > redirect_to :action => "size", :id => > "#{@params[''id'']}-#{thumbnail_size}" > end > end > > Image table: > CREATE TABLE `images` ( > `id` int(11) NOT NULL auto_increment, > `filename` varchar(255) NOT NULL default '''', > `data` longblob NOT NULL, > `mime` varchar(20) NOT NULL default '''', > `size` varchar(20) NOT NULL default '''', > PRIMARY KEY (`id`) > ) TYPE=MyISAM AUTO_INCREMENT=35 ; >
I''m not sure if this is the cause of your problem, but since you''re location records by ID why don''t you just use: db_image = Image.find(id) instead of db_image = Image.find_first(id) Actually this may be the cause of your problem. find_first may just be returning the first record in the Images database, since you''re not providing a valid test condiation. You can verify this by looking at the log output that should be getting written to log/development.log, but I''m guessing you''re sending an SQL query that is just: SELECT * FROM Images LIMIT 1 Cheers, Ben On Wed, 23 Feb 2005 04:58:25 -0800, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''ve actually got a question. > > When extracting images from the database, it sometimes seems to cache > the image. So, when I use the below class, the function that returns > an image will (sometimes) return the same image no matter what image > id is passed in to it. > > Any ideas on how I can stop this? I''m in development mode with > Webrick, caching appears off, latest stable gems. > > > On Wed, 23 Feb 2005 04:49:51 -0800, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Don''t know if anyone will find this useful, but it''s a little class > > that automatically resizes images on the fly. You need RMagick > > installed. > > > > In other words, you can go to a URL like /image/size/32-100 and it > > will return image #32 resized to 100 pixels wide. Or, > > /image/thumbnail/32 and it will return a thumbnail sized image #32. > > > > Ideas on how to improve this or comments on code style? > > > > class ImageController < ApplicationController > > > > def full > > redirect_to :action => "size", :id => @params["id"] > > end > > > > # Expects params of either "id" or "id-width". > > # Returns the specified picture (with the specified width if it''s given). > > def size > > input = @params["id"] > > id, width = input.split("-") > > > > db_image = Image.find_first(id) > > > > if width.nil? > > # No width given > > return_data = db_image.data > > else > > # Width given, resize! > > magick_image = Magick::Image.from_blob(db_image.data)[0] > > return_data > > magick_image.change_geometry(width) do |w, h, img| > > img.resize(w,h) > > end.to_blob > > end > > > > send_data(return_data, > > :filename => db_image.filename, > > :type => db_image.mime, > > :disposition => "inline") > > end > > > > def thumbnail > > thumbnail_size = 100 > > redirect_to :action => "size", :id => > > "#{@params[''id'']}-#{thumbnail_size}" > > end > > end > > > > Image table: > > CREATE TABLE `images` ( > > `id` int(11) NOT NULL auto_increment, > > `filename` varchar(255) NOT NULL default '''', > > `data` longblob NOT NULL, > > `mime` varchar(20) NOT NULL default '''', > > `size` varchar(20) NOT NULL default '''', > > PRIMARY KEY (`id`) > > ) TYPE=MyISAM AUTO_INCREMENT=35 ; > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On Wed, 23 Feb 2005 14:22:19 +0100, Ben Schumacher <benschumacher-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m not sure if this is the cause of your problem, but since you''re > location records by ID why don''t you just use: > > db_image = Image.find(id) > > instead of > > db_image = Image.find_first(id) > > Actually this may be the cause of your problem. find_first may just be > returning the first record in the Images database, since you''re not > providing a valid test condiation. You can verify this by looking at > the log output that should be getting written to log/development.log, > but I''m guessing you''re sending an SQL query that is just: SELECT * > FROM Images LIMIT 1 > > Cheers, > > BenYou were correct, sir. Much thanks.> > > On Wed, 23 Feb 2005 04:58:25 -0800, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I''ve actually got a question. > > > > When extracting images from the database, it sometimes seems to cache > > the image. So, when I use the below class, the function that returns > > an image will (sometimes) return the same image no matter what image > > id is passed in to it. > > > > Any ideas on how I can stop this? I''m in development mode with > > Webrick, caching appears off, latest stable gems. > > > > > > On Wed, 23 Feb 2005 04:49:51 -0800, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Don''t know if anyone will find this useful, but it''s a little class > > > that automatically resizes images on the fly. You need RMagick > > > installed. > > > > > > In other words, you can go to a URL like /image/size/32-100 and it > > > will return image #32 resized to 100 pixels wide. Or, > > > /image/thumbnail/32 and it will return a thumbnail sized image #32. > > > > > > Ideas on how to improve this or comments on code style? > > > > > > class ImageController < ApplicationController > > > > > > def full > > > redirect_to :action => "size", :id => @params["id"] > > > end > > > > > > # Expects params of either "id" or "id-width". > > > # Returns the specified picture (with the specified width if it''s given). > > > def size > > > input = @params["id"] > > > id, width = input.split("-") > > > > > > db_image = Image.find_first(id) > > > > > > if width.nil? > > > # No width given > > > return_data = db_image.data > > > else > > > # Width given, resize! > > > magick_image = Magick::Image.from_blob(db_image.data)[0] > > > return_data > > > magick_image.change_geometry(width) do |w, h, img| > > > img.resize(w,h) > > > end.to_blob > > > end > > > > > > send_data(return_data, > > > :filename => db_image.filename, > > > :type => db_image.mime, > > > :disposition => "inline") > > > end > > > > > > def thumbnail > > > thumbnail_size = 100 > > > redirect_to :action => "size", :id => > > > "#{@params[''id'']}-#{thumbnail_size}" > > > end > > > end > > > > > > Image table: > > > CREATE TABLE `images` ( > > > `id` int(11) NOT NULL auto_increment, > > > `filename` varchar(255) NOT NULL default '''', > > > `data` longblob NOT NULL, > > > `mime` varchar(20) NOT NULL default '''', > > > `size` varchar(20) NOT NULL default '''', > > > PRIMARY KEY (`id`) > > > ) TYPE=MyISAM AUTO_INCREMENT=35 ; > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Joe, This looks nice. I haven''t tried it yet but it is something I would have had to tackle in the near future. I had a similar script in my previous PHP projects that I am now porting over to rails. The only thing that is missing from yours is the ability to give maximum dimensions for the longest side so the orientation of the image doesn''t affect the overall size. I only quickly perused your code and I haven''t tried it yet so please correct me if I have overlooked this feature. I understand that you can give a maximum width for the image and the height will be proportionally sized, however consider the following example. A landscape image, 200px wide and 100px tall would be resized to 100x50 with /image/size/32-100. A portrait image 100px wide and 200px tall would not be resized with /image/size/32-100 as the original width is 100px. This would allow a portrait image to be twice as big as the landscape image - not always ideal if you want to loop through an image gallery where you want them all to be the same size. A possible solution would be to add a slightly different method for just such an occasion. /image/max/1-100 might return an image where the longest side (width or height) is 100px and the other is proportionately scaled. The result might be used in something like http://alteradesign.com/portfolio/ where we have a mixture of orientations and we want them to all be scaled to the same size, not just same width. Just a thought. Thanks again for saving me some time. / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / R O B E R T B O U S Q U E T Web Programming Consultant Doheny Office of Technology Support University of Southern California On Feb 23, 2005, at 4:49 AM, Joe Van Dyk wrote:> Don''t know if anyone will find this useful, but it''s a little class > that automatically resizes images on the fly. You need RMagick > installed. > > In other words, you can go to a URL like /image/size/32-100 and it > will return image #32 resized to 100 pixels wide. Or, > /image/thumbnail/32 and it will return a thumbnail sized image #32. > > Ideas on how to improve this or comments on code style? > > > > class ImageController < ApplicationController > > def full > redirect_to :action => "size", :id => @params["id"] > end > > # Expects params of either "id" or "id-width". > # Returns the specified picture (with the specified width if it''s > given). > def size > input = @params["id"] > id, width = input.split("-") > > db_image = Image.find_first(id) > > if width.nil? > # No width given > return_data = db_image.data > else > # Width given, resize! > magick_image = Magick::Image.from_blob(db_image.data)[0] > return_data > magick_image.change_geometry(width) do |w, h, img| > img.resize(w,h) > end.to_blob > end > > send_data(return_data, > :filename => db_image.filename, > :type => db_image.mime, > :disposition => "inline") > end > > def thumbnail > thumbnail_size = 100 > redirect_to :action => "size", :id => > "#{@params[''id'']}-#{thumbnail_size}" > end > end > > > > Image table: > CREATE TABLE `images` ( > `id` int(11) NOT NULL auto_increment, > `filename` varchar(255) NOT NULL default '''', > `data` longblob NOT NULL, > `mime` varchar(20) NOT NULL default '''', > `size` varchar(20) NOT NULL default '''', > PRIMARY KEY (`id`) > ) TYPE=MyISAM AUTO_INCREMENT=35 ; > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On Wed, 2005-02-23 at 23:21 -0800, Robert Bousquet wrote:>Joe, > >I had a similar script in my previous PHP projects that I am now >porting over to rails. The only thing that is missing from yours is >the ability to give maximum dimensions for the longest side so the >orientation of the image doesn''t affect the overall size. I only >quickly perused your code and I haven''t tried it yet so please correct >me if I have overlooked this feature. >I do something similar to this but on upload of the images we create both a thumb and a mid to store in the db but trash the full size. Here''s how I scale it instead of just the width: @params[''image''][''thumb''] = magick_image.change_geometry(''100x100'') do |w, h, img| img.resize(w,h) end.to_blob @params[''image''][''mid''] = magick_image.change_geometry(''400x400'') do |w, h, img| img.resize(w,h) end.to_blob jim
On Thu, 24 Feb 2005 17:38:54 -0700, jim <jim-rhf1kIDhBaeB8E1WFlbJj6xOck334EZe@public.gmane.org> wrote:> On Wed, 2005-02-23 at 23:21 -0800, Robert Bousquet wrote: > >Joe, > > > >I had a similar script in my previous PHP projects that I am now > >porting over to rails. The only thing that is missing from yours is > >the ability to give maximum dimensions for the longest side so the > >orientation of the image doesn''t affect the overall size. I only > >quickly perused your code and I haven''t tried it yet so please correct > >me if I have overlooked this feature. > > > I do something similar to this but on upload of the images we create > both a thumb and a mid to store in the db but trash the full size. > Here''s how I scale it instead of just the width: > > @params[''image''][''thumb''] > magick_image.change_geometry(''100x100'') do |w, h, img| > img.resize(w,h) > end.to_blob > @params[''image''][''mid''] > magick_image.change_geometry(''400x400'') do |w, h, img| > img.resize(w,h) > end.to_blob > > jim > > _______________________________________________Does "100x100" force the image to that specific dimension? (i.e. change the image''s aspect ratio?) Joe
On Thu, 2005-02-24 at 16:43 -0800, Joe Van Dyk wrote:>Does "100x100" force the image to that specific dimension? (i.e. >change the image''s aspect ratio?) >According to the RMagick docs it scales the image to within those constraints. And from my visual testing the images aspect ratio is not changing. jim