# FlexImage pre-processing avec beer in brazil

**URL:** <https://discuss.rubyonrails.org/t/fleximage-pre-processing-avec-beer-in-brazil/26757>\
**Category:** rubyonrails-talk\
**Created:** [March 24, 2008, 8:02am UTC](https://discuss.rubyonrails.org/t/fleximage-pre-processing-avec-beer-in-brazil/26757 "2008-03-24T08:02:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Drehmer](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@Daniel\_Drehmer](https://discuss.rubyonrails.org/u/Daniel_Drehmer)\
**Post date:** [March 24, 2008, 8:02am UTC](https://discuss.rubyonrails.org/t/fleximage-pre-processing-avec-beer-in-brazil/26757/1 "2008-03-24T08:02:09Z")

</div>

Hi everyone...

Since i can't find noob friendly information about rmagick and fleximage on the web, I'm here offering a beer for the one good soul who can help me to get rid of my ignorance.

I've been trying for weeks to make fleximage to work, and now that it does, I'd like to do the following:

class MyImage \< FlexImage::Model &nbsp;&nbsp;def fooify! &nbsp;&nbsp;&nbsp;&nbsp;img = rmagick\_image

&nbsp;&nbsp;&nbsp;&nbsp;# retrieve image's width and height

&nbsp;&nbsp;&nbsp;&nbsp;# compare both values to determine if the image is "portrait" or "landscap" (if width \> height, aspect\_ratio = landscape, etc...)

&nbsp;&nbsp;&nbsp;&nbsp;# if image is "portrait", resizes it using a threshold of 200px height, decreasing the width proportionally. &nbsp;&nbsp;&nbsp;&nbsp;# else, if it's landscape, it resizes to a 400px WIDTH, resizing the height proportionally

&nbsp;&nbsp;&nbsp;&nbsp;# than store the image in a blob or filesystem, and the aspect\_ratio value in the DB

&nbsp;&nbsp;&nbsp;&nbsp;self.rmagick\_image = img &nbsp;&nbsp;&nbsp;&nbsp;self &nbsp;&nbsp;end end

My main point of ignorance is about how to gather information about the image's width and height.

The rest I think I can figure out myself (but since I took a lot of time just to make fleximage work, I'm not so confident and I still apreciate your kindness to share your thoughts on it, specially if you think that creating a method inside the model isn't the best aproach and have better practices in mind)

I would really apreciate some help on this, and if you can do it, I owe you a couple beers (however, you'll have to come to brazil to drink it)

Thank You

---

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/jimc/32/740_2.png) [@jimc](https://discuss.rubyonrails.org/u/jimc)\
**Post date:** [March 24, 2008, 1:21pm UTC](https://discuss.rubyonrails.org/t/fleximage-pre-processing-avec-beer-in-brazil/26757/2 "2008-03-24T13:21:40Z")

</div>

> Hi everyone...

[snip]

> class MyImage \< FlexImage::Model def fooify! img = rmagick\_image
> 
> ```
> \# retrieve image&#39;s width and height
> 
> \# compare both values to determine if the image is &quot;portrait&quot; or
> 
> ```
> 
> "landscap" (if width \> height, aspect\_ratio = landscape, etc...)
> 
> ```
> \# if image is &quot;portrait&quot;, resizes it using a threshold of 200px
> 
> ```
> 
> height, decreasing the width proportionally. # else, if it's landscape, it resizes to a 400px WIDTH, resizing the height proportionally
> 
> ```
> \# than store the image in a blob or filesystem, and the
> 
> ```
> 
> aspect\_ratio value in the DB
> 
> ```
> self\.rmagick\_image = img
> self
> 
> ```
> 
> end end
> 
> My main point of ignorance is about how to gather information about the image's width and height.

width = img.columns height = img.rows aspect = height \> 0 ? width.to\_f / height.to\_f : 0

aspect\_string = aspect \> 1 ? 'landscape' : 'portrait'

For resizing, you'll probably need to calculate the percentage of scale to meet your desired dimensions, and then use the scale! method.

scale\_pct = aspect \> 1 ? 400.0 / width.to\_f : 200.0 / height.to\_f img.scale!(scale\_pct)

Full documentation on rMagick is here:

[http://studio.imagemagick.org/RMagick/doc/](http://studio.imagemagick.org/RMagick/doc/)

The primary thing you'll want to look at in the documentation is in the reference section, under Classes, the four pages about the Image class. There are also some useful tutorials.

Jim Crate Que Viva, LLC

---

<div class="post-metadata">

**Author:** ![Daniel\_Drehmer](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@Daniel\_Drehmer](https://discuss.rubyonrails.org/u/Daniel_Drehmer)\
**Post date:** [March 26, 2008, 6:28am UTC](https://discuss.rubyonrails.org/t/fleximage-pre-processing-avec-beer-in-brazil/26757/3 "2008-03-26T06:28:11Z")

</div>

I couldn't try your code yet, but I'll do it within the next couple days...

But thank you very much for the help! I'm sure this will be very helpful to me and a lot of people.

---

<div class="post-metadata">

**Author:** ![Daniel\_Drehmer](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@Daniel\_Drehmer](https://discuss.rubyonrails.org/u/Daniel_Drehmer)\
**Post date:** [March 27, 2008, 11:04am UTC](https://discuss.rubyonrails.org/t/fleximage-pre-processing-avec-beer-in-brazil/26757/4 "2008-03-27T11:04:47Z")

</div>

The code you've written is beautiful. It worked like a charm. Thank you again!
