From 7aa954e21213cfafd9930b786cdcfb7b04c00ede Mon Sep 17 00:00:00 2001 From: nilsding Date: Mon, 29 Dec 2014 14:51:52 +0100 Subject: [PATCH] added cropper processor --- app/models/user.rb | 9 +++++++-- lib/paperclip_processors/cropper.rb | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 lib/paperclip_processors/cropper.rb 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 +