我在搜索解決這個問題時跑過這篇文章。當您將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
在控制器/視圖我使用的用戶代理寶石和做:
documents_controller.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 %>
毫無疑問,有一個更好的方法來做到這一點,但目前這是行之有效的。