回形针自定义处理器不更改图像类型
我用回形针自定义处理器遇到了一些问题。
在命令行这一行:
$ convert cats.jpg -thumbnail 300x400 -bordercolor white -background black +polaroid cats.png
成功转换为:
https://dl.dropboxusercontent.com/u/4233433/cats.jpg
进入这个:
https://dl.dropboxusercontent.com/u/4233433/cats.png
即JPEG转换为具有透明背景的PNG。 这正是我想要实现的目标。
但是,当我尝试使用Paperclip在Rails(4.0.1)中执行此操作时,我最终得到:
[链接发表在评论]
它被重命名为PNG,但实际上是JPEG。
我的模特:
class Submission < ActiveRecord::Base has_attached_file :photo, processors: [:polarize], styles: { polarized: { format: 'png', is_polarized: true } } belongs_to :user end
我的处理器:
module Paperclip class Polarize < Processor def initialize file, options = {}, attachment = nil super @file = file @attachment = attachment @is_polarized = options[:is_polarized] @current_format = File.extname(@file.path) @format = options[:format] @basename = File.basename(@file.path, @current_format) end def make temp_file = Tempfile.new([@basename, @format].compact.join(".")) temp_file.binmode if @is_polarized run_string = "convert #{fromfile} -thumbnail 300x400 -bordercolor white -background white +polaroid #{tofile(temp_file)}" Paperclip.run(run_string) end temp_file end def fromfile File.expand_path(@file.path) end def tofile(destination) File.expand_path(destination.path) end end end
在我的数据库中, photo_content_type
是image/jpeg
, photo_file_name
是cats.jpg
当我分别想到image/png
和cats.png
。 有任何想法吗?
UPDATE
错误就在这一行
temp_file = Tempfile.new([@basename, @format].compact.join("."))
改变它
temp_file = Tempfile.new([@basename, @format])
解决问题。 感谢shaun-frost-duke-jackson
看看网站上的文档,但很确定它应该如下所示:
has_attached_file :avatar, :styles => { :thumb => ["32x32#", :png] }
https://github.com/thoughtbot/paperclip
在后期处理
猜猜你的问题在这里:
@format = options[:format] @basename = File.basename(@file.path, @current_format) temp_file = Tempfile.new([@basename, @format].compact.join("."))