在寻找相同问题的解决方案时,我遇到了这篇文章。当您将 pdf 转换为 jpeg 时,它会创建一个长 pdf,其中所有页面都首尾相连,因此您需要将图像裁剪为所需的纵横比并丢弃其余部分。以下是我最终使用的:
version :thumb_safari do #special version for safari and ios
process :resize_to_fit => [200,200]
process :convert => 'jpg'
process :paper_shape
def full_filename (for_file = model.logo.file)
super.chomp(File.extname(super)) + '.jpg'
end
end
version :thumb do #all browsers except safari
process :resize_to_fit => [200,200]
process :convert => 'jpg' #must convert to jpg before running paper shape
process :paper_shape
process :convert => 'jpg' #after running paper_shape it will default to original file type
def full_filename (for_file = model.logo.file)
super.chomp(File.extname(super)) + '.jpg'
end
end
def paper_shape
manipulate! do |img|
if img.rows*4 != img.columns*3
width=img.columns
height=img.columns/3*4
img.crop!(0,0,width,height,true)
else
img
end
end
end
在控制器/视图中,我使用了 useragent gem 并这样做了:
文档控制器.rb
def index
@user_agent=UserAgent.parse(request.user_agent)
@search = Document.search(params[:q])
end
index.html.rb
<% if @user_agent.browser.downcase == 'safari' %>
<%= link_to(image_tag(doc.pdfdoc_url(:thumb_safari).to_s, :class=>"dropshadow", :size => "150x225"), doc.pdfdoc_url)%>
<% else %>
<%= link_to(image_tag(doc.pdfdoc_url(:thumb).to_s, :class=>"dropshadow", :size => "150x225"), doc.pdfdoc_url)%>
<% end %>
毫无疑问,有一种更好的方法可以做到这一点,但目前正在奏效。