Papercrop不会保存裁剪的图像

时间:2016-02-29 08:33:59

标签: ruby-on-rails ruby-on-rails-4 rubygems paperclip carrierwave

我已经安装了papercrop 0.3并使用了回形针版本4.3.1 37589f9。

我在裁剪视图中有以下代码:

 <%= form_for @image, url: wizard_path, method: :put, html:{ class: "padding #{"processed" if @event.errors.present?}" } do |f| %>
      <%= f.cropbox :photo, :width => 500 %>
       <%= f.crop_preview :photo, :width => 150 %>
      <%= f.submit "Save", , :id=>"commit" %>
 <% end %>

图像和预览正确渲染。我按下提交按钮,以下是我服务器控制台中的输出:

Started PUT "/events/90/activity_setup/activity_image_crop" for 127.0.0.1 at 2016-02-29 03:19:16 -0500
Processing by ActivitySetupController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"9P3TTupYZPpbudEtcy2NUi7vQM98FViFslQGWteffRPz8nQ/SYI0JIhclFH4C6Zi0SQdiiBzJEoBhLEaKxhzdA==", "event_image"=>{"photo_original_w"=>"655", "photo_original_h"=>"435", "photo_box_w"=>"500", "photo_aspect"=>"1.0", "photo_crop_x"=>"381", "photo_crop_y"=>"124", "photo_crop_w"=>"250", "photo_crop_h"=>"250"}, "commit"=>"", "event_id"=>"90", "id"=>"activity_image_crop"}
  Event Load (0.5ms)  SELECT  "events".* FROM "events" WHERE "events"."id" = $1 LIMIT 1  [["id", 90]]
step is:activity_image_crop
   (0.3ms)  SELECT COUNT(*) FROM "event_categories" WHERE "event_categories"."event_id" = $1  [["event_id", 90]]
  Schedule Load (0.2ms)  SELECT  "schedules".* FROM "schedules" WHERE "schedules"."event_id" = $1 LIMIT 1  [["event_id", 90]]
   (0.2ms)  SELECT COUNT(*) FROM "ticket_types" WHERE "ticket_types"."event_id" = $1  [["event_id", 90]]
   (0.2ms)  SELECT COUNT(*) FROM "event_images" WHERE "event_images"."event_id" = $1  [["event_id", 90]]
  EventImage Load (0.3ms)  SELECT  "event_images".* FROM "event_images" WHERE "event_images"."event_id" = $1  ORDER BY "event_images"."id" ASC LIMIT 1  [["event_id", 90]]
   (0.1ms)  BEGIN
Command :: PATH=/usr/local/bin/:$PATH; file -b --mime '/var/folders/ph/fy4ch9s11ssgdrc_dd8ltr040000gn/T/0d0871f0806eae32d30983b62252da5020160229-53505-uttb90.jpeg'
Command :: PATH=/usr/local/bin/:$PATH; identify -format '%wx%h,%[exif:orientation]' '/var/folders/ph/fy4ch9s11ssgdrc_dd8ltr040000gn/T/0d0871f0806eae32d30983b62252da5020160229-53505-1gxacyq.jpeg[0]' 2>/dev/null
Command :: PATH=/usr/local/bin/:$PATH; identify -format %m '/var/folders/ph/fy4ch9s11ssgdrc_dd8ltr040000gn/T/0d0871f0806eae32d30983b62252da5020160229-53505-1gxacyq.jpeg[0]'
Command :: PATH=/usr/local/bin/:$PATH; convert '/var/folders/ph/fy4ch9s11ssgdrc_dd8ltr040000gn/T/0d0871f0806eae32d30983b62252da5020160229-53505-1gxacyq.jpeg[0]' -crop 250x250+381+124 -auto-orient -resize "x100" +repage -quality 100 '/var/folders/ph/fy4ch9s11ssgdrc_dd8ltr040000gn/T/8dc9c06ba7fa16c1011aed269772a9cf20160229-53505-ehwzjg'
Command :: PATH=/usr/local/bin/:$PATH; identify -format '%wx%h,%[exif:orientation]' '/var/folders/ph/fy4ch9s11ssgdrc_dd8ltr040000gn/T/0d0871f0806eae32d30983b62252da5020160229-53505-1gxacyq.jpeg[0]' 2>/dev/null
Command :: PATH=/usr/local/bin/:$PATH; identify -format %m '/var/folders/ph/fy4ch9s11ssgdrc_dd8ltr040000gn/T/0d0871f0806eae32d30983b62252da5020160229-53505-1gxacyq.jpeg[0]'
Command :: PATH=/usr/local/bin/:$PATH; convert '/var/folders/ph/fy4ch9s11ssgdrc_dd8ltr040000gn/T/0d0871f0806eae32d30983b62252da5020160229-53505-1gxacyq.jpeg[0]' -crop 250x250+381+124 -auto-orient -resize "450x300!" -quality 100 '/var/folders/ph/fy4ch9s11ssgdrc_dd8ltr040000gn/T/8dc9c06ba7fa16c1011aed269772a9cf20160229-53505-pfkfle'
Command :: PATH=/usr/local/bin/:$PATH; identify -format '%wx%h,%[exif:orientation]' '/var/folders/ph/fy4ch9s11ssgdrc_dd8ltr040000gn/T/0d0871f0806eae32d30983b62252da5020160229-53505-1gxacyq.jpeg[0]' 2>/dev/null
Command :: PATH=/usr/local/bin/:$PATH; identify -format %m '/var/folders/ph/fy4ch9s11ssgdrc_dd8ltr040000gn/T/0d0871f0806eae32d30983b62252da5020160229-53505-1gxacyq.jpeg[0]'
Command :: PATH=/usr/local/bin/:$PATH; convert '/var/folders/ph/fy4ch9s11ssgdrc_dd8ltr040000gn/T/0d0871f0806eae32d30983b62252da5020160229-53505-1gxacyq.jpeg[0]' -crop 250x250+381+124 -auto-orient -resize "600x400!" -quality 100 '/var/folders/ph/fy4ch9s11ssgdrc_dd8ltr040000gn/T/8dc9c06ba7fa16c1011aed269772a9cf20160229-53505-1q3w5pu'
Command :: PATH=/usr/local/bin/:$PATH; identify -format '%wx%h,%[exif:orientation]' '/var/folders/ph/fy4ch9s11ssgdrc_dd8ltr040000gn/T/0d0871f0806eae32d30983b62252da5020160229-53505-1gxacyq.jpeg[0]' 2>/dev/null
Command :: PATH=/usr/local/bin/:$PATH; identify -format %m '/var/folders/ph/fy4ch9s11ssgdrc_dd8ltr040000gn/T/0d0871f0806eae32d30983b62252da5020160229-53505-1gxacyq.jpeg[0]'
Command :: PATH=/usr/local/bin/:$PATH; convert '/var/folders/ph/fy4ch9s11ssgdrc_dd8ltr040000gn/T/0d0871f0806eae32d30983b62252da5020160229-53505-1gxacyq.jpeg[0]' -crop 250x250+381+124 -auto-orient -resize "750x300!" -quality 100 '/var/folders/ph/fy4ch9s11ssgdrc_dd8ltr040000gn/T/8dc9c06ba7fa16c1011aed269772a9cf20160229-53505-1g591aq'
[paperclip] deleting /Users/andyarmstrong/Documents/Personal/clazzoo_main/public/system/event_images/photos/000/000/096/original/929.jpeg
[paperclip] deleting /Users/andyarmstrong/Documents/Personal/clazzoo_main/public/system/event_images/photos/000/000/096/thumb/929.jpeg
[paperclip] deleting /Users/andyarmstrong/Documents/Personal/clazzoo_main/public/system/event_images/photos/000/000/096/medium/929.jpeg
[paperclip] deleting /Users/andyarmstrong/Documents/Personal/clazzoo_main/public/system/event_images/photos/000/000/096/large/929.jpeg
[paperclip] deleting /Users/andyarmstrong/Documents/Personal/clazzoo_main/public/system/event_images/photos/000/000/096/desktop/929.jpeg
   (0.2ms)  COMMIT
   (0.1ms)  BEGIN

