rails3+ paperclip图片加水印

1. 新建项目,并移除index.html
 
  rails new paperclip-water
  rm public/index.html
 


2. 修改Gemfile 添加paperclip
  
 gem 'paperclip'


3. 新建asset
    r
ails g scaffold asset ttile:string


4. 添加paperclip支持
   
rails g paperclip asset data


5. 新建 lib/paperclip_processors/watermark.rb,源文件在:
    http://gist.github.com/raw/465088/3de6e4f50ff659b91ee094764af842968d4e0de3/watermark.rb

6. 修改asset.rb
 
require 'paperclip_processors/watermark'
class Asset < ActiveRecord::Base
  has_attached_file :data, :processors => [:watermark],
  :styles => {
    :original => {
      :geometry => '550x400>',
      :watermark_path => "#{Rails.root}/public/images/rails.png",#水印图片所在位置
      :position => 'Center' #添加的水印在图片哪个位置
    }
  }
end


7. 修改 _form.html.erb,添加文件上传支持,并修改 show.html.erb,显示图片
  
<%= form_for(@asset,:html => {:multipart => true}) do |f| %>

  #show.html.erb
   <p>
  <%= @asset.title %>
</p>
<p>
  <%=image_tag @asset.data.url() %>
</p>

   
8.  运行迁移文件
    
rake db:migrate


9.  修改routes.rb
   
      root :to => "assets#index"
      resources :assets
   


10. 启动项目 测试
   
    rails s
   


-------
继续打标记:
转载注明: javaeye-- doabit..
----------------------------

你可能感兴趣的:(html,OS,Ruby,ActiveRecord,Rails)