裁剪图像后,S3 不会存储所有图像尺寸
当我创建一个带有图像的对象时,我使用回形针 + S3 存储图像
has_attached_file :image,
:whiny => false,
:styles => { :large => "550x340>",
:medium => "165x165>",
:small => "100x100>",
:thumbnail => "55x55>"},
:processors => [:cropper],
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "/:id/:style",
:bucket => "XXX"
,一切都很顺利(4 个不同尺寸的图像副本存储在我的 S3 存储桶中)
当我使用 JCrop 裁剪图像时,问题就出现了,S3 存储了 4 个副本,但是裁剪后的图像具有相同的尺寸,实际上是大尺寸。
我的控制器.rb:
def update
@deal = Deal.find(params[:id])
respond_to do |format|
if @deal.update_attributes(params[:deal])
format.html { redirect_to(@deal, :notice => 'Deal was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @deal.errors, :status => :unprocessable_entity }
end
end
I storing images using paperclip + S3
has_attached_file :image,
:whiny => false,
:styles => { :large => "550x340>",
:medium => "165x165>",
:small => "100x100>",
:thumbnail => "55x55>"},
:processors => [:cropper],
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "/:id/:style",
:bucket => "XXX"
when I create an object with image every thing goes fine (4 copies of the image with 4 different sizes stored in my S3 Bucket )
The problem comes when I crop the image using JCrop, S3 store 4 copies but with the same size for the cropped image actually the large size.
My controller.rb:
def update
@deal = Deal.find(params[:id])
respond_to do |format|
if @deal.update_attributes(params[:deal])
format.html { redirect_to(@deal, :notice => 'Deal was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @deal.errors, :status => :unprocessable_entity }
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它的工作原理如下:
现在,我能想到的最佳方法是使用新文件名上传裁剪后的图像。这样,当 CDN 检查其缓存时,它不会在那里找到该文件,而是向 s3 请求该文件。您可以使用delayed_job或resque在后台删除旧文件。
希望它能澄清事情。
This is how it works:
Now, most optimal way I can think of is to upload the cropped image with a new file name. So that when CDN checks its cache, it doesn't find it there and asks s3 for the file. You can delete the old file in background using delayed_job or resque.
Hope, it clarifies things.