No commit activity in last 3 years
No release in over 3 years
CarrierWave extension for on fly specifying crop area with preview and cropping images before they saved.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

Gem Version

CarrierWave-Crop-On-Fly

CarrierWave extension for on fly specifying crop area with preview and cropping images before they saved.

this gem based on carrierwave-crop

Installation

Install the latest stable release:

    $[sudo] gem install carrierwave-crop-on-fly

In Rails, add it to your Gemfile:

    gem 'carrierwave-crop-on-fly'

Also you must add mini_magick or rmagick gem

    gem 'mini_magick'

or

    gem 'rmagick'

And then execute:

    $ bundle

Finally, restart the server to apply the changes.

Getting Started

Add the required files in assets

In .js file

    //= require jquery
    //= require jcrop.js
    //= require_self
    
    $(document).ready(function() {
        document.jcrop.init({ file_input_id: 'user_avatar' });
    });

In .css file

    *= require jquery.jcrop
    *= require jcrop_fix

Usage

Open your model file and add the CarrierWave uploader:

    class User < ActiveRecord::Base
      mount_uploader :avatar, AvatarUploader
    end

In the CarrierWave uploader:

    class AvatarUploader < CarrierWave::Uploader::Base
      # use mini_magick
      include CarrierWave::MiniMagick
      # or you may use rmagick:
      # include CarrierWave::RMagick

      process crop: [100, 100]
    end

In the view:

    <%= form_for @user do |f| %>
      <%= f.file_field :avatar %>
      <%= f.cropbox :avatar, width: 300, height: 300 %>
      <%= f.previewbox :avatar %>
      <%= f.submit 'Crop' %>
    <% end %>

in controller

    class UsersController < ApplicationController
        def update
            @user = User.find(params[:id])
            @user.update_attributes(user_params)
        end
        
        private
        
        def user_params
            params.require(:user).permit(:avatar)
        end
    end

Credits and resources