回形针自定义处理器不更改图像类型 [英] Paperclip custom processor not changing image type
问题描述
我在使用回形针自定义处理器时遇到了一些问题。
I'm having a few problems with a paperclip custom processor.
在命令行这一行:
$ 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。这正是我想要实现的目标。
i.e a JPEG converted into a PNG with a transparent background. Which is exactly what I am trying to achieve.
然而,当我尝试使用Paperclip在Rails(4.0.1)中执行此操作时,我最终得到:
However when I attempt to do this within Rails (4.0.1) using Paperclip I end up with :
[链接发表于评论]
[Link posted in comment]
它被重命名为PNG,但实际上是JPEG。
It's renamed as a PNG but is actually a 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
。有什么想法?
In my Database photo_content_type
is image/jpeg
and photo_file_name
is cats.jpg
when I would have expected image/png
and cats.png
respectively. Any ideas?
更新
错误在这一行
temp_file = Tempfile.new([@basename, @format].compact.join("."))
将其更改为
temp_file = Tempfile.new([@basename, @format])
修复问题。感谢shaun-frost-duke-jackson
fixes things. Credit to shaun-frost-duke-jackson
推荐答案
看看网站上的文档,但很确定它应该像下面:
Have a look at the documentation on the website but pretty sure it should be like below:
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("."))
这篇关于回形针自定义处理器不更改图像类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!