Need help with Rails paperclip gem

169 views
Skip to first unread message

Nilesh

unread,
Mar 15, 2016, 4:28:36 AM3/15/16
to Ruby on Rails: Talk

Currently I am working on a ROR code which uses rails paperclip gem.


My requirement is, to process images as per its file extension.


Case 1: If image type is “svg”, then do not process original image(as ImageMagick breaks on resizing svg images). Upload original image, along with large, medium, grayscale, grayscale_small versions. However all files will be similar to original file, with different file names. Now I am able to upload only original.svg file. However other versions are not created correctly, and I did not find any solution so far.


Case 2: If it is not a “svg” image, then process image as per given style. This is working as expected.


Current code looks like:

has_mongoid_attached_file( :image, {
:styles          => lambda { |img_url| img_url.instance.image_content_type != "image/svg+xml" ? {
      :large           => "x248",
      :medium     => "x64",
      :grayscale    => { :processors => [:grayscale], :size => "82x82" },
      :grayscale_small => { :processors => [:grayscale], :size => "48x48" }
      } : 
{ 
 :large => {write-a-code-to-upload-file-same-as-original-file-with-name-large}, 
:medium => {write-a-code-to-upload-file-same-as-original-file-with-name-medium} 
:grayscale => {write-a-code-to-upload-file-same-as-original-file-with-name-grayscale}  
:grayscale_small => {write-a-code-to-upload-file-same-as-original-file-with-name-grayscal_small} 
}
}
}

Gem versions:

mongoid: 3.0.15
paperclip: 2.3.11
mongoid-paperclip: 0.0.8

Please let me know if you have worked on similar requirement. Any pointers will be highly appreciated.

Colin Law

unread,
Mar 15, 2016, 4:38:28 AM3/15/16
to Ruby on Rails: Talk
On 15 March 2016 at 08:28, Nilesh <navale...@gmail.com> wrote:
> Currently I am working on a ROR code which uses rails paperclip gem.
>
>
> My requirement is, to process images as per its file extension.
>
>
> Case 1: If image type is “svg”, then do not process original image(as
> ImageMagick breaks on resizing svg images).

Are you sure there is still a problem resizing svg? I thought that was
fixed some time ago, though I have not done it myself.

Colin

Nilesh

unread,
Mar 15, 2016, 5:22:18 AM3/15/16
to Ruby on Rails: Talk
Hi Colin,

Yes, there is still a problem with ruby gem versions I am using. Using above resize :styles code, produced SVG images are invalid xml, and do not open in browser. So I want to upload as it is.

Thanks.

Colin Law

unread,
Mar 15, 2016, 5:30:27 AM3/15/16
to Ruby on Rails: Talk
On 15 March 2016 at 09:22, Nilesh <navale...@gmail.com> wrote:
> Hi Colin,
>
> Yes, there is still a problem with ruby gem versions I am using. Using above
> resize :styles code, produced SVG images are invalid xml, and do not open in
> browser. So I want to upload as it is.

Which version are you using?

Colin

>
> Thanks.
>
> On Tuesday, March 15, 2016 at 2:08:28 PM UTC+5:30, Colin Law wrote:
>>
>> On 15 March 2016 at 08:28, Nilesh <navale...@gmail.com> wrote:
>> > Currently I am working on a ROR code which uses rails paperclip gem.
>> >
>> >
>> > My requirement is, to process images as per its file extension.
>> >
>> >
>> > Case 1: If image type is “svg”, then do not process original image(as
>> > ImageMagick breaks on resizing svg images).
>>
>> Are you sure there is still a problem resizing svg? I thought that was
>> fixed some time ago, though I have not done it myself.
>>
>> Colin
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/4ea9a2c5-585d-493f-9345-53dd9b372479%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Nilesh

unread,
Mar 15, 2016, 5:52:40 AM3/15/16
to Ruby on Rails: Talk

$ bundle exec rails -v

Rails 3.1.3


$ ruby -v

ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-darwin14.4.0]


Gem versions:

