diff --git a/app/models/user.rb b/app/models/user.rb index 66c75441..05048789 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -40,8 +40,9 @@ class User < ActiveRecord::Base # validates :website, format: { with: WEBSITE_REGEX } - has_attached_file :profile_picture, styles: { medium: "256x256>", thumb: "80x80>" }, - default_url: "/images/:style/no_avatar.png", use_timestamp: false + has_attached_file :profile_picture, styles: { large: "500x500#", medium: "256x256#", small: "80x80#" }, + default_url: "/images/:style/no_avatar.png", use_timestamp: false, + processors: [:cropper] validates_attachment_content_type :profile_picture, :content_type => /\Aimage\/.*\Z/ process_in_background :profile_picture @@ -155,4 +156,8 @@ class User < ActiveRecord::Base def report_comment(report, content) ModerationComment.create!(user: self, report: report, content: content) end + + def cropping? + !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank? + end end diff --git a/lib/paperclip_processors/cropper.rb b/lib/paperclip_processors/cropper.rb new file mode 100644 index 00000000..29cf6771 --- /dev/null +++ b/lib/paperclip_processors/cropper.rb @@ -0,0 +1,22 @@ +module Paperclip + class Cropper < Thumbnail + def transformation_command + if crop_command + x = super + i = x.index '-crop' + 2.times { x.delete_at i } if i + x + else + super + end + end + + def crop_command + target = @attachment.instance + if target.cropping? + ['-crop', "'#{target.crop_w.to_i}x#{target.crop_h.to_i}+#{target.crop_x.to_i}+#{target.crop_y.to_i}'"] + end + end + end +end +