Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 524
tanya Avatar asked Sep 29 '11 06:09

tanya


2 Answers

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

like image 170
tanya Avatar answered Oct 13 '22 23:10

tanya


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.

like image 22
Verdi Erel Ergün Avatar answered Oct 13 '22 23:10

Verdi Erel Ergün