开发者

paperclip - converting tiff images into jpg or png

I'm using paperclip to allow user to upload images and I want to convert an image into jpg/png only if it's a tiff image.

I am using the following code in my image.rb:

validates_attachment_presence :data
  validates_attachment_content_type :data,
  :content_type => ['image/jpeg', 'image/pjpeg',
                                   'image/jpg', 'image/png', 'image/tif', 'image/gif'], :message => "has to be in a proper format"

I would like to know if it is possible to convert tiff开发者_运维百科 images into jpg or png when uploading them.

Thanks a lot


Finally found a solution. Thought it might be helpful to somebody else.

In the image.rb, enter the following lines of code:

 :styles => {
    :thumb => ["150x172#",:jpg],
    :large => ["100%", :jpg]
  }

This will create 2 additional folders in the main image folder, thumb and large. If you want to display the images (which were tiff originally), simply display the jpeg version found in the 'large' folder.

Note that the original tiff images will still be stored in the 'original' folder under the main image folder.

Cheers


Tanya your solution is good. However if you don't care about the file type, you can store all images as jpg by passing the :original option as well. Example:

:styles => {
....
  :original => ["100%", :jpg]
}

That will help ensure nowhere where the file is called will you run into issues.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