Manipulation d'images

Image Manipulation is an art of transforming an image in a way you desire it to be exhibited as, rather than what the original image exhibits.

In applications related to rails manipulation has to be done while uploading the image as per the requisites of the application like back-ground, color and size, which would give a unique look to the applications.

We can achieve this by using ImageMagick. 

  • ImageMagick is a software suite to create, edit, compose, or convert bitmap images.
  • We can use ImageMagick to re-size, flip, mirror, rotate, distort, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves.

Features of ImageMagick : 

  • Format conversion: convert an image from one format to another (e.g. PNG to JPEG).
  • Transform: re-size, rotate, crop, flip or trim an image.
  • Transparency: render portions of an image invisible.
  • Draw: add shapes or text to an image.
  • Decorate: add a border or frame to an image.
  • Special effects: blur, sharpen, threshold, or tint an image.
  • Animation: create a GIF animation sequence from a group of images.
  • Text & comments: insert descriptive or artistic text in an image.
  • Image identification: describe the format and attributes of an image.
  • Composite: overlap one image over another.
  • Montage: juxtapose image thumbnails on an image canvas.

Add gem rmagick for ruby or rmagick4j for jruby in your gem file.

Using paperclip convert_options we can give options for background-color, border-color, quality, re-size, shadow etc.,

Image Crop :

Is implemented using paperclip et jcrop

We can directly call jcrop using id/class. Then we can get the new height and width.

$(function() {

$(‘#cropbox’).Jcrop();

});

For default crop size

$(function() {

$(‘#cropbox’).Jcrop({

onChange: update_crop,

onSelect: update_crop,

setSelect: [0, 0, 500, 500],

aspectRatio: 1

});

});

For Update Crop Size

function update_crop(coords) {

$(‘#crop_x’).val(coords.x);

$(‘#crop_y’).val(coords.y);

$(‘#crop_w’).val(coords.w);

$(‘#crop_h’).val(coords.h);

}

Référence: http://railscasts.com/episodes/182-cropping-images?view=asciicast

Image Background:

In our model we can add convert_option for the image field and we can set the required color as background.

For example given pink.

:convert_options => {

:all => ‘-background HotPink -alpha Background’

}

Image Dimension Validation:

Using Paperclip::Geometry we can set the image width and height to some value.

def file_dimensions(width = 100, height = 100)

dimensions = Paperclip::Geometry.from_file(photo.queued_for_write[:original].path)

unless dimensions.width < width && dimensions.height < height

errors.add :photo, “Width must be #{width}px and height must be #{height}px”

fin

fin

Référence:http://stackoverflow.com/questions/5454561/rails-paperclip-how-to-check-the-image-dimensions-before-saving

Image Resolution:

We can set image resolution using convert_options → quality option.

Using this option we only can set resolution.

:convert_options => {

:all => ‘-quality 75’

}

Here quality can be set in range of 0-100.

Référence:

http://www.imagemagick.org/script/command-line-options.php

http://www.imagemagick.org/script/convert.php

Conclusive Note:

The Image Manipulation is hence helpful in transforming the images as per the requirements of the user and hence making them feel their applications or images are even more attractive.

This allows to crop, rotate, re-size the images as per our requirements and has a wide range of applicability in these days.

En savoir plus :

With ingenious solutions, progressive business values, and proven track record, RailsCarma est le mieux adapté pour vous aider avec tous vos besoins de développement. Notre processus de développement repose en grande partie sur une attention constante aux détails, notamment la qualité, le design et l’excellence. Nous transformons vos idées en réalité. Connectez-vous à nous via notre Contactez-nous page.

Manasa Heggere
 
Développeur senior Ruby on Rails

Abonnez-vous pour les dernières mises à jour

Articles Similaires

Laissez un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *

fr_FRFrench