mongoid: 3.0.15
paperclip: 2.3.11
mongoid-paperclip: 0.0.8





Nilesh

unread,
Mar 15, 2016, 5:57:28 AM3/15/16
to Ruby on Rails: Talk

$ identify

Version: ImageMagick 6.9.1-10 Q16 x86_64 2016-02-29 http://www.imagemagick.org

Walter Lee Davis

unread,
Mar 15, 2016, 7:39:51 AM3/15/16
to rubyonra...@googlegroups.com

> On Mar 15, 2016, at 4:28 AM, Nilesh <navale...@gmail.com> wrote:
>
> Currently I am working on a ROR code which uses rails paperclip gem.
>
>
>
> My requirement is, to process images as per its file extension.

Here's how I solved this for a document management system that needed transcoding for video, resizing for images, and nothing for every other file type (zip, powerpoint, etc.).

has_attached_file :blob,
:preserve_files => true,
:styles => Proc.new { |a| a.instance.file_styles(a.instance) },
:processors => Proc.new { |a| a.processors(a) }

def file_styles(a)
if a.video?
return {
:thumb => { :geometry => "320x320>", :format => 'jpg', :time => 1 },
:poster => { :geometry => "1280x720>", :format => 'jpg', :time => 1 },
:mp4 => { :geometry => "1280x720>", :format => :mp4, :streaming => true }
}
end
if a.photo?
return { :thumb => ["320x320>", :png], :large => ["1500x1500>", :png] }
end
if a.audio?
return { :wav => ['audio', :wav], :mp3 => ['audio', :mp3] }
end
{} # if you get here, then there are no styles needed, just the original
end

def processors(a)
if a.video?
return [:ffmpeg, :qtfaststart]
end
if a.photo?
return [ :my_thumbnail ]
end
if a.audio?
return [ :audio ]
end
[] # if you get here, then no processing needed
end

Hope this helps,

Walter
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/97af62aa-072f-4e0a-a984-992bf3a35fc6%40googlegroups.com.

Walter Lee Davis

unread,
Mar 15, 2016, 7:44:22 AM3/15/16
to rubyonra...@googlegroups.com

> On Mar 15, 2016, at 7:38 AM, Walter Lee Davis <wa...@wdstudio.com> wrote:
>
>>
>> On Mar 15, 2016, at 4:28 AM, Nilesh <navale...@gmail.com> wrote:
>>
>> Currently I am working on a ROR code which uses rails paperclip gem.
>>
>>
>>
>> My requirement is, to process images as per its file extension.
>
> Here's how I solved this for a document management system that needed transcoding for video, resizing for images, and nothing for every other file type (zip, powerpoint, etc.).

Forgot to mention -- the video?, photo?, and audio? methods were just little helpers I wrote on the model that looked at the mime-type of the file under review:

def audio?
['audio/mpeg', 'audio/x-wav', 'audio/x-aiff', 'audio/ogg'].include?(blob_content_type.to_s.downcase)

In case you wondered...

Walter
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/961E5979-0B53-40F3-B6ED-3DE833380D6A%40wdstudio.com.

Colin Law

unread,
Mar 15, 2016, 7:55:57 AM3/15/16
to Ruby on Rails: Talk
On 15 March 2016 at 09:52, Nilesh <navale...@gmail.com> wrote:
> $ bundle exec rails -v
>
> Rails 3.1.3

Did you realise that was superseded in Feb 2012 and there have been
critical security fixes since then? That version is not suitable for
use. If this is a new app then start again with Rails version 4, if
an existing one then at least upgrade to the latest 3.1 immediately,
and then migrate to 4 when you can.

>
>
> $ ruby -v
>
> ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-darwin14.4.0]
>
>
> Gem versions:
>
> mongoid: 3.0.15
> paperclip: 2.3.11

That is an even earlier version dating from early 2011. Stop wasting
time trying to make it work and upgrade to latest rails and gems.
> https://groups.google.com/d/msgid/rubyonrails-talk/34c134c8-22e7-4a9b-a02a-00154d032b4d%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages