End*_*ins 4 ruby-on-rails rmagick amazon-s3 paperclip
我一直在尝试使用paperclip将文件(RMagick处理的输出)上传到s3.我一直在收到错误
找不到http://upload.wikimedia.org/wikipedia/commons/thumb/4/48/EBay_logo.png/800px-EBay_logo.png= > 800px-EBay_logo.png的处理程序 PNG 800x349 800x349 + 0 + 0 DirectClass 8-位37kb
基本上,在我的系统中 - 用户使用外部URL上传徽标,我处理徽标并修剪空白并上传到我的s3系统.我一直在使用临时文件作为中间人,但我想直接这样做:
在我的模型中,我这样做:
def fetch_and_trim_logo
puts "logo is #{logo}"
if logo_changed?
response = RestClient.get logo
if response.code == 200
img = Magick::Image::read(logo)[0]
puts "This image is #{img.columns}x#{img.rows} pixels"
trimmed_img = img.trim
puts "Trimmed image is #{trimmed_img.columns}x#{trimmed_img.rows} pixels"
temp_file = Tempfile.new(["trimmed_image",".png"])
trimmed_img.write("png:" + temp_file.path)
my_asset = visitor.user.my_assets.new
my_asset.uploaded_file = temp_file
my_asset.save
end
end
end
Run Code Online (Sandbox Code Playgroud)
我的My_asset模型具有所有回形针设置,如下所示:
Class MyAsset<ActiveRecord::Base
---
---
has_attached_file :uploaded_file
attr_accessible :uploaded_file
validates_attachment_size :uploaded_file, :less_than=>5.megabyte
has_attached_file :picture,
:styles => { :thumb => "300x300#" },
:default_url => "//#{ENV['CLOUDFRONT_URL']}/assets/picture_missing.png"
end
Run Code Online (Sandbox Code Playgroud)
这种方法有效!但是当我改变的时候
my_asset = visitor.user.my_assets.new
my_asset.uploaded_file = temp_file
my_asset.save
Run Code Online (Sandbox Code Playgroud)
至
my_asset = visitor.user.my_assets.new
my_asset.uploaded_file = trimmed_img
my_asset.save
Run Code Online (Sandbox Code Playgroud)
其中trimmed_img是RMagick处理的输出,我收到错误"No Handler found"
任何想法如何解决这一问题?
好的,解决方案是将Magic图像对象更改为File对象,然后再将其上传到Paperlip
所以,
processed_image = StringIO.open(trimmed_img.to_blob)
Run Code Online (Sandbox Code Playgroud)
然后直接用回形针上传处理过的图像
| 归档时间: |
|
| 查看次数: |
819 次 |
| 最近记录: |