Commit a634308f

mo k <m@mokhan.ca>
2011-12-29 02:23:12
trying to get jcrop loading.
1 parent 75ec6e2
app/assets/javascripts/application.js
@@ -7,4 +7,14 @@
 //= require jquery
 //= require jquery_ujs
 //= require jquery-ui
+//= require jquery.Jcrop
+//= require jquery.color
+//= require bootstrap-alerts
+//= require bootstrap-buttons
+//= require bootstrap-dropdown
+//= require bootstrap-modal
+//= require bootstrap-popover
+//= require bootstrap-scrollspy
+//= require bootstrap-tabs
+//= require bootstrap-twipsy
 //= require_tree .
app/assets/javascripts/photos.js.coffee
@@ -1,3 +0,0 @@
-# Place all the behaviors and hooks related to the matching controller here.
-# All this logic will automatically be available in application.js.
-# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
app/assets/stylesheets/custom.css
@@ -1,2 +1,7 @@
 .fright{float:right;}
 .hidden{display:none;}
+.preview {
+  width:100px;
+  height:100px;
+  overflow:hidden;
+}
app/controllers/creations_controller.rb
@@ -63,8 +63,19 @@ class CreationsController < ApplicationController
     end
   end
 
+  # GET /creations/crop/1
   def crop
-    
+    @creation = current_user.creations.find(params[:id])
+  end
+
+  def crop_update
+    @creation = current_user.creations.find(params[:id])
+    @creation.crop_x = params[:creation]["crop_x"]
+    @creation.crop_y = params[:creation]["crop_y"]
+    @creation.crop_h = params[:creation]["crop_h"]
+    @creation.crop_w = params[:creation]["crop_w"]
+    @creation.save
+    redirect_to(@creation, :notice => 'Creation was successfully cropped.')
   end
 
 end
app/models/creation.rb
@@ -7,6 +7,9 @@ class Creation < ActiveRecord::Base
   has_many :photos, :dependent => :destroy
   mount_uploader :image, ImageUploader
 
+  attr_accessor :crop_x, :crop_y, :crop_h, :crop_w
+  after_update :reprocess
+
   define_index do
     indexes :name, :sortable => true
     indexes story
@@ -16,4 +19,17 @@ class Creation < ActiveRecord::Base
     story.split[0...50].join(' ') + '...'
   end
 
+  def cropping?
+    !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
+  end
+
+  def profile_geometry
+    img = Magick::Image::read('http://cakeside.dev'+self.image_url).first
+    @geometry = {:width => img.columns, :height => img.rows }
+  end
+
+  def reprocess
+    self.image.recreate_versions!
+  end
+
 end
app/uploaders/image_uploader.rb
@@ -33,7 +33,7 @@ class ImageUploader < CarrierWave::Uploader::Base
 
   process :set_content_type
   # process :resize_to_fit => [700, 540]
-  process :resize_to_fit => [660, 470]
+  # process :resize_to_fit => [660, 470]
   process :convert => 'png'
   # process :scale => [200, 300]
   #
@@ -42,10 +42,22 @@ class ImageUploader < CarrierWave::Uploader::Base
   # end
 
   # Create different versions of your uploaded files:
+  version :large do 
+    process :resize_to_fit => [660, 470]
+  end
+
   version :thumb do
+    process :manualcrop
     process :resize_to_fill => [210, 150]
   end
 
+  def manualcrop
+    return unless model.cropping?
+    manipulate! do |img|
+      img = img.crop(model.crop_x.to_i, model.crop_y.to_i,model.crop_h.to_i,model.crop_w.to_i)
+    end
+  end
+
   # Add a white list of extensions which are allowed to be uploaded.
   # For images you might use something like this:
   def extension_white_list
