# RE: Rmagick Howto?

**URL:** <https://www.ruby-forum.com/t/re-rmagick-howto/49733>\
**Category:** Rails\
**Created:** [December 8, 2005, 7:03am UTC](https://www.ruby-forum.com/t/re-rmagick-howto/49733 "2005-12-08T07:03:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3d9bf3/32.png) [@tom](https://www.ruby-forum.com/u/tom)\
**Post date:** [December 8, 2005, 7:03am UTC](https://www.ruby-forum.com/t/re-rmagick-howto/49733/1 "2005-12-08T07:03:51Z")

</div>

Here’s some code to take an image and create one that has a maximum  
dimension of 100 pixels:

```
img_big = Magick::Image.from_blob(file_field.read).first
img_smaller = img_big.change_geometry("100x100") { |cols, rows, img|
  img.resize(cols, rows)
}
thumbnail_column_data = img_smaller.to_blob { self.quality = 75 }

```

Remember to always follow up the RMagick calls with:

```
fDisabled = GC.enable
GC.start
GC.disable if fDisabled

```

Otherwise Ruby will soon run out of memory.

---

<div class="post-metadata">

**Author:** ![tom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3d9bf3/32.png) [@tom](https://www.ruby-forum.com/u/tom)\
**Post date:** [December 8, 2005, 6:09pm UTC](https://www.ruby-forum.com/t/re-rmagick-howto/49733/2 "2005-12-08T18:09:00Z")

</div>

I am trying to add a thumbnail to the image upload example in the “Agile  
Web D. with Rails” book.

I added a blob column “thumbnail” to the table.  
Added “require ‘RMagick’” to the bottom of environment.rb

My model looks like:

* * *

class Picture \< ActiveRecord::Base  
belongs\_to :picture\_category  
validates\_format\_of :content\_type, :with =\> /^image/,  
:message =\> “— you can only upload pictures”

def picture=(picture\_field)  
self.name = base\_part\_of(picture\_field.original\_filename)  
self.content\_type = picture\_field.content\_type.chomp  
self.data = picture\_field.read

```
img_big = Magick::Image.from_blob(self.data)
img_smaller = img_big.change_geometry("100x100") { |cols, rows, img|
  img.resize(cols, rows)
}
self.thumbnail = img_smaller.to_blob { self.quality = 75 }

```

end  
def base\_part\_of(file\_name)  
name = File.basename(file\_name)  
name.gsub(/[^\w.\_-]/, ‘’)  
end  
end

* * *

The error I am getting is:  
undefined method `change\_geometry’ for [ JPEG 1024x768 DirectClass 8-bit  
201kb]:Array

I am sure there are probably mutiple mistakes. What am I doing wrong  
(besides error recovery and validation, one step at a time)?

---

<div class="post-metadata">

**Author:** ![tom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3d9bf3/32.png) [@tom](https://www.ruby-forum.com/u/tom)\
**Post date:** [December 8, 2005, 6:33pm UTC](https://www.ruby-forum.com/t/re-rmagick-howto/49733/3 "2005-12-08T18:33:57Z")

</div>

Try this code

```
img = Magick::Image.from_blob(self.content_data).first

img.change_geometry!('140x200') { |cols, rows, i|
  i.resize!(cols, rows)
}

self.thumbnail = img.to_blob

GC.start
```
