在创建映像之前,将jCrop与cloudinary一起通过rails 4进行裁剪 [英] Using jCrop with cloudinary through rails 4 to crop before creating image

查看:88
本文介绍了在创建映像之前,将jCrop与cloudinary一起通过rails 4进行裁剪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有一段时间了.我正在使用cloudinary上传照片,并尝试在照片创建操作上使用jcrop实现裁剪功能.实现cloudinary之后,我在Jcrop上遵循了railscasts#182.我认为我在获取新的裁剪参数时遇到问题(x ,y,w,h)在保存图片之前返回给上传者.

I've been at this for some time now. I"m using cloudinary to upload photos and am trying to implement a cropping feature using jcrop on a photo create action. After I implemented cloudinary I followed railscasts #182 on Jcrop. I think I'm having problems getting the new cropped parameters (x,y,w,h) back to the uploader before the image is saved.

更新:我什至没有把值放到f.text_fields中.当我移动收割机时,应该有新的值吧?这是空白字段的图片:

Update: I'm not even getting the values put into the f.text_fields. When I move the cropper around, there should be new values right? Here's a pic of what it looks like with empty fields:

另外,当我提交照片时,现在我在作物剪裁方面遇到了矛盾!=作物

Also when I submit the photo, now I'm getting conflicting transformation on crop fit!=crop

这是我的代码...

image_uploader.rb

image_uploader.rb

class ImageUploader < CarrierWave::Uploader::Base
  #include Sprockets::Helpers::RailsHelper
  #include Sprockets::Helpers::IsolatedHelper

  include Cloudinary::CarrierWave

  process :tags => ["profile pic"]
  process :convert => "jpg"

  version :thumbnail 
    process :crop
    eager
    resize_to_fill(150, 150)
    cloudinary_transformation :quality => 80          
  end 

  # For more options, see
  # http://cloudinary.com/documentation/rails_integration#carrierwave

  def crop
  if model.crop_x.present?
    resize_to_limit(400, 400)
    manipulate! do |img|
      x = model.crop_x.to_i
      y = model.crop_y.to_i
      w = model.crop_w.to_i
      h = model.crop_h.to_i
      img.crop!(x, y, w, h)
    end
  end
end

photos.js.coffee

photos.js.coffee

update = (coords) ->
  $("#crop_x").val coords.x
  $("#crop_y").val coords.y
  $("#crop_w").val coords.w
  $("#crop_h").val coords.h
$(document).ready ->
  $(".cloudinary-fileupload").fileupload(
    dropZone: "#dropzone"
    start: (e) ->
      $(".status").text "Starting upload..."

    progress: (e, data) ->
      $(".status").text "Uploading... " + Math.round((data.loaded * 100.0) / data.total) + "%"

    fail: (e, data) ->
      $(".status").text "Upload failed"
  ).off("cloudinarydone").on "cloudinarydone", (e, data) ->
    $("#photo_bytes").val data.result.bytes
    $(".status").text ""
    $(".preview").html($.cloudinary.image(data.result.public_id,
      format: data.result.format
      width: 400
      height: 400
      crop: "fit"
      id: "jcrop_target"
    )).css height = "400"
    $("#jcrop_target").Jcrop
      aspectRatio: 1
      setSelect: [100, 0, 200, 200]
      onSelect: @update
      onChange: @update

photo.rb(我已在控制器中为crop_x,y,w,h设置了params.permit)

photo.rb (I have set params.permit for crop_x,y,w,h in the controller)

class Photo < ActiveRecord::Base
    belongs_to :master
    belongs_to :dog
    mount_uploader :image, ImageUploader
    validates_presence_of :image
    attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
    before_create :crop_image

    def crop_image
        image.recreate_versions! if crop_x.present?
    end

end

photo/new.html.erb

photo/new.html.erb

<div id="dropzone">
<h1>Change Profile Photo.</h1>
<div class="container">
  <div class="row">
    <div class="col-xs-12 col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2">
      <div id="direct_upload">
        <p>Select the upload button or drag and drop an image.</p>
        <% if params[:photo_option] == "dog_photo" %>
          <% url = master_dog_photos_path %>
        <% else %>
          <% url = master_photos_path %>
        <% end %>
          <%= form_for(@photo, :url => url, role: "form") do |f| %>
          <!--<div class="form_line">
            <%= f.label :title, "Title:" %>
            <div class="form_controls">
              <%= f.text_field :title %>
            </div>
          </div>-->
          <div class="form_line form-group">
            <%= f.label :image, "Image:" %>

            <div class="form-group">
              <div class="upload_button_holder">
                <%= link_to "Upload", "#", class: "btn btn-primary btn-lg upload_button" %>
                <%= f.cl_image_upload(:image) %>
              </div>
              <span class="status"></span>
            </div>
          </div>

          <div class="form-group">
            <div class="form_controls">
              <div class="preview"></div>
            </div>
          </div>

            <%= f.text_field :crop_x, id: "crop_x"%>
            <%= f.text_field :crop_y, id: "crop_y"%>
            <%= f.text_field :crop_w, id: "crop_w"%>
            <%= f.text_field :crop_h, id: "crop_h"%>

          <div class="form-group">
            <div class="form_controls">
              <%= f.submit "Submit Photo", class: "btn btn-lg btn-success" %>
              <% if @error %><span class="error"><%= @error %></span><% end %>
            </div>
          </div>
          <%= f.hidden_field :bytes %>
          <%= hidden_field_tag :direct, params[:direct] %>
        <% end %>
      </div>

      <% if params[:photo_option] == "dog_photo" %>
      <% link_to "Back to House", master_path(@master) %>
      <% else %>
        <a href="<%= edit_master_registration_path %>">Back to Master edit.</a>
      <% end %>
    </div>
  </div>
</div>
</div>

<!-- Configure Cloudinary jQuery plugin -->
<%= cloudinary_js_config %>

推荐答案

好,我通过以下操作解决了它...从更新回调中删除了@,这使字段使用裁剪参数进行了更新(语法错误)对我而言).然后,我将裁剪值永久存储在模型中,并且摆脱了图像.回调,然后将以下代码放入我的上传器中:

Ok I solved it by doing the following... took the @ off of the update callbacks and that made the fields get updated with the cropping parameters (bad syntax on my part). Then I persistantly stored the cropping values in the model and got rid of the image.revisions! callback and then put the following code in my uploader:

class ImageUploader < CarrierWave::Uploader::Base
  #include Sprockets::Helpers::RailsHelper
  #include Sprockets::Helpers::IsolatedHelper

  include Cloudinary::CarrierWave

  process :tags => ["profile pic"]
  process :convert => "jpg"

  version :thumbnail do
    cloudinary_transformation :transformation => [
        {:width => 400, :height => 400, :crop => :limit}]
    process :custom_crop 
  end  

  def custom_crop
    return :x => model.crop_x, :y => model.crop_y, 
      :width => model.crop_w, :height => model.crop_h, :crop => :crop
  end
end

我认为以下内容:

<%= image_tag(@dog.photos.last.image.thumbnail.url, :width => 150, :height => 150, :crop => :fill, class: "img-circle center") %>

这篇关于在创建映像之前,将jCrop与cloudinary一起通过rails 4进行裁剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