gpt4 book ai didi

ruby-on-rails - 使用 Paperclip、ImageMagick、Jcrop 和 S3 存储进行裁剪 : Why won't 'image.reprocess!' reprocess?

转载 作者:行者123 更新时间:2023-12-04 03:43:35 25 4
gpt4 key购买 nike

我(松散地)关注 RailsCasts tutorial #182 ,它使用 Paperclip、ImageMagick 和 Jcrop 来允许自定义裁剪上传的图像。

由于我使用 Amazon S3 进行文件存储,因此我不得不重新调整教程的部分内容以适应。一切似乎都运行良好,除了我的图像的裁剪版本没有被重新处理(或者重新处理的结果没有重新上传到 S3)——所以在裁剪过程之后,我留下了我最初上传的相同图像(这适用于我为每个图像存储的所有图像尺寸)。

这是我的特征(如特征图像)模型:

class Feature < ActiveRecord::Base
require "#{Rails.root}/lib/paperclip_processors/cropper.rb"

attr_accessible :image_file_name, :image
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
after_update :reprocess_image, :if => :cropping?

if Rails.env == "production"
S3_CREDENTIALS = { :access_key_id => '<REDACTED>',
:secret_access_key => '<REDACTED>',
:bucket => "<REDACTED>"}
else
S3_CREDENTIALS = { :access_key_id => '<REDACTED>',
:secret_access_key => '<REDACTED>',
:bucket => "<REDACTED>"}
end

has_attached_file :image,
:styles => { :small => "240x135>", :croppable => "960x960>", :display => "960x540>" },
:processors => [:cropper],
:storage => :s3,
:s3_credentials => S3_CREDENTIALS,
:path => "features/:id/:style.:extension"

validates_attachment_content_type :image, :content_type => ['image/jpeg', 'image/gif', 'image/png',
'image/pjpeg', 'image/x-png'],
:message => 'must be a JPEG, GIF or PNG image'

def cropping?
!crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
end

def image_geometry(style = :original)
@geometry ||= {}
path = (image.options[:storage]==:s3) ? image.url(style) : image.path(style)
@geometry[style] ||= Paperclip::Geometry.from_file(path)
end

private

def reprocess_image
image.reprocess!
end
end

这是我的“cropper.rb”(回形针处理器):
module Paperclip
class Cropper < Thumbnail
def transformation_command
if crop_command
crop_command + super.sub(/ -crop \S+/, '')
else
super
end
end

def crop_command
target = @attachment.instance
if target.cropping?
" -crop '#{target.crop_w}x#{target.crop_h}+#{target.crop_x}+#{target.crop_y}'"
end
end
end
end

我的 FeaturesController 的相关操作:
class FeaturesController < ApplicationController

def new
@feature = Feature.new
end

def create
@feature = Feature.new(params[:feature])
if @feature.save
if params[:feature][:image].blank?
flash[:notice] = "New feature added!"
redirect_to @feature
else
render :crop
end
else
@title = "Add a New Feature"
render :new
end
end

def edit
@feature = Feature.find(params[:id])
@title = "Edit #{@feature.headline}"
end

def update
@feature = Feature.find(params[:id])
if @feature.update_attributes(params[:feature])
if params[:feature][:image].blank?
flash[:notice] = "Feature updated!"
redirect_to @feature
else
render :crop
end
else
@title = "Edit Feature"
render :edit
end
end
end

以及我的“crop.html.erb” View 的相关行:
<% content_for :javascript_includes do %>
<%= javascript_include_tag 'jquery.Jcrop.min' %>
<script type="text/javascript" charset="utf-8">
$(function() {
$('#cropbox').Jcrop({
onChange: update_crop,
onSelect: update_crop,
setSelect: [0, 0, 960, 540],
aspectRatio: 960/540
});
});

function update_crop(coords) {
var ratio = <%= @feature.image_geometry(:original).width %> / <%= @feature.image_geometry(:croppable).width %>;
$("#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 %>
<% content_for :style_includes do %>
<%= stylesheet_link_tag 'jquery.Jcrop', :media => 'screen' %>
<% end %>

<%= image_tag @feature.image.url(:croppable), :id => "cropbox" %>

<% form_for @feature do |f| %>
<% for attribute in [:crop_x, :crop_y, :crop_w, :crop_h] %>
<%= f.hidden_field attribute, :id => attribute %>
<% end %>
<p><%= f.submit "Crop" %></p>
<% end %>

问题不是自定义裁剪(偏移、裁剪区域等)有错误,而是存在 没有裁剪发生 当我单击“裁剪”时——我只剩下从原始上传/过程中获得的图像。似乎没有“image.reprocess!”正在发生(或者重新处理的结果没有保存到 S3)。

为什么会这样,我该怎么办?

最佳答案

不知道这是否相同,但我在重新处理时遇到了问题,并按照此处的答案进行了修复:

paperclip error while reporcessing after rails 3 upgrade

关于ruby-on-rails - 使用 Paperclip、ImageMagick、Jcrop 和 S3 存储进行裁剪 : Why won't 'image.reprocess!' reprocess?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6615707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com