Commit d1d5f8ca by LI Daobing

add qiniu_image_tag

parent 531a133a
......@@ -43,11 +43,21 @@ for more information on `qiniu_host`, read http://docs.qiniutek.com/v2/sdk/ruby/
```ruby
class Image < ActiveRecord::Base
attr_accessible :file
has_attached_file :file, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :path => ":class/:attachment/:id/:style/:basename.:extension"
has_attached_file :file, :path => ":class/:attachment/:id/:basename.:extension"
validates :file, :attachment_presence => true
end
```
* show image in your view
```erb
<%= qiniu_image_tag @image.file.url, :thumbnail => '300x300>', :quality => 80 %>
or
<%= image_tag qiniu_image_path(@image.file.url, :thumbnail => '300x300>', :quality => 80) %>
```
support options: `thumbnail`, `gravity`, `crop`, `quality`, `rotate`, `format`, `auto_orient`. for more information on these options, check the document: http://docs.qiniutek.com/v3/api/foimg/#fo-imageMogr
## Contributing
1. Fork it
......
require "paperclip-qiniu/version"
require 'paperclip/storage/qiniu'
require 'paperclip/qiniu/action_view_extensions/qiniu_image_path' if defined?(ActionView)
require 'paperclip/qiniu/action_view_extensions/qiniu_image_tag' if defined?(ActionView)
......@@ -3,11 +3,28 @@ module Paperclip
module ActionViewExtensions
module QiniuImagePath
def qiniu_image_path(source, options={})
options = options.clone
thumbnail = options.delete(:thumbnail)
gravity = options.delete(:gravity)
crop = options.delete(:crop)
quality = options.delete(:quality)
rotate = options.delete(:rotate)
format = options.delete(:format)
auto_orient = options.delete(:auto_orient)
res = source
res += "?imageMogr" if thumbnail
res += "?imageMogr"
res += "/thumbnail/#{CGI.escape thumbnail}" if thumbnail
res.html_safe
res += "/gravity/#{CGI.escape gravity}" if gravity
res += "/crop/#{CGI.escape crop}" if crop
res += "/quality/#{CGI.escape quality.to_s}" if quality
res += "/rotate/#{CGI.escape rotate.to_s}" if rotate
res += "/format/#{CGI.escape format.to_s}" if format
res += "/auto-orient" if auto_orient
if res.end_with? '?imageMogr'
source
else
res
end
end
end
end
......
module Paperclip
module Qiniu
module ActionViewExtensions
module QiniuImageTag
def qiniu_image_tag(source, options={})
options.symbolize_keys!
src = path_to_image(source)
options[:src] = qiniu_image_path(src, options)
unless src =~ /^(?:cid|data):/ || src.blank?
options[:alt] = options.fetch(:alt){ image_alt(src) }
end
if size = options.delete(:size)
options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$}
end
if mouseover = options.delete(:mouseover)
options[:onmouseover] = "this.src='#{path_to_image(mouseover)}'"
options[:onmouseout] = "this.src='#{options[:src]}'"
end
tag("img", options)
end
end
end
end
end
ActionView::Base.send :include, Paperclip::Qiniu::ActionViewExtensions::QiniuImageTag
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment