Question

I want to be able to validate the image is exactly a certain with or a certain height, or if it's square.

In the validation block of the model that has_attachment, when I try to access image_size, width, or height, it always comes out as null.

I also asked the question here if you want more details.

Was it helpful?

Solution

Yea, you need to hack a bit in order to get it to work, but not so much. Adapting from attachment_fu's own image processor:

 validate :validate_image_size

  private

  def validate_image_size
    w, h = width, height

    unless w or h
      with_image do |img|
        w, h = img.columns, img.rows
      end
    end

    errors.add(:width, "must less than 250px")  if w > 250
    errors.add(:height, "must less than 250px")  if h > 250
  end
end

OTHER TIPS

You haven't specified what language and system you're working on.

Still, for most web frameworks, I think that the standard way to do this by using image magic. Try the identify function. .

Have you taken a look at mini-magick?

You can git clone it from here:

http://github.com/probablycorey/mini_magick/tree/master

If you need to learn about git, check out these links:

http://git.or.cz/course/svn.html (crash course with git, compared to subversion)

http://github.com/guides/git-screencasts (github screencasts)

It's a ruby wrapper around the imagemagick functions (unsure if attachment_fu uses this internally), but it's absolutely leaps and bounds better than RMagick (RMagick is extremely bloated, lots of memory problems). Anywho, mini-magick will let you do all the things you need and then some. Check out the README listed on the github link above, and it'll give you the rundown on how to use it.

Here's a snippet:

#For resizing an image
image = MiniMagick::Image.from_file("input.jpg")
image.resize "100x100"
image.write("output.jpg")

#For determining properties of an image...
image = MiniMagick::Image.from_file("input.jpg")
image[:width] # will get the width (you can also use :height and :format)

I think you are missing prerequisite gems that should be installed in order to use attachment_fu for resizing the image . I have worked with attachment_fu plugin which is dependent on following gems

  1. rmagick-2.11.0

  2. image_science-1.2.0

Ensure you have installed above gems and make changes to the width and height in the has_attachment then you could see the changes .

Good luck !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top