পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Saturday, March 11, 2017

Ruby on Rails 2.1 - RMagick Guide

Rails provide a bindings to ImageMagick and GraphicsMagick which are popular and stable C libraries. The RMagick library provides the same interface against ImageMagick and GraphicsMagick, so it does not matter which one you use.

You can get RMagick by installing the rmagick gen on Unix or rmagick-win32 gem on Windows. Let's install it on a Unix machine as follows −
$ gem install rmagick
The RMagick module comes along with class Magick::Image which lets you resize images four different methods −
  • resize(width, height)
  • scale(width, height)
  • sample(width, height)
  • thumbnail(width, height)
All these methods accept a pair integer values, corresponding to the width and height in pixels of the thumbnail you want.

Example

Here's an example that uses resize() method to resize the image. It takes the file tmp.jpg and makes a thumbnail of it 100 pixels wide by 100 pixels tall −
require 'rubygems'
require 'RMagick'

class ImageController < ApplicationController

   def createThubnail
      width, height = 100, 100

      img =  Magick::Image.read('tmp.jpg').first
      thumb = img.resize(width, height)
    
      # If you want to save this image use following
      # thumb.write("mythumbnail.jpg")

      # otherwise send it to the browser as follows
      send_data(thumb.to_blob, :disposition => 'inline', :type => 'image/jpg')
   end
end
Here are the steps to create a thumbnail −
  • Here the class method Image.read receives an image filename as an argument and returns an array of Image objects. You obtain the first element of that array which is obviously our tmp.jpg image.
  • Next we are calling method resize with the desired arguments which is creating a thumbnail.
  • Finally we are directing this image to browser. You can also use method thumb.write("mythumbnail.jpg") to store this image locally are your machine.

Converting Image Formats

This is very easy to convert an image file from one format to another format. The RMagick handles is very smartly. You can just read in the file and write it out with a different extension.

Example

Following example converts a JPEG file into a GIF file −
require 'rubygems'
require 'RMagick'

class ImageController < ApplicationController

   def changeFormat

      img =  Magick::Image.read('tmp.jpg').first
    
      # If you want to save this image use following
      # img.write("mythumbnail.gif")

      # otherwise send it to the browser as follows
      send_data(img.to_blob, :disposition => 'inline', :type => 'image/gif')
   end
end
Similar way you can change format based on your requirement as follows −
img = Magick::Image.read("tmp.png").first
img.write("tmp.jpg")                 # Converts into JPEG
img.write("tmp.gif")                 # Converts into GIF
img.write("JPG:tmp")                 # Converts into JPEG
img.write("GIF:tmp")                 # Converts into GIF

No comments:

Post a Comment