Code Monkey home page Code Monkey logo

cropper's Introduction

#cropper

Rails gem with Jquery and Jcrop

Expiremental Version. Use At Your Own Risk

How does works.

Crops an image and send to your form, when you update/create your model, the cropped image path is sent to your model. integrating with paperclip in a simple way.

Installation

Add Cropper to your Gemfile:

gem 'cropper', :git => 'git://github.com/reinaldomendes/cropper.git'

And run bundle install within your app's directory.

in your model

without paperclip

#User
 class User < ActiveRecord::Base   
   cropper :image
   def image= file
    FileUtils.copy(file,SOME_DIRECTORY)
    #.. do something
    super SOME_DIRECTORY
    
   end
   ...
 end

using paperclip

If you are using paperclip go to your active record model.

#User
 class User < ActiveRecord::Base
   has_attached_file :image
   cropper :image
   ...
 end

in your view

Using prettyPhoto

 #new.html.erb
 <%= form_for(@user) do |f| %>
    <%= f.crop_field :file,:rel => 'prettyPhoto' do %>
      .... Any Html Here
    <%end%>
 <%end %>

Using iframe

 #new.html.erb
 <iframe id="myIframe"></iframe>
 <%= form_for(@user) do |f| %>
    <%= f.crop_field :file,:target => 'myIframe' do %>
      .... Any Html Here
    <%end%>
 <%end %>

also you can bind events with jQuery or other library, the inportant thing to observe is: the url of link created by "f.crop_field" method.

Modifying the behavior of Jcrop

You can call methods of 'jcrop_api'

 //new.html.erb
<script type="text/javascript">
//<![CDATA[
  window.cropper = {
    init: function(jcrop_api){// the init method of copper object expose the 'jcrop_api' object
      
    jcrop_api.setOptions({
        'aspectRatio' : 3/4,
        'minSize' : [60,80]
      });
    },
    finish:function(){//called when user crop an image and return from server
      //do something
      var iframe = document.getElementById('_iframe')
      iframe.parentNode.removeChild(iframe)
    }
  }
//]]>
</script> 

Temporary Image Files

Cropper uses the rails temp dir/cropper to save temporary image files. Those files is auto excluded in certain time.

configuration

You can control this time by defining the following initializer

 #config/initializers/cropper.rb
 Cropper.setup do| config|
   cropper.clean_tmp_time = 15.minutes #clean temporary files time
   cropper.mount_at = "/" # scope for rails routes
 end

More reference about Jcrop

http://deepliquid.com/content/Jcrop.html

TODO

  • Create rails generators for more flexibility of user
  • Simplify cropper views, separating embed js in other files when possible.

cropper's People

Contributors

reinaldomendes avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.