From 4c050908aac836c9243faed07e28f3afef643a1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80=20?= =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B5=D0=B2?= Date: Mon, 7 Sep 2015 16:45:44 +0300 Subject: [PATCH 1/4] added only versions and data html attributes --- .gitignore | 2 + README.md | 61 +++++++++++++++++++++++++ lib/carrierwave/crop/helpers.rb | 35 ++++++++++++-- lib/carrierwave/crop/model_additions.rb | 54 +++++++++++++++------- 4 files changed, 131 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index a955cfa..1ad9307 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,5 @@ test/tmp test/version_tmp tmp .DS_Store +*.swp +*.swo diff --git a/README.md b/README.md index d88438e..cba82d2 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,38 @@ CarrierWave extension to crop uploaded images using Jcrop plugin with preview. + +## This is a fork of gem. +### Two changes from main version(PR is sent) + +8. You can set versions where need to crop. + + #Version for ipad + version :ipad_main do + process crop: :image + end + + #Set crop versions for example in _ipad.html.erb partial + <%= f.cropbox :avatar, only: :ipad_main %> + + #Version for android + version :android_main do + process crop: :image + end + + #Set crop versions for example in _ipad.html.erb partial + <%= f.cropbox :avatar, only: :android_main %> + + + #or both + <%= f.cropbox :avatar, only: [:ipad_main, :android_main] %> + +9. If you need to html data attributes you can set data option + + <%= f.cropbox :avatar, data: {width: 1024, height: 768} %> + + #and you can use it in javascript + ## Installation Install the latest stable release: @@ -168,6 +200,35 @@ If there are no versions, and original file is to be cropped directly then call resize_to_limit(100,100) end +8. You can set versions where need to crop. + + #Version for ipad + version :ipad_main do + process crop: :image + end + + #Set crop versions for example in _ipad.html.erb partial + <%= f.cropbox :avatar, only: :ipad_main %> + + #Version for android + version :android_main do + process crop: :image + end + + #Set crop versions for example in _ipad.html.erb partial + <%= f.cropbox :avatar, only: :android_main %> + + + #or both + <%= f.cropbox :avatar, only: [:ipad_main, :android_main] %> + +9. If you need to html data attributes you can set data option + + <%= f.cropbox :avatar, data: {width: 1024, height: 768} %> + + #and you can use it in javascript + + ### Credits and resources * [CarrierWave](https://github.com/carrierwaveuploader/carrierwave) * [Deep Liquid's JCrop](http://deepliquid.com/content/Jcrop.html) diff --git a/lib/carrierwave/crop/helpers.rb b/lib/carrierwave/crop/helpers.rb index 8bd6848..2b276be 100644 --- a/lib/carrierwave/crop/helpers.rb +++ b/lib/carrierwave/crop/helpers.rb @@ -21,7 +21,7 @@ def previewbox(attachment, opts = {}) if(self.object.send(attachment).class.ancestors.include? CarrierWave::Uploader::Base ) ## Fixes Issue #1 : Colons in html id attributes with Namespaced Models - model_name = self.object.class.name.downcase.split("::").last + model_name = self.object.class.name.downcase.split("::").last width, height = 100, 100 if(opts[:width] && opts[:height]) width, height = opts[:width].round, opts[:height].round @@ -40,24 +40,49 @@ def previewbox(attachment, opts = {}) # Form helper to render the actual cropping box of an attachment. # Loads the actual image. Cropbox has no constraints on dimensions, image is renedred at full size. # Box size can be restricted by setting the :width and :height option. If you override one of width/height you must override both. - # By default original image is rendered. Specific version can be specified by passing version option + # By default original image is rendered. Specific version can be specified by passing version option. # # cropbox :avatar # cropbox :avatar, width: 550, height: 600 # cropbox :avatar, version: :medium + # cropbox :avatar, data: {width: 400, height: 600} + # + # Also you can set :only option. It will crop only this version. + # cropbox :avatar, only: :ipad_main + # + # version :ipad_main do + # process crop: :image + # end # # @param attachment [Symbol] attachment name # @param opts [Hash] specify version or width and height options def cropbox(attachment, opts={}) attachment = attachment.to_sym attachment_instance = self.object.send(attachment) + data_options = opts[:data] + if(attachment_instance.class.ancestors.include? CarrierWave::Uploader::Base ) ## Fixes Issue #1 : Colons in html id attributes with Namespaced Models - model_name = self.object.class.name.downcase.split("::").last + model_name = self.object.class.name.downcase.split("::").last hidden_elements = self.hidden_field(:"#{attachment}_crop_x", id: "#{model_name}_#{attachment}_crop_x") + + #only option + if opts[:only].is_a? Array + opts[:only].each_with_index do |item, idx| + hidden_elements << self.hidden_field(:"#{attachment}_only_version", + id: "#{model_name}_#{attachment}_only_version_#{idx}", + value: item, :multiple => true) + end + else + hidden_elements << self.hidden_field(:"#{attachment}_only_version", + id: "#{model_name}_#{attachment}_only_version", + value: opts[:only]) + end if opts[:only].present? + [:crop_y, :crop_w, :crop_h].each do |attribute| - hidden_elements << self.hidden_field(:"#{attachment}_#{attribute}", id: "#{model_name}_#{attachment}_#{attribute}") + hidden_elements << self.hidden_field(:"#{attachment}_#{attribute}", + id: "#{model_name}_#{attachment}_#{attribute}") end box = @template.content_tag(:div, hidden_elements, style: "display:none") @@ -72,7 +97,7 @@ def cropbox(attachment, opts={}) else img = self.object.send(attachment).url end - crop_image = @template.image_tag(img, :id => "#{model_name}_#{attachment}_cropbox") + crop_image = @template.image_tag(img, id: "#{model_name}_#{attachment}_cropbox", data: data_options ) box << @template.content_tag(:div, crop_image, wrapper_attributes) end end diff --git a/lib/carrierwave/crop/model_additions.rb b/lib/carrierwave/crop/model_additions.rb index 541065b..a06759b 100644 --- a/lib/carrierwave/crop/model_additions.rb +++ b/lib/carrierwave/crop/model_additions.rb @@ -12,7 +12,7 @@ module ClassMethods # @param attachment [Symbol] Name of the attachment attribute to be cropped def crop_uploaded(attachment) - [:crop_x, :crop_y, :crop_w, :crop_h].each do |a| + [:crop_x, :crop_y, :crop_w, :crop_h, :only_version].each do |a| attr_accessor :"#{attachment}_#{a}" end after_update :"recreate_#{attachment}_versions" @@ -47,8 +47,15 @@ def method_missing(method, *args) # @param attachment [Symbol] Name of the attribute to be cropped def crop_image(attachment) if cropping?(attachment) + only_version = self.send("#{attachment}_only_version") + only_version = only_version.to_sym if only_version.is_a? String + attachment_instance = send(attachment) - attachment_instance.recreate_versions! + if only_version + attachment_instance.recreate_versions!(only_version) + else + attachment_instance.recreate_versions! + end end end @@ -59,12 +66,12 @@ def crop_image(attachment) module Uploader # Performs cropping. - # + # # On original version of attachment - # process crop: :avatar + # process crop: :avatar # - # Resizes the original image to 600x600 and then performs cropping - # process crop: [:avatar, 600, 600] + # Resizes the original image to 600x600 and then performs cropping + # process crop: [:avatar, 600, 600] # # @param attachment [Symbol] Name of the attachment attribute to be cropped def crop(attachment, width = nil, height = nil) @@ -73,21 +80,21 @@ def crop(attachment, width = nil, height = nil) y = model.send("#{attachment}_crop_y").to_i w = model.send("#{attachment}_crop_w").to_i h = model.send("#{attachment}_crop_h").to_i + only_version = model.send("#{attachment}_only_version") attachment_instance = model.send(attachment) if self.respond_to? "resize_to_limit" - begin - if width && height - resize_to_limit(width, height) - end - manipulate! do |img| - if attachment_instance.kind_of? CarrierWave::RMagick - img.crop!(x, y, w, h) - elsif attachment_instance.kind_of? CarrierWave::MiniMagick - img.crop("#{w}x#{h}+#{x}+#{y}") - img + if only_version + if only_version.is_a? Array + only_version.each do |item| + crop_manipulate(attachment_instance, width, height, x, y, w, h) if item.to_sym == self.version_name.to_sym + end + else + crop_manipulate(attachment_instance, width, height, x, y, w, h) if only_version.to_sym == self.version_name.to_sym end + elsif only_version.nil? + crop_manipulate(attachment_instance, width, height, x, y, w, h) end rescue Exception => e @@ -100,6 +107,21 @@ def crop(attachment, width = nil, height = nil) end end + #manipulate croping + def crop_manipulate(attachment_instance, width, height, x, y, w, h) + if width && height + resize_to_limit(width, height) + end + manipulate! do |img| + if attachment_instance.kind_of? CarrierWave::RMagick + img.crop!(x, y, w, h) + elsif attachment_instance.kind_of? CarrierWave::MiniMagick + img.crop("#{w}x#{h}+#{x}+#{y}") + img + end + end + end + end ## End of Uploader end ## End of Crop end ## End of CarrierWave From 036ae0b367433739662308d20d70f283abbb2e64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80=20?= =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B5=D0=B2?= Date: Mon, 7 Sep 2015 16:47:59 +0300 Subject: [PATCH 2/4] fix readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cba82d2..b1f502a 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ CarrierWave extension to crop uploaded images using Jcrop plugin with preview. process crop: :image end - #Set crop versions for example in _ipad.html.erb partial + #Set crop versions for example in _andtoid.html.erb partial <%= f.cropbox :avatar, only: :android_main %> @@ -215,7 +215,7 @@ If there are no versions, and original file is to be cropped directly then call process crop: :image end - #Set crop versions for example in _ipad.html.erb partial + #Set crop versions for example in _android.html.erb partial <%= f.cropbox :avatar, only: :android_main %> From 74117fd1ee1c1dcc4fb4ea743cda2cf9e20583aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80=20?= =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B5=D0=B2?= Date: Mon, 7 Sep 2015 17:58:08 +0300 Subject: [PATCH 3/4] fix when version is Array --- lib/carrierwave/crop/model_additions.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/carrierwave/crop/model_additions.rb b/lib/carrierwave/crop/model_additions.rb index a06759b..1126348 100644 --- a/lib/carrierwave/crop/model_additions.rb +++ b/lib/carrierwave/crop/model_additions.rb @@ -52,7 +52,12 @@ def crop_image(attachment) attachment_instance = send(attachment) if only_version - attachment_instance.recreate_versions!(only_version) + if only_version.is_a? Array + only_version.map!(&:to_sym) + attachment_instance.recreate_versions!(*only_version) + else + attachment_instance.recreate_versions!(only_version.to_sym) + end else attachment_instance.recreate_versions! end From 01a4fca2a6f41daa43c5905a67bfd6c4ef8f9e55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80=20?= =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B5=D0=B2?= Date: Tue, 8 Sep 2015 13:46:47 +0300 Subject: [PATCH 4/4] little fixe --- lib/carrierwave/crop/model_additions.rb | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/carrierwave/crop/model_additions.rb b/lib/carrierwave/crop/model_additions.rb index 1126348..ba6d455 100644 --- a/lib/carrierwave/crop/model_additions.rb +++ b/lib/carrierwave/crop/model_additions.rb @@ -48,16 +48,13 @@ def method_missing(method, *args) def crop_image(attachment) if cropping?(attachment) only_version = self.send("#{attachment}_only_version") - only_version = only_version.to_sym if only_version.is_a? String + only_version = [only_version] if only_version.is_a? String attachment_instance = send(attachment) - if only_version - if only_version.is_a? Array - only_version.map!(&:to_sym) - attachment_instance.recreate_versions!(*only_version) - else - attachment_instance.recreate_versions!(only_version.to_sym) - end + + if only_version.is_a? Array + only_version.map!(&:to_sym) + attachment_instance.recreate_versions!(*only_version) else attachment_instance.recreate_versions! end