2010-04-26 10 views

Répondre

1

En général, j'ai eu si mauvaise chance avec RMagick que je trouve généralement plus facile de faire juste un système() appeler avec la commande en elle pour modifier les images . Si vous avez adopté cette approche, vous pouvez utiliser exactement la commande dans le lien que vous avez référencé.

9

Inclure rmagick dans votre code source. Assurez-vous de placer l'inclusion dans la déclaration de classe.

require 'rmagick' 
include Magick 

Créer une méthode, comme celui-ci

def thumb(source_image, geometry_string, radius = 10) 
    source_image.change_geometry(geometry_string) do |cols, rows, img| 

    # Make a resized copy of the image 
    thumb = img.resize(cols, rows) 

    # Set a transparent background: pixels that are transparent will be 
    # discarded from the source image. 
    mask = Image.new(cols, rows) {self.background_color = 'transparent'} 

    # Create a white rectangle with rounded corners. This will become the 
    # mask for the area you want to retain in the original image. 
    Draw.new.stroke('none').stroke_width(0).fill('white'). 
     roundrectangle(0, 0, cols, rows, radius, radius). 
     draw(mask) 

    # Apply the mask and write it out 
    thumb.composite!(mask, 0, 0, Magick::CopyOpacityCompositeOp) 
    thumb 
    end 
end 

Appelez la méthode comme celui-ci

source_image = Image.read('my-big-image.jpg').first 
thumbnail_image = thumb(source_image, '64x64>', 8) 
thumbnail_image.write('thumb.png') 

Je structuré de cette façon parce que je l'ai déjà l'image ouverte à d'autres fins au point Je crée la vignette. Il pourrait être plus logique pour vous de mettre les opérations de fichiers directement dans la méthode.

En outre, vous voudrez peut-être regarder comment les chaînes de géométrie fonctionnent http://www.imagemagick.org/RMagick/doc/imusage.html#geometry

4

En utilisant le code de Fitter Man avec CarrierWave::RMagick

Méthode:

def resize_and_round(geometry_string, radius = 10) 
    manipulate! do |original| 
    original.change_geometry(geometry_string) do |cols, rows, img| 

     # Make a resized copy of the image 
     thumb = img.resize(cols, rows) 

     # Set a transparent background: pixels that are transparent will be 
     # discarded from the source image. 
     mask = Magick::Image.new(cols, rows) {self.background_color = 'transparent'} 

     # Create a white rectangle with rounded corners. This will become the 
     # mask for the area you want to retain in the original image. 
     Magick::Draw.new.stroke('none').stroke_width(0).fill('white'). 
      roundrectangle(0, 0, cols, rows, radius, radius). 
      draw(mask) 

     # Apply the mask and write it out 
     thumb.composite!(mask, 4,4, Magick::CopyOpacityCompositeOp) 
     thumb 
    end 
    end 
end 

Utilisation:

process :resize_and_round => ['200x200', 20]