app/views/creations/crop.html.erb
@@ -0,0 +1,42 @@
+<% content_for :head do %>
+<script type="text/javascript" charset="utf-8">
+$(function() {
+    try{
+  $('#cropbox').Jcrop({
+    onChange: update_crop,
+    onSelect: update_crop,
+    setSelect: [0, 0, 500, 500],
+    aspectRatio: 1
+  });
+  }catch(e){
+  alert(e);
+  }
+});
+
+function update_crop(coords) {
+    var rx = 100/coords.w;
+    var ry = 100/coords.h;
+    var lw = $('#cropbox').width();
+    var lh = $('#cropbox').height();
+    var ratio = <%= @creation.profile_geometry[:width] %> / lw ;
+
+  $('#preview').css({
+    width: Math.round(rx * lw) + 'px',
+    height: Math.round(ry * lh) + 'px',
+    marginLeft: '-' + Math.round(rx * coords.x) + 'px',
+    marginTop: '-' + Math.round(ry * coords.y) + 'px'
+  });
+  $("#crop_x").val(Math.round(coords.x * ratio));
+  $("#crop_y").val(Math.round(coords.y * ratio));
+  $("#crop_w").val(Math.round(coords.w * ratio));
+  $("#crop_h").val(Math.round(coords.h * ratio));
+}
+</script>
+<% end %>
+
+<%= image_tag @creation.image_url, :id => "cropbox" %>
+
+<h3>Preview</h3>
+<div class="preview">
+    <%= image_tag @creation.image_url, :id => "preview" %>
+</div>
app/views/layouts/application.html.erb
@@ -8,16 +8,19 @@
     <!--[if lt IE 9]>
     <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
     <![endif]-->
-    <%= stylesheet_link_tag :application %>
+<%= stylesheet_link_tag :application %>
+<%= stylesheet_link_tag params[:controller] %>
     <style type="text/css">
       body { padding-top: 60px; }
     </style>
-    <link rel="shortcut icon" href="/assets/favicon.ico">
-    <link rel="apple-touch-icon" href="/assets/apple-touch-icon.png">
-    <link rel="apple-touch-icon" sizes="72x72" href="/assets/apple-touch-icon-72x72.png">
-    <link rel="apple-touch-icon" sizes="114x114" href="/assets/apple-touch-icon-114x114.png">
+<link rel="shortcut icon" href="/assets/favicon.ico">
+<link rel="apple-touch-icon" href="/assets/apple-touch-icon.png">
+<link rel="apple-touch-icon" sizes="72x72" href="/assets/apple-touch-icon-72x72.png">
+<link rel="apple-touch-icon" sizes="114x114" href="/assets/apple-touch-icon-114x114.png">
+<%= javascript_include_tag :application %>
+<%= javascript_include_tag params[:controller] %>
+<%= yield :head %>
     <%= csrf_meta_tags %>
-    <%= javascript_include_tag "application" %>
   </head>
   <body>
     <div class="topbar" data-dropdown="dropdown" >
config/routes.rb
@@ -8,6 +8,8 @@ Cake::Application.routes.draw do
   resources :creations do
     resources :photos, :only => [:create, :destroy]
   end
+  match 'creations/crop/:id' => 'creations#crop', :method => 'GET'
+  # match 'creations/crop_update' => 'creations#crop_update', :as => 'crop_update', :method => 'POST'
 
   # /profiles
   get "profiles/index"
app/assets/javascripts/bootstrap-alerts.js → vendor/assets/javascripts/bootstrap-alerts.js
File renamed without changes
app/assets/javascripts/bootstrap-buttons.js → vendor/assets/javascripts/bootstrap-buttons.js
File renamed without changes
app/assets/javascripts/bootstrap-dropdown.js → vendor/assets/javascripts/bootstrap-dropdown.js
File renamed without changes
app/assets/javascripts/bootstrap-modal.js → vendor/assets/javascripts/bootstrap-modal.js
File renamed without changes
app/assets/javascripts/bootstrap-popover.js → vendor/assets/javascripts/bootstrap-popover.js
File renamed without changes
app/assets/javascripts/bootstrap-scrollspy.js → vendor/assets/javascripts/bootstrap-scrollspy.js
File renamed without changes
app/assets/javascripts/bootstrap-tabs.js → vendor/assets/javascripts/bootstrap-tabs.js
File renamed without changes
app/assets/javascripts/bootstrap-twipsy.js → vendor/assets/javascripts/bootstrap-twipsy.js
File renamed without changes
app/assets/javascripts/jquery.color.js → vendor/assets/javascripts/jquery.color.js
File renamed without changes
app/assets/javascripts/jquery.Jcrop.js → vendor/assets/javascripts/jquery.Jcrop.js
File renamed without changes
capfile
@@ -2,7 +2,7 @@ load 'deploy' if respond_to?(:namespace) # cap2 differentiator
 Dir['vendor/gems/*/recipes/*.rb','vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
 
 load 'config/deploy' # remove this line to skip loading any of the default tasks
-# load 'deploy/assets'
+load 'deploy/assets'
 
 task :search_libs, :roles => :web do
   run "ls -x1 /usr/lib | grep -i xml"