仔细观察,我的crop_x为381和crop_y 124似乎是在控制台命令中,这意味着事情进展顺利.....我可以多次看到命令:-crop 250x250+381+124。< / p>

然而,当我使用以下方式渲染图像时

<%= image_tag @event.event_images.first.photo.url %> I get the same unchanged image. Do I need to pass some kind of parameter to url to show my cropped file?!

存储图像的模型在这里:

# == Schema Information
#
# Table name: event_images
#
#  id                 :integer          not null, primary key
#  caption            :string
#  event_id           :integer
#  created_at         :datetime         not null
#  updated_at         :datetime         not null
#  photo_file_name    :string
#  photo_content_type :string
#  photo_file_size    :integer
#  photo_updated_at   :datetime
#  canned_image_id    :string
#

class EventImage < ActiveRecord::Base
  attr_accessor :PAPERCLIP_STORAGE_OPTS

  has_attached_file :photo, PAPERCLIP_STORAGE_OPTS
  crop_attached_file :photo

  validates_attachment_content_type :photo, :content_type => ["image/jpg", "image/jpeg", "image/gif", "image/png"]
  validates_with AttachmentSizeValidator, :attributes => :photo, :less_than => 3.megabytes

  belongs_to :event

   def photo_url(size=nil)
    if(canned_image_id)then
      path = ActionController::Base.helpers.image_path("event_create/activity_image/choice-"+canned_image_id+".png")
      path = path.sub! '/assets/', ''
      return path
    else
      if(size)then
        return photo.url(size)
      else
        return photo.url()
      end
    end
  end
end

用于更新此模型实例的控制器代码是(在更新方法中:

image = @event.event_images.first
 image.update_attributes(image_params)

我的PAPER_CLIP_STORAGE_OPTS是:

PAPERCLIP_STORAGE_OPTS = {
     :styles => { :thumb => '100x100!#', :medium => '450x300!>', :large => '600x400!>', :desktop => '750x300!>'},
     :convert_options => { :all => '-quality 100' },
     :processor       => [ :cropper,:papercrop ],
     :default_url => 'business.jpg'
   }

我尝试删除处理器阵列中的cropper,这可能是冲突的,但没有区别。

请帮忙

1 个答案:

答案 0 :(得分:1)

我创建了一个简单的应用来重现您的问题:https://github.com/universal/papercropTest

从该示例应用程序中,papercrop gem似乎只裁剪样式中定义的变体,但保持原始文件不变。所以我想你已经有效的代码,与你预期的略有不同。

因此,应正确裁剪拇指,中等和大的变化,原始保持不变。这有意义,但可能只是违反直觉。 : - )