问题
I'm having a few problems with a paperclip custom processor.
At the command line this line :
$ convert cats.jpg -thumbnail 300x400 -bordercolor white -background black +polaroid cats.png
Successfully converts this :
https://dl.dropboxusercontent.com/u/4233433/cats.jpg
Into this :
https://dl.dropboxusercontent.com/u/4233433/cats.png
i.e a JPEG converted into a PNG with a transparent background. Which is exactly what I am trying to achieve.
However when I attempt to do this within Rails (4.0.1) using Paperclip I end up with :
[Link posted in comment]
It's renamed as a PNG but is actually a JPEG.
My model :
class Submission < ActiveRecord::Base
has_attached_file :photo,
processors: [:polarize],
styles: {
polarized: {
format: 'png',
is_polarized: true
}
}
belongs_to :user
end
And my processor :
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
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?
UPDATE
The error was in this line
temp_file = Tempfile.new([@basename, @format].compact.join("."))
changing it to
temp_file = Tempfile.new([@basename, @format])
fixes things. Credit to shaun-frost-duke-jackson
回答1:
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
Under post processing
Guess your issue is here:
@format = options[:format]
@basename = File.basename(@file.path, @current_format)
temp_file = Tempfile.new([@basename, @format].compact.join("."))
来源:https://stackoverflow.com/questions/20225589/paperclip-custom-processor-not-changing-image-type