gpt4 book ai didi

ruby-on-rails - jcrop 预览包含奇怪的错误

转载 作者:行者123 更新时间:2023-12-04 06:03:14 24 4
gpt4 key购买 nike

我的 jcrop 预览需要帮助。我正在关注 railscasts 中的本教程#182,但出了点问题,我似乎能弄明白。这有点难以解释,但裁剪预览不起作用,第二张图像出现在裁剪调整 handle 内。这个额外的图像会随着裁剪尺寸的调整而拉伸(stretch)和倾斜。由于这有点令人困惑,而且我的应用程序仍在我的本地计算机上,因此我包含了一个 video demo of the current functionality 以及我假设的是下面的所有相关代码。

我也给 jcrop 发了一封邮件,他们说这可能是我预览区的错误导致的。我认为这是正确的,因为我目前正在使用 Ryan Bates 的 railscasts tut 提供的工具。我认为唯一可能有问题的是“attr_accessible”和“attr_accessor”。我是 ruby​​ 和 rails 的新手,所以我很难理解我的错误。任何帮助将不胜感激,如果我可以提供任何进一步的信息,请告诉我。

首先是我的avatar_uploader.rb

class AvatarUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick

storage :file

# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

version :large do
resize_to_limit(600, 600)
end

version :thumb do
process :crop
resize_to_fill(100, 100)
end

version :mini do
process :crop
resize_to_fill(48, 48)
end

version :nano do
process :crop
resize_to_fill(20, 20)
end

def crop
if model.crop_x.present?
resize_to_limit(600, 600)
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
end

用户.rb

class User < ActiveRecord::Base
belongs_to :team
# Define roles
ROLES = %w[admin conference president tailgater author banned]

# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me,
:role, :avatar, :team_id
#Include uploader
mount_uploader :avatar, AvatarUploader
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
after_update :crop_avatar

def crop_avatar
avatar.recreate_versions! if crop_x.present?
end

end

crop.html.erb

<h1>Crop Avatar</h1>

<%= image_tag @user.avatar_url(:large), id: "cropbox" %>

<h4>Preview</h4>
<div style="width:100px; height:100px; overflow:hidden">
<%= image_tag @user.avatar.url(:large), :id => "mypreview" %>
</div>

<%= form_for @user do |f| %>
<% %w[x y w h].each do |attribute| %>
<%= f.hidden_field "crop_#{attribute}" %>
<% end %>
<div class="actions">
<%= f.submit "Crop" %>
</div>
<% end %>

用户.js.咖啡

jQuery ->
new AvatarCropper()

class AvatarCropper
constructor: ->
$('#cropbox').Jcrop
aspectRatio: 1
setSelect: [0, 0, 600, 600]
onSelect: @update
onChange: @update

update: (coords) =>
$('#user_crop_x').val(coords.x)
$('#user_crop_y').val(coords.y)
$('#user_crop_w').val(coords.w)
$('#user_crop_h').val(coords.h)
@updatePreview(coords)

updatePreview: (coords) =>
$('#preview').css
width: Math.round(100/coords.w * $('#cropbox').width()) + 'px'
height: Math.round(100/coords.h * $('#cropbox').height()) + 'px'
marginLeft: '-' + Math.round(100/coords.w * coords.x) + 'px'
marginTop: '-' + Math.round(100/coords.h * coords.y) + 'px'

最佳答案

我遇到了和你一样的问题。这是由于与 bootstrap css 冲突。详情和解决办法可以找here .

关于ruby-on-rails - jcrop 预览包含奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9510568/

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