Ruby on rails Carrierwave和Jcrop未使用Minimagick进行裁剪
我正在学习Ryan Bates关于如何使用Carrierwave和Jcrop裁剪图像的教程 但是,图像没有被裁剪。x、 y、w、h值正在参数中传递,但未进行裁剪。我用的是Minimagick。我真的很感激别人的帮助 我在Stackoverflow中也关注过类似的问题,但没有一个能给出好的解决方案。 多谢各位 照片上传器Ruby on rails Carrierwave和Jcrop未使用Minimagick进行裁剪,ruby-on-rails,carrierwave,jcrop,Ruby On Rails,Carrierwave,Jcrop,我正在学习Ryan Bates关于如何使用Carrierwave和Jcrop裁剪图像的教程 但是,图像没有被裁剪。x、 y、w、h值正在参数中传递,但未进行裁剪。我用的是Minimagick。我真的很感激别人的帮助 我在Stackoverflow中也关注过类似的问题,但没有一个能给出好的解决方案。 多谢各位 照片上传器 process :resize_to_fit => [800,800] version :big do process :resize_to_limit
process :resize_to_fit => [800,800]
version :big do
process :resize_to_limit => [800,600]
process :convert => 'jpg'
end
version :thumb, :from_version => :big do
process :crop
resize_to_fill(100,100)
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 "#{model.crop_x}x#{model.crop_y}+#{model.crop_w}+#{model.crop_h}"
img
end
end
end
查看
<%= image_tag @photo.photo_url(:big), id: "cropbox" %>
更新
#Cropping spots
def crop_spot
photo.recreate_versions! if crop_x.present?
end
img.crop
方法不会更改图像,但会返回一个新图像。所以要么使用img.crop
,它在位编辑图像,或删除最后一个img
,以便重新编辑图像
然后,crop
方法变为
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 "#{model.crop_x}x#{model.crop_y}+#{model.crop_w}+#{model.crop_h}"
end
end
end
对我来说,它是这样工作的:
include CarrierWave::MiniMagick
version :thumb do
process :resize_to_fit => [Cover::IMAGE_WIDTH, -1] # '-1' means that height is infinite (this is works for minimagick, but for rmagick it should be '10000').
process :manual_crop
process :resize_to_fill => [340, 191]
end
version :big do
process :resize_to_fit => [Cover::IMAGE_WIDTH, -1]
process :manual_crop
process :resize_to_fill => [Cover::IMAGE_WIDTH, Cover::IMAGE_HEIGHT]
end
def manual_crop
return if model.crop_coords.blank?
manipulate! do |img|
img.crop "#{model.crop_coords['w'].to_i}x#{model.crop_coords['h'].to_i}+#{model.crop_coords['x'].to_i}+#{model.crop_coords['y'].to_i}!"
img
end
end
crop_坐标是由JCrop返回的坐标(但我们不需要x2和y2,所以这里不使用它们)
考虑到格式为“WxH+X+Y”。
请参阅和您的作物定位方法在哪里?你能把它贴出来吗please@Baloo请检查我的最新邮件是否由javascrpt设置了裁剪文本字段?@Baloo抱歉,我没有看到您的回复。是的,正在设置值。我还是不知道该怎么办!是给玛吉克的。img.crop适用于MiniMagick。文档表明crop期望WxH+X+Y
include CarrierWave::MiniMagick
version :thumb do
process :resize_to_fit => [Cover::IMAGE_WIDTH, -1] # '-1' means that height is infinite (this is works for minimagick, but for rmagick it should be '10000').
process :manual_crop
process :resize_to_fill => [340, 191]
end
version :big do
process :resize_to_fit => [Cover::IMAGE_WIDTH, -1]
process :manual_crop
process :resize_to_fill => [Cover::IMAGE_WIDTH, Cover::IMAGE_HEIGHT]
end
def manual_crop
return if model.crop_coords.blank?
manipulate! do |img|
img.crop "#{model.crop_coords['w'].to_i}x#{model.crop_coords['h'].to_i}+#{model.crop_coords['x'].to_i}+#{model.crop_coords['y'].to_i}!"
img
end
end