
[英]Rails paperclip amazon aws s3 gem, how to change image url?
[英]How to crop a temp image with Paperclip and store it in Amazon S3 in Rails 3
我在用回形针做一些非同寻常的技巧时遇到了很大的麻烦。 这是我的情况:我的应用程序用户有一个头像图片,我的想法是让他们通过Jcrop裁剪头像。 我的应用程序托管在Heroku中,因此我必须将图像上传到Amazon S3。 我已经使用了这种流行的railscast来实现裁剪功能,但是它需要处理两倍的图像。 问题是从这里开始的。
我认为,可能的解决方案可能不是第一次(当用户选择图像时)处理图像,而是第二次处理图像。 我已经在控制器中实现了以下代码:
def change_avatar
@user = current_user
paperclip_parameters = params[:user][:avatar] #first time call
if @temp_image_object.present? || params[:avatar].present?
if check_crop_params #second controller call
@user.avatar = File.new(@user.tmp_avatar_path) #overrides the
redirect_to @user, notice: t("messages.update.user.avatar") if @user.save
else #first controller call
@temp_path = @user.generate_temp_image(paperclip_parameters)
@user.tmp_avatar_path = @new_path #store the custom path to retrieve it in the second call
render :action => 'avatar_crop' if @user.save
end
end
end
def check_crop_params
!params[:user][:crop_x].blank? && !params[:user][:crop_y].blank? && !params[:user][:crop_w].blank? && !params[:user][:crop_h ].blank?
end
在我的用户模型中:
#this method copies the original image tempfile when user upload the image to a custom path and returns the custom path
def generate_temp_image(paperclip_parameters)
uploaded_img_path = uploaded_img.tempfile.path
temp_dir = Rails.root.join('public/tmp')
Dir.mkdir(temp_dir) unless Dir.exists?(temp_dir)
FileUtils.cp(uploaded_img.tempfile, temp_dir)
new_path = uploaded_img_path
end
我还有jcrop的自定义处理器,在处理图像时会使用裁剪变量。 当我上传图像(第一个控制器调用)时,change_avatar方法运行良好,但是当我裁剪图像(第二个控制器调用)时,图像没有被裁剪,回形针会创建图像样式文件,但会忽略我的裁剪。
有任何想法吗? 我该做什么?
我忘了一个小细节..在服务器日志中查看时,我意识到回形针过程并未裁剪图像,因此我在自定义处理器中进行了查找,发现它取决于裁剪参数:crop_x,crop_y,crop_w, crop_h。 由于某种原因,此参数未到达处理器,因此图像永远不会被裁剪。 我要做的就是手动将这些参数分配给用户var,它可以正常工作!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.