安装rmagick gem
A new release 2.13.2 of RMagick is now available on github as well as rubygems. This release will fix the installation issues due to ruby 1.9.3 and ImageMagick 6.8+. You can install it the usual way with bundle by adding to Gemfile:
or if you want to be on edge:
You also install it directly by doing:
|
RMagick的基本使用
(1)定义主要对象
使用RMagick,重要会用到两个对象:Image和Draw。
- require 'rubygems'
- require 'RMagick'
- img = Magick::Image.read('image/rails.png').first
- width = img.columns
- height = img.rows
- thumb = img.resize(width * 0.8, height * 0.8)
- thumb.write('image/rails_thumb_1.png') {self.quality = 50} #compress压缩大小
-
- require 'rubygems'
- require 'RMagick'
- img = Magick::Image.read('image/rails.png').first #Image.read返回一个数组(因为有时候我们会read一个GIF动画,那么GIF动画的每一帧会存到这个数组里,真是周到)
- width, height = 100, 100
- thumb = img.resize(width, height)
- thumb.write('image/rails_thumb.png')
给图片加水印(中文的)
首先给文件增加必要的gem引入
require 'rubygems'
require 'RMagick'
给图片嵌入中文文字
def t1
img=Magick::Image.read('path\read_image1.jpg').first #图片路径,用相对路径即可,相对于public来说的
my_text="\251 这是黑马的标致"
copyright=Magick::Draw.new
copyright.annotate(img,0,0,3,18,my_text) do #可设字的位置
self.gravity = Magick::CenterGravity
self.font='public\images\simsun.ttc' #这地方必须使用中文字库,才能打中文到图片上。在windows中c:\windows\fonts\simsun.ttc拷到项目的public\images目录下就可以随着项目使用了。另外注意:笔者是使用utf-8字符集来编辑源文件的,如果你不是,请在程序中对汉字转换编码为utf-8
self.pointsize=96 #字体大小
self.font_weight=Magick::BoldWeight
self.fill='red' #字的颜色
self.gravity=Magick::SouthEastGravity
self.stroke = "none"
end
img=img.raise #浮雕效果
img.write('path\img')
end
def test_photo
img=Magick::Image.read('public\photo\big_image\1\1119218437.jpg').first
img2=Magick::Image.read('public\photo\big_image\2\DSCN4991-thumb.jpg').first #版权图片
my_text="\251 黑马的标致"
img.composite!(img2, -0,-0, Magick::CopyCompositeOp) #图片叠加 ,CopyCompositeOp是composite的运算之一,还有很多运算方法,实现各种效果,可以在官网找到 http://www.imagemagick.org/RMagick/doc/constants.html#CompositeOperator
copyright=Magick::Draw.new
copyright.annotate(img,0,0,3,18,my_text) do #可设字的位置
self.gravity = Magick::CenterGravity
self.font='public\images\simsun.ttc'
self.pointsize=96 #字体大小
self.font_weight=Magick::BoldWeight
self.fill='red' #字的颜色
self.gravity=Magick::SouthEastGravity
self.stroke = "none"
end
img=img.raise #浮雕效果
mark.rotate!(-90) #可旋转
img = img.watermark(mark, 0.15, 0, Magick::EastGravity) #0.15是透明度
img.write('public\photo\big_image\1\1119218437-image1_bak.jpg')
end
文字特效
Rows = 60
Cols = 250
Text = 'Ruby rocks!'
anim = Magick::ImageList.new
ex = Magick::Image.new(Cols, Rows)
text = Magick::Draw.new
text.gravity = Magick::CenterGravity
text.pointsize = 36
text.font_weight = Magick::BoldWeight
text.font_style = Magick::ItalicStyle
text.stroke = 'transparent'
text.annotate(ex, 0, 0, 2, 2, Text) {
self.fill = 'gray60'
}
anim << ex.copy
ex = ex.blur_image(0, 3)
anim << ex.copy
text.annotate(ex, 0, 0, -1, -1, Text) {
self.fill = 'maroon'
}
anim << ex.copy
anim.delay = 100
anim.cur_image.delay = 300
anim.iterations = 0
anim.write('shadow.gif')
exit
缩略图
[1] pry(main)> require "RMagick"
# => true
[2] pry(main)> img=Magick::Image.read('pic7.jpg').first
# => pic7.jpg JPEG 600x800 600x800+0+0 DirectClass 8-bit 121kb
[3] pry(main)> c,r=img.columns,img.rows
# => [600, 800]
[4] pry(main)> l=c>r ? c : r
# => 800
[5] pry(main)> f=640.0/l
# => 0.8
[6] pry(main)> t1=img.thumbnail(f)
# => pic7.jpg JPEG 600x800=>480x640 DirectClass 8-bit
[7] pry(main)> t1.write('t1.jpg')
# => pic7.jpg=>t1.jpg JPEG 600x800=>480x640 DirectClass 8-bit 84kb
[8] pry(main)> img=nil
# => nil