Getting image dimensions with file_column

If I’m using file_column and RMagick to upload images and associate them
with my database records, what should I do to find out the exact
dimensions of a given image?

I can use the url_for_file_column command to link to them, but I’m not
sure how to find out the image’s height and width in my program.

Thanks!
Jeff

If I’m using file_column and RMagick to upload images and associate them
with my database records, what should I do to find out the exact
dimensions of a given image?

I can use the url_for_file_column command to link to them, but I’m not
sure how to find out the image’s height and width in my program.

I don’t remember where I got it, but this works for jpegs… used like:

jpeg = JPEG.new(“some.jpeg”)
puts jpeg.width
puts jpeg.height


class JPEG
attr_reader :width, :height, :bits

def initialize(file)
if file.kind_of? IO
examine(file)
else
File.open(file, ‘rb’) { |io| examine(io) }
end
end

private
def examine(io)
raise ‘malformed JPEG’ unless io.getc == 0xFF && io.getc == 0xD8 #
SOI

 class << io
   def readint; (readchar << 8) + readchar; end
   def readframe; read(readint - 2); end
   def readsof; [readint, readchar, readint, readint, readchar]; end
   def next
     c = readchar while c != 0xFF
     c = readchar while c == 0xFF
     c
   end
 end

 while marker = io.next
   case marker
   when 0xC0..0xC3, 0xC5..0xC7, 0xC9..0xCB, 0xCD..0xCF # SOF markers
     length, @bits, @height, @width, components = io.readsof
     raise 'malformed JPEG' unless length == 8 + components * 3
   when 0xD9, 0xDA:  break # EOI, SOS
   when 0xFE:        @comment = io.readframe # COM
   when 0xE1:        io.readframe # APP1, contains EXIF tag
   else              io.readframe # ignore frame
   end
 end

end
end

Thanks. I’m pretty certain RMagick actually has that stuff built into
it, I’m just not sure how to access it from inside my Rails application.

Jeff

Philip H. wrote:

I don’t remember where I got it, but this works for jpegs… used like:

jpeg = JPEG.new(“some.jpeg”)
puts jpeg.width
puts jpeg.height

Thanks. I’m pretty certain RMagick actually has that stuff built into
it, I’m just not sure how to access it from inside my Rails application.

Me too, but the less I can use imagemagick the happier I am :slight_smile: I was
really close to copying the source from php’s getimagesize function
which
can do gif, png, and bitmap as well, but found this which was enough for
me…

Yes, you can get it from RMagick, I believe its the image.cols and
image.rows to obtain the width and height.

If you want to avoid imageMagick, take a look at the commandline tool
jhead, which will give you that info easily.

http://www.sentex.net/~mwandel/jhead/

John.

Jeff C.man wrote:

Thanks. I’m pretty certain RMagick actually has that stuff built into
it, I’m just not sure how to access it from inside my Rails application.

Jeff

Philip H. wrote:

I don’t remember where I got it, but this works for jpegs… used like:

jpeg = JPEG.new(“some.jpeg”)
puts jpeg.width
puts jpeg.height

What I’m trying to do is get access to this information from within my
Rails program.

Assuming I have a model such as User, with an “image” field, what I’m
trying to do is get access to dimensions of the User’s image.

I know how to do url_for_file_column(‘user’, ‘image’), but how would I
get access to the dimensions, how would I create the RMagick/ImageMagick
object to call on the “cols” and “rows” methods?

I’m surprised this doesn’t seem to have come up for any others, or
perhaps I’m just missing something. I don’t think a command-line tool
is quite what I’m looking for, because I want my controller and view to
have access to these numbers.

Assuming that I have a User object with an “image” field created by the
file_column plugin, does anyone know exactly how I could access the
RMagick object for that image?

Jeff

Guest wrote:

Yes, you can get it from RMagick, I believe its the image.cols and
image.rows to obtain the width and height.

If you want to avoid imageMagick, take a look at the commandline tool
jhead, which will give you that info easily.

This is not a very helpful answer to your problem, but I looked at
file_column and acts_as_attachment. I found the latter to be nicer for
working with images. I am able to access image geometry with
acts_as_attachment.

Guest wrote:

Yes, you can get it from RMagick, I believe its the image.cols and
image.rows to obtain the width and height.

It is actually image.columns and image.rows