开发者

Rails paperclip change file

开发者 https://www.devze.com 2023-04-12 04:24 出处:网络
I used rmagick in Rails to convert images I upload from one file type to JPEG. I can make it call from the new image now; I did:

I used rmagick in Rails to convert images I upload from one file type to JPEG. I can make it call from the new image now; I did:

image = Magick::ImageList.new 'public/system/photos/' + @picture.id.to_s +
                  开发者_Python百科            '/original/' + @picture.photo_file_name

image.write 'public/system/photos/' + @picture.id.to_s + '/original/' + 
            @picture.photo_file_name.sub(/\.\w*/, '.jpg')

@picture.photo_file_name = @picture.photo_file_name.sub /\.\w*/,'.jpg'

Now I have created two files, how should delete the original file, or overwrite the original file rather than create a new one like I am now?


Re: the discussion in the comments, here's an example from the Paperclip docs:

class User < ActiveRecord::Base
  has_attached_file :photo,
                    :styles => {
                      :small => {
                        :geometry => '38x38#',
                        :quality => 40,
                        :format => 'JPG'
                      },
                      :medium => {
                        :geometry => '92x92#',
                        :quality => 50
                      }
end

Note the ":format => 'JPG'" line. As you can see it's trivial to tell Paperclip to convert the file to JPEG while it's going about the rest of the business, so if you're using Paperclip already you don't need to do a separate conversion step with rmagick directly.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号