Hi, I am looking for the best solution to resize images to fit within a certain area. The images will only be made smaller if they will not fit within a fixed size area. It is important to maintain the same proportions. This ImageMagick extension looks promising: http://vantulder.net/rails/magick/ However, I believe it may be overkill for just resizing images sometimes. I have also seen a few javascript examples for resizing, but may not degrade nicely. Also, these images are stored externally and are accessed via http://somesite.com/someimage.gif syntax which the ImageMagick extention does not seem to cater for (maybe after_filter will work though). Any feedback on this is appreciated. Thanks, Tom
On 10/11/05, Tom Davies <atomgiant-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I am looking for the best solution to resize images to fit within a > certain area. The images will only be made smaller if they will not > fit within a fixed size area. It is important to maintain the same > proportions. > > This ImageMagick extension looks promising: > > http://vantulder.net/rails/magick/ > > However, I believe it may be overkill for just resizing images > sometimes. I have also seen a few javascript examples for resizing, > but may not degrade nicely.I''ve been using ruby-gd (http://raa.ruby-lang.org/project/ruby-gd/) on one site for quite awhile now.> > Also, these images are stored externally and are accessed via > http://somesite.com/someimage.gif syntax which the ImageMagick > extention does not seem to cater for (maybe after_filter will work > though). > > Any feedback on this is appreciated. > > Thanks, > Tom > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Hey Tom, Well, it *sounds* as if you want Rails to serve your images and do a check to see if they fit within a certain size. You can do this, of course, but, depending on how you have your routes configured that might involve Rails serving up every image requested, so performance could be a big issue. However, with some careful configuration, you could get everything set up to check only those images served from a certain space, like your example url. Otherwise, just installing RMagick (http://rmagick.rubyforge.org/) is your easiest bet. YMMV on installing depending on your platform, but it has done the trick for me on Windows (there were some issues that took some ironing out, if I remember), FreeBSD 5.3 and 5.4, Ubuntu, and Gentoo. Once you''ve got RMagick installed, it''s just a matter of checking your dimensions and reacting appropriately, for example: def inline img = Magick::Image::read("<some-file-somewhere>").first if img.columns > SOME_CONSTANT img.change_geometry!(DEFAULT_IMG_DISPLAY_W) { |cols, rows, img| img.resize!(cols, rows) } end return_data = img.to_blob send_data return_data, :type => ''image/jpeg'', :disposition => ''inline'' end I glanced at van Tulder''s imagemagick tags, and they look neat, actually. I''d like it if he put more dates up on his page, so it would be easier to track updates and progress, although there are dates in the RDocs. Otherwise, I don''t know why they''d be overkill, per se. I mean, if it ultimately saves you time and causes no one undue discomfort, then hey, fly the F-16 instead of the Cessna, by all means. Peace, Greg
Hi Greg, Thanks for your excellent response. I like the F-16 analogy :) I just have a couple followup questions if you have a moment. To give a bit more background, I will allow the linking of images from external sites from my site. There may be up to 10 images of this type per page. If I load these images up but do not resize them, will there still be a big performance hit? One other option I was considering was to just set the width and height properties in HTML for each image. But to do that, I would need to get the aspect ratio for each image. Is there an efficient way to use RMagick for this? Thanks again, Tom On 10/11/05, Greg McClure <gmcclure-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hey Tom, > > Well, it *sounds* as if you want Rails to serve your images and do a > check to see if they fit within a certain size. You can do this, of > course, but, depending on how you have your routes configured that > might involve Rails serving up every image requested, so performance > could be a big issue. However, with some careful configuration, you > could get everything set up to check only those images served from a > certain space, like your example url. > > Otherwise, just installing RMagick (http://rmagick.rubyforge.org/) is > your easiest bet. YMMV on installing depending on your platform, but > it has done the trick for me on Windows (there were some issues that > took some ironing out, if I remember), FreeBSD 5.3 and 5.4, Ubuntu, > and Gentoo. > > Once you''ve got RMagick installed, it''s just a matter of checking your > dimensions and reacting appropriately, for example: > > def inline > img = Magick::Image::read("<some-file-somewhere>").first > if img.columns > SOME_CONSTANT > img.change_geometry!(DEFAULT_IMG_DISPLAY_W) { |cols, rows, img| > img.resize!(cols, rows) > } > end > return_data = img.to_blob > send_data return_data, :type => ''image/jpeg'', :disposition => ''inline'' > end > > I glanced at van Tulder''s imagemagick tags, and they look neat, > actually. I''d like it if he put more dates up on his page, so it would > be easier to track updates and progress, although there are dates in > the RDocs. Otherwise, I don''t know why they''d be overkill, per se. I > mean, if it ultimately saves you time and causes no one undue > discomfort, then hey, fly the F-16 instead of the Cessna, by all > means. > > Peace, > Greg > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On 12/10/05, Tom Davies <atomgiant-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Greg, > One other option I was considering was to just set the width and > height properties in HTML for each image. But to do that, I would > need to get the aspect ratio for each image. Is there an efficient > way to use RMagick for this?There is certainly an efficient way in RMagick for this, in fact the answer was allready in Greg''s post. So let''s have a look:> > def inline > > img = Magick::Image::read("<some-file-somewhere>").first > > if img.columns > SOME_CONSTANTImage#columns ans Image#rows give you the width and height of the image in pixels. http://studio.imagemagick.org/RMagick/doc/imageattrs.html#columns http://studio.imagemagick.org/RMagick/doc/imageattrs.html#rows grtz, wannes
Hi wannes, Thanks. I understood that. By efficient, I meant in terms of performance on the server resources :) Not based on how easy it would be to do it using RMagick. Tom On 10/12/05, wannes <oeaniz-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 12/10/05, Tom Davies <atomgiant-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi Greg, > > One other option I was considering was to just set the width and > > height properties in HTML for each image. But to do that, I would > > need to get the aspect ratio for each image. Is there an efficient > > way to use RMagick for this? > > There is certainly an efficient way in RMagick for this, in fact the > answer was allready in Greg''s post. So let''s have a look: > > > > def inline > > > img = Magick::Image::read("<some-file-somewhere>").first > > > if img.columns > SOME_CONSTANT > > Image#columns ans Image#rows give you the width and height of the > image in pixels. > > http://studio.imagemagick.org/RMagick/doc/imageattrs.html#columns > http://studio.imagemagick.org/RMagick/doc/imageattrs.html#rows > > grtz, > wannes > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On 12-okt-2005, at 3:32, Greg McClure wrote:> > ..., if it ultimately saves you time and causes no one undue > discomfort, then hey, fly the F-16 instead of the Cessna, by all > means.That''s a good marketing motto for Rails I believe. -- Julian "Julik" Tarkhanov
Hey Tom, Well -- if I''m understanding you correctly -- if you''re going to be getting image dimensions on the fly then, no matter which methods in whichever library are used to get and set the HTML width and height elements, the library *has* to read the file from someplace, off the filesystem or from a database, then spit back the width and height values (columns and rows in RMagick) out of memory. Efficient, in that case, is a relative term. What is usually being asked is, "Is is efficient *enough*?" There''s a whole bunch of factors that go into that though: How many concurrent users on the site and how many requests are being served, how large the files which are being read, how beefy the hardware is, what other resource hogging activities are going on in the page in question. It might be just a matter of trying it and benchmarking it. A better approach might be to store the height and width values in a database as the files are being uploaded. In other words, write an after filter in the model which, after storing basic image details in a database and then saving the image to the database or to the filesystem, further uses RMagick or whatever to read the new file and do whatever necessary operations you need, like getting the dimensions, writing a thumbnail to another directory, etc. Or, if your files aren''t being uploaded, then you could write a method that could read a directory all at once and perform these operations, storing necessary values when required, after a new directory has been created for your new images, or whatever. That way, all the work has been done up front and users later on in the process aren''t wondering what''s taking the page so long to load. Hope this addresses your question. Best of luck, Greg
Thanks Greg. That is an excellent suggestion about performing the work up front. Given your feedback, my first iteration will probably be to take the easy way out and just tell the user a recommended maximum and then set the overflow to hidden in CSS. That will at least keep my layout from breaking. Thanks again for your feedback. Tom On 10/12/05, Greg McClure <gmcclure-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hey Tom, > > Well -- if I''m understanding you correctly -- if you''re going to be > getting image dimensions on the fly then, no matter which methods in > whichever library are used to get and set the HTML width and height > elements, the library *has* to read the file from someplace, off the > filesystem or from a database, then spit back the width and height > values (columns and rows in RMagick) out of memory. > > Efficient, in that case, is a relative term. What is usually being > asked is, "Is is efficient *enough*?" There''s a whole bunch of factors > that go into that though: How many concurrent users on the site and > how many requests are being served, how large the files which are > being read, how beefy the hardware is, what other resource hogging > activities are going on in the page in question. It might be just a > matter of trying it and benchmarking it. > > A better approach might be to store the height and width values in a > database as the files are being uploaded. In other words, write an > after filter in the model which, after storing basic image details in > a database and then saving the image to the database or to the > filesystem, further uses RMagick or whatever to read the new file and > do whatever necessary operations you need, like getting the > dimensions, writing a thumbnail to another directory, etc. > > Or, if your files aren''t being uploaded, then you could write a method > that could read a directory all at once and perform these operations, > storing necessary values when required, after a new directory has been > created for your new images, or whatever. That way, all the work has > been done up front and users later on in the process aren''t wondering > what''s taking the page so long to load. > > Hope this addresses your question. > > Best of luck, > Greg > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >