Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "only version(s)" and data html attributes for javascript #16

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ test/tmp
test/version_tmp
tmp
.DS_Store
*.swp
*.swo
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 _andtoid.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:
Expand Down Expand Up @@ -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 _android.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)
Expand Down
35 changes: 30 additions & 5 deletions lib/carrierwave/crop/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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
Expand Down
56 changes: 40 additions & 16 deletions lib/carrierwave/crop/model_additions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -47,8 +47,17 @@ 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] if only_version.is_a? String

attachment_instance = send(attachment)
attachment_instance.recreate_versions!

if only_version.is_a? Array
only_version.map!(&:to_sym)
attachment_instance.recreate_versions!(*only_version)
else
attachment_instance.recreate_versions!
end
end
end

Expand All @@ -59,12 +68,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)
Expand All @@ -73,21 +82,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
Expand All @@ -100,6 +109,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
Expand Down