kirtithorat / carrierwave-crop Goto Github PK
View Code? Open in Web Editor NEWCarrierwave extension to crop uploaded images using Jcrop plugin with preview.
License: MIT License
Carrierwave extension to crop uploaded images using Jcrop plugin with preview.
License: MIT License
Hello!
Would it be possible to generate javascript instead of coffeescript code for the image uploader? I know of several developers that prefer javascript to coffeescript, and I feel being able to provide that option would help make the gem for flexible and easier to use.
Would you consider making this gem compatible with CarrierWave Backgrounder and Delayed::Job?
The gem currently doesn't work with those, because it relies on virtual attributes as defined in crop_uploaded
. These attributes are not persisted and since Delayed Job simply stores/retrieves the object by its ID these values will not be available when the processing happens.
I see two solutions for this:
One is solution is adding additional methods to ActiveRecord to serialize these virtual attributes for Delayed Job. This solution is described here, but doesn't seem particularly elegant.
The other solution is to actually store the crop attributes in the database, rather than using virtual attributes. This way the attributes persist and are available to the Delayed Job worker. The attributes can be set to nil
once processing is completed so cropping?
will only be true if the attributes are set again.
I've been able to make some progress on implementing the latter solution, but I'm still not 100% there yet. For some reason the images are only cropped when a new image is uploaded, but not when the crop attributes are changed.
Any suggestions on how to make it work, or how to debug it?
Right now, Jcrop.gif doesn't get compiled as part of the asset pipeline. I use nginx to serve up static resources, so the request for Jcrop.gif doesn't succeed. The css file should be an erb file that calls asset_path.
I can submit a PR for this if desired.
Hey,
I just wanted to know if you'd be okay with an option to disable the auto-generation of the attr_accessor
s for the X_crop_Y
attributes. My use case needs me to save those, and I'd rather not have some glue code to save them in the model.
Hi,
I have an error on rails 4.2 using the gem carrierwave-crop 0.1.2.
when I try to crop the image an error occurs:
NoMethodError (undefined method `crop' for #LocationShowcaseUploader::Uploader70164076328740:0x007fa0b6842a98)
my code:
class LocationShowcase < ActiveRecord::Base
# other stuff..
mount_uploader :image, LocationShowcaseUploader, mount_on: :image_file_name
crop_uploaded :image
# other stuff..
end
class LocationShowcaseUploader < CarrierWave::Uploader::Base
include CarrierWave::Compatibility::Paperclip
include SharedUploader
include CarrierWave::MiniMagick
include CarrierWave::ImageOptimizer
# other stuff..
version :default do
process resize_to_fit: [600, -1], optimize: [{ quality: 60 }]
end
version :large, if: :is_cropped? do
process crop: :default
process resize_to_fit: [1335, -1], optimize: [{ quality: 60 }]
end
version :medium, if: :is_cropped? do
process crop: :default
process resize_to_fit: [770, -1], optimize: [{ quality: 60 }]
end
version :small, if: :is_cropped? do
process crop: :default
process resize_to_fit: [610, -1], optimize: [{ quality: 60 }]
end
version :admin, if: :is_cropped? do
process crop: :default
process resize_to_fit: [-1, 70], optimize: [{ quality: 60 }]
end
private
def is_cropped?(image)
model.image_crop_x.present?
end
end
As mention here,when you are generating random unique filenames, recreate_versions!
doesn't save the new filename to the database.
So I have to call save! by myself after update_attributes
def update
@model.update(model_update_params)
@model.save!
respond_with @model
end
Is there a better solution?
If I specify version on cropbox and/or previewbox --- the resulted crop is not representative of the area selected. Its not even close. I'm really sorry I dont have any spec to illustrate this...I tried.
Hi everyone,
I just wanted point out that this gem can be used with Mongoid. All you have to do is to add necessary modules in the model; Just the two lines of code noted in following example.
class Profile
include Mongoid::Document
include Mongoid::Timestamps
# Include these modules to use carreirwave-crop
extend CarrierWave::Crop::ModelAdditions::ClassMethods
include CarrierWave::Crop::ModelAdditions::InstanceMethods
# ... rest of the model ...
# you can use carrierwave-crop methods same way as before
mount_uploader :avatar, AvatarUploader
crop_uploaded :avatar
end
Cheers, Farzad.
Hi
I have a problem with your solution: If image is too large it's get resized by jCrop or browser ( didn't get quite well who is doing it ) ( For example when I tried 3000x2000 actually I get something near 1700x1400 ). And as a result cropped area is not as I expected ( it's smaller than you see in preview ).
I have Ad and photo instead of User and avatar, and using MiniMagick, so my monkey patch looked like this:
I added photo_displayed_height and photo_displayed_width accessors to model which owns attachment, so I can store in it size of photo actually displayed in browser
added them to form:
= f.previewbox :photo, width: 100, height: 100
=f.hidden_field :photo_displayed_height
=f.hidden_field :photo_displayed_width
update: (coords) =>
$('#ad_photo_displayed_height').val($('#ad_photo_cropbox').height())
$('#ad_photo_displayed_width').val($('#ad_photo_cropbox').width())
version :thumb do
process :transform_crop_attributes
process crop: :photo ## Crops this version based on original image
resize_to_limit(100,100)
end
def transform_crop_attributes
if file && model
real_width, real_height = MiniMagick::Image.open(file.file)[:dimensions]
if real_height != model.photo_displayed_height || real_width != model.photo_displayed_width
[:photo_crop_y, :photo_crop_w].each do |attr|
model.send(attr) && model.send("#{attr}=", model.send(attr).to_i * real_height.to_i / model.photo_displayed_height.to_i )
end
[:photo_crop_x, :photo_crop_h].each do |attr|
model.send(attr) && model.send("#{attr}=", model.send(attr).to_i * real_width.to_i / model.photo_displayed_width.to_i )
end
end
end
end
I have a model A that uses Carrierwave Crop and when I submit a nested form that includes multiple model Bs that has an image, I get the following error. Character is Model B.
Model A has many Model B and Model B belongs to Model A
undefined method `image_coverpage_crop_x' for #<Character:0x007fcfdd064528>
if @vn.update(vn_params) // This is the line that raises the error
I have ensured that the strong params in Model A includes the cropping fields and that it allows nested attributes. Everything works fine if I update Model A by itself or Model B by itself. Updating all of them at the same time causes the error.
Is it possible to crop an image passing negative values for crop_x and crop_y?
Can't update my rails app due to this gem depends on
carrierwave-crop was resolved to 0.1.2, which depends on
rails (>= 3.2)
Hi and thanks for this great gem.
I have found a little problem that appears when I am using Namespaced Models.
This causes the previewbox and cropbox helper to create html tags with colons "::" inside the id attribute.
For example if my model is Asset::Image and my attachement is file, the helpers will create tags with the ids "asset::image_file_previewbox" and "asset::image_file_cropbox".
Although this is valid according to w3c, this causes some problems when you try to access these attributes by jquery, because you have to escape all colons with a double backslash linke this:
$('#asset\\:\\:image_file_cropbox').Jcrop
It also differs from the rails convention, that only uses underscores as delimiter inside html ids.
Maybe it would be enough to replace all double colons "::" inside "model_name" in helpers.rb with one underscore
No activity for more than 2 years, is this gem still active?
Whenever using accepts_nested_attributes_for and within its hash params, when the Carrierwave field comes as first the attribute, the cropping doesn't work. It only works when it is the last.
For example, this works (picture params (Carrierwave's) as last) :
Project.create "pictures_attributes"=>{
"0"=>{
"picture_crop_x"=>"0",
"picture_crop_y"=>"0",
"picture_crop_w"=>"200",
"picture_crop_h"=>"200",
"picture"=> File.open('/path/img.png')
}
}, "name"=>"A name"
And this doesn't work (picture params as first):
Project.create "pictures_attributes"=>{
"0"=>{
"picture"=> File.open('/path/img.png'),
"picture_crop_x"=>"0",
"picture_crop_y"=>"0",
"picture_crop_w"=>"200",
"picture_crop_h"=>"200"
}
}, "name"=>"A name"
I haven't tried for regular params, only for the accepts_nested_attributes_for.
I looked the source code, it should be handled with method_missing
but the missing_method
is never called, I checked with debugger. I have Rails 4.2.1.
gem 'carrierwave', github: 'carrierwaveuploader/carrierwave'
I'm getting the following error:
Bundler could not find compatible versions for gem "carrierwave":
In Gemfile:
carrierwave
carrierwave-crop was resolved to 0.1.2, which depends on
carrierwave (< 0.11.0, >= 0.8.0)
I cannot use 0.10 as it does not support multiple file uploads, which i need.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.