# FlexImage pre-processing avec beer in brazil

**URL:** <https://www.ruby-forum.com/t/fleximage-pre-processing-avec-beer-in-brazil/132738>\
**Category:** Rails\
**Created:** [March 24, 2008, 9:02am UTC](https://www.ruby-forum.com/t/fleximage-pre-processing-avec-beer-in-brazil/132738 "2008-03-24T09:02:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gattone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/898d66/32.png) [@gattone](https://www.ruby-forum.com/u/gattone)\
**Post date:** [March 24, 2008, 9:02am UTC](https://www.ruby-forum.com/t/fleximage-pre-processing-avec-beer-in-brazil/132738/1 "2008-03-24T09:02:42Z")

</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  
def fooify!  
img = rmagick\_image

```
# retrieve image's width and height

# compare both values to determine if the image is "portrait" or

```

“landscap” (if width \> height, aspect\_ratio = landscape, etc…)

```
# if image is "portrait", 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.

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:** ![gattone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/898d66/32.png) [@gattone](https://www.ruby-forum.com/u/gattone)\
**Post date:** [March 24, 2008, 2:22pm UTC](https://www.ruby-forum.com/t/fleximage-pre-processing-avec-beer-in-brazil/132738/2 "2008-03-24T14:22:30Z")

</div>

On Mar 24, 4:02 am, Daniel D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi everyone…

[snip]

> height, decreasing the width proportionally.
> 
> 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:** ![gattone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/898d66/32.png) [@gattone](https://www.ruby-forum.com/u/gattone)\
**Post date:** [March 26, 2008, 7:29am UTC](https://www.ruby-forum.com/t/fleximage-pre-processing-avec-beer-in-brazil/132738/3 "2008-03-26T07:29:05Z")

</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:** ![gattone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/898d66/32.png) [@gattone](https://www.ruby-forum.com/u/gattone)\
**Post date:** [March 27, 2008, 12:05pm UTC](https://www.ruby-forum.com/t/fleximage-pre-processing-avec-beer-in-brazil/132738/4 "2008-03-27T12:05:36Z")

</div>

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