画像の加工

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.
  • 透明性: 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 そして 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);

}

参照: 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”

終わり

終わり

参照: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.

参照:

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

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

結論的なメモ:

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.

続きを読む :

With ingenious solutions, progressive business values, and proven track record, レールカーマ 開発のあらゆるニーズを支援するのに最適です。当社の開発プロセスは、品質、デザイン、卓越性など、細部へのたゆまぬこだわりに基づいて確立されています。私たちはあなたのアイデアを現実に変えます。私たちを通じて私たちとつながりましょう お問い合わせ ページ。

マナサ・ヘゲレ
 
Ruby on Rails シニア開発者

最新のアップデートを購読する

関連記事

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

jaJapanese