- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我(松散地)关注 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
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
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
<% 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 %>
最佳答案
不知道这是否相同,但我在重新处理时遇到了问题,并按照此处的答案进行了修复:
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/
使用 ImageMagick 我想绘制一个相对于图像宽度的图元 这对我不起作用 mogrify -stroke white -fill white -draw "rectangle 100,100 %
对于一个项目,我试图创建图像的透视失真以匹配 DVD 盒正面模板。所以我想使用 ImageMagick (CLI) 自动执行此操作,但我很难理解此转换的数学方面。 convert \ -verbo
我遵循的是这个示例http://cubiq.org/create-fixed-size-thumbnails-with-imagemagick,这正是我要对图像执行的操作,除了背景残留物(即白色边框)
我将.psd转换为一个文件夹内的.png文件。如何在具有不同扩展名的文件夹中保持每个文件的名称相同? 例如,我输入文件夹图像,然后从终端执行 $ convert *.psd *.png 但是它给.pn
我正在使用以下 ImageMagick 脚本(带有用于 PHP 的 Imagick)来生成字体图像。这个脚本用大约 0.1 秒来生成一个大约 30 个字符的 48 大小的图像。目标速度大约是 0.01
我正在使用以下 ImageMagick 脚本(带有用于 PHP 的 Imagick)来生成字体图像。这个脚本用大约 0.1 秒来生成一个大约 30 个字符的 48 大小的图像。目标速度大约是 0.01
我遇到了一个 paper它使用由 ImageMagick convert 命令生成的合成手写数据,使用许多不同的手写字体 ( Example images from paper )。 他们已经用他们的
我有一张图片。我需要从图像中裁剪矩形区域。该矩形区域由黑色边框标识。矩形里面是我需要的。是否可以在 imagemagick 中裁剪黑色边框矩形区域?我知道可以通过提供矩形区域的偏移量(-crop Wx
我有一堆图片是桌面墙纸,但尺寸不同。对于我的示例,我将使用分辨率 1920x1080。如何缩放图像以便: 对于宽度和高度小于我的屏幕的图像,图像居中然后按比例放大,直到两者中较小的达到 1920 或
我必须处理大量图像,并且正在寻找检测边框颜色的方法。例如,拍摄这张图片: 是否可以通过某种方式确定图像具有白色背景?解决方法是提取 1px 的边框并检查提取的边框的颜色是否同样为白色。但我认为 Ima
我正在使用 PowerShell 脚本将 Unicode 字符批量转换为 PNG 文件。参见 http://pastebin.com/aGJzk4Hh . 我发现要转换 " 字符,必须使用指定标签:\
目前我正在尝试转换为 HEIF,但唯一与 HEIF 相关的选项是 heic:preserve-orientation(在 CLI 选项 define 中)。我如何为 HEIF 和其他格式使用其他特定于
正如您在答案 “Diff” an image using ImageMagick 中看到的那样,“delta”图像以某种方式变亮了: 如何使用 ImageMagick 使图像变亮? 最佳答案 我假设您
我正在使用 ImageMagick 的 convert 来调整一些 .png 文件的大小,问题是 convert 似乎正在向 .png 写入一些额外的信息: EXtdate:create 2012-1
此 blog post 图像中使用的边框阴影效果似乎嵌入在图像本身(不是 css3)中。如何在 imagemagick 中创建它? 编辑 1: 我偶然发现的解决方案作为答案发布在下面。 最佳答案 有一
我有大约 200 张扫描照片,我想从中裁剪掉空白区域。参见示例: 有人可以为我提供适当的命令行代码来执行此操作吗?...我一直在尝试整理 -trim 和 -fuzz 选项,但没有成功。并非所有图像的大
我正在创建一个重命名照片的 bash 脚本,以便它们在文件名中包含日期和时间。我想用 ImageMagick 的 identify 实用程序建立一个文件名。 identify -format "IMG
我有一张长图source pic我想为每个子图像添加文本,但是当向书中添加文本时,我失败了,最终图像和代码: convert src.jpg -gravity north -pointsize 53
我想使用 CLI 向图像添加一个黑色矩形,该图像 1) 居中并且 2) 从每个边缘开始 X 像素。 基本上,我几乎想要 -border 的对立面(而不是添加某种颜色的边框,我想保留图像的 X 像素作为
所以我想创建一张由三张小图片组成的 3600x2280 大图片。第一个应调整为 1680x1050 并放置在左上角。第二个需要调整为 1920x1200 并立即放置在它的右侧(+1680 以上)。第三
我是一名优秀的程序员,十分优秀!