rails 回形针图像大小调整

Paperclip image resizing in rails

我在我的项目中使用回形针上传图片。我已经覆盖了默认路径 (path: ':class/:id/:attachment/:basename.:extension')。因为我错过了路径名的样式,现在我需要为 image.Now 添加缩略图我已经更改了路径(

    config.paperclip_defaults = {
       styles: { thumb: "40x40" },
       path: ':class/:id/:attachment/:style/:basename.:extension',
       url: ':class/:id/:attachment/:style/:basename.:extension'}

) 风格,我已经在 S3 上传了图片。 当我 运行 以前上传的文件的回形针耙任务时,它显示以下错误

NoMethodError: undefined method `match' for nil:NilClass.

当我尝试通过控制台重新处理方法时,

User Load (1.0ms)  SELECT  "users".* FROM "users" WHERE "users"."deleted_at" IS NULL ORDER BY "users"."id" DESC LIMIT   [["LIMIT", 1]]
[paperclip] copying users/100000/profile_picture/Screen_Shot_2018-03-13_at_3.46.34_PM.png to local file /var/folders/f_/rp805gln2p7_1z0prxsw7www00015k/T/2bfc1b167915d853fafaa0a3ef5d83d220180319-5563-1cqfhvv.png
NoMethodError: undefined method `match' for nil:NilClass

假设您正在使用 rake paperclip:refresh CLASS=User 为已创建的图像创建版本。您可以检查更多选项 here.

但是您当前对模型的更改不允许您这样做,因为您将路径从 path: ':class/:id/:attachment/:basename.:extension' 更改为 path: ':class/:id/:attachment/:style/:basename.:extension'

您已经创建的图像位于类似 abc/123/attachement_name/basename.:extension 的路径中。但是在你改变回形针后在 abc/123/attachement_name/origional/basename.:extension.

中查看它

您可以使用 here 中的 rake 任务。进行最少的更改后,它将解决您的问题。

namespace :paperclip do
  desc "Recreate attachments and save them to new destination"

  task :move_attachments => :environment do

    Model.find_each do |model|
      unless model.image_file_name.blank?
        filename = Rails.root.join('public', 'system', 'images', model.id.to_s, model.image_file_name)

        if File.exists? filename
          #image = File.new filename
          model.remote_image_url = filename
          model.save
          model.image.reprocess! 
          image.close
        end
      end
    end
  end
end

Model 替换为您的型号名称和带有附件名称的图像,一切都会起作用。