`
jsntghf
  • 浏览: 2482533 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

给你的图片加上水印

阅读更多

在此,描述三种比较简单的水印效果。更多效果,请参考RMagick的API http://www.imagemagick.org/RMagick/doc/image3.html

 

效果一:给图片加上中文水印以及浮雕效果

  def index
    img = Magick::Image.read("#{File.expand_path(RAILS_ROOT)}/public/images/source.jpg").first    #图片路径
    text = "高海峰"
    copyright = Magick::Draw.new
    copyright.annotate(img, 0, 0, 8, 12, text) do  #可以设置文字的位置,参数分别为路径、宽度、高度、横坐标、纵坐标
      self.gravity = Magick::CenterGravity
      #加上此句才能将中文印到图片上,可以在windows中的C:\WINDOWS\Fonts目录下找到字体文件SimSun.ttc
      self.font = "#{File.expand_path(RAILS_ROOT)}/public/images/simsun.ttc"
      self.font_weight = Magick::BoldWeight      #粗体
      self.pointsize = 14                        #字体的大小
      self.fill = '#FFF'                         #字体的颜色
      self.gravity = Magick::SouthEastGravity
      self.stroke = "none"
    end
    img = img.raise  #浮雕效果
    img.write("#{File.expand_path(RAILS_ROOT)}/public/images/target.jpg")   #生成图片的路径
  end 

效果图如下

 

效果二:图片叠加以及中文水印和浮雕效果

  require 'rubygems'
  require 'RMagick'

  def index    
    img = Magick::Image.read("#{File.expand_path(RAILS_ROOT)}/public/images/source.jpg").first
    img2 = Magick::Image.read("#{File.expand_path(RAILS_ROOT)}/public/images/go.gif").first #版权图片
    text = "高海峰"
    img.composite!(img2, 10, 10, Magick::CopyCompositeOp) #图片叠加
    #CopyCompositeOp是composite的算法之一。还有很多运算方法,实现各种效果,
    #可以在官网找到 http://www.imagemagick.org/RMagick/doc/constants.html#CompositeOperator
    copyright = Magick::Draw.new
    copyright.annotate(img, 0, 0, 8, 12, text) do
      self.gravity = Magick::CenterGravity
      self.font = "#{File.expand_path(RAILS_ROOT)}/public/images/simsun.ttc"
      self.pointsize = 14
      self.font_weight = Magick::BoldWeight
      self.fill = '#FFF'
      self.gravity = Magick::SouthEastGravity
      self.stroke = "none"
    end
    img = img.raise
    img = img.watermark(img, 0.15, 0, Magick::EastGravity)  #0.15是透明度 0是饱和度
    img.write("#{File.expand_path(RAILS_ROOT)}/public/images/target.jpg")
  end

 效果图如下

 

效果三:图片翻转效果

  require 'rubygems'
  require 'RMagick'

  def index    
    img = Magick::Image.read("#{File.expand_path(RAILS_ROOT)}/public/images/source.jpg").first
    img2 = Magick::Image.read("#{File.expand_path(RAILS_ROOT)}/public/images/go.gif").first
    text = "高海峰"
    img.composite!(img2, 10, 10, Magick::CopyCompositeOp)
    copyright = Magick::Draw.new
    copyright.annotate(img, 0, 0, 8, 12, text) do
      self.gravity = Magick::CenterGravity
      self.font = "#{File.expand_path(RAILS_ROOT)}/public/images/simsun.ttc"
      self.pointsize = 14
      self.font_weight = Magick::BoldWeight
      self.fill = '#FFF'
      self.gravity = Magick::SouthEastGravity
      self.stroke = "none"
    end
    img = img.raise
    mark = img.rotate!(45)  #可旋转
    img.roll(img.columns/4, img.rows/4)
    img = img.watermark(mark, 0.15, 0, Magick::EastGravity)
    img.write("#{File.expand_path(RAILS_ROOT)}/public/images/target.jpg")
  end

效果图如下

2
1
分享到:
评论
1 楼 机器人 2009-11-03  
高海锋不错嘛。

相关推荐

Global site tag (gtag.js) - Google Analytics