【问题标题】:Shrinerb producing blurry PDF file uploadsShrinerb 产生模糊的 PDF 文件上传
【发布时间】:2019-01-11 23:25:10
【问题描述】:

我已搜索但找不到任何有关使用Shrine 上传 pdf 文件的信息。我有它的工作,但 pdf 模糊和像素化,即使引用 (:original) 版本。

initializers/shrine.rb

require 'shrine'
require 'shrine/storage/file_system'

Shrine.storages = {
    cache: Shrine::Storage::FileSystem.new('public', prefix: 'uploads/cache'),
    store: Shrine::Storage::FileSystem.new('public', prefix: 'uploads/store')
}

Shrine.plugin :activerecord
Shrine.plugin :backgrounding
Shrine.plugin :remove_attachment
Shrine.plugin :logging
Shrine.plugin :delete_raw
Shrine.plugin :upload_endpoint
Shrine.plugin :cached_attachment_data
Shrine.plugin :restore_cached_data
Shrine.plugin :determine_mime_type

image_uploader.rb

require 'image_processing/mini_magick'
class ImageUploader < Shrine
  ALLOWED_TYPES = %w[image/jpeg image/jpg image/png image/gif application/pdf]
  MAX_SIZE = 50
  include ImageProcessing::MiniMagick

  plugin :remove_attachment
  plugin :pretty_location
  plugin :processing
  plugin :versions
  plugin :validation_helpers
  plugin :store_dimensions
  plugin(:default_url) { |_| '/img/preview-not-available.jpg' }

  Attacher.validate do
    validate_max_size MAX_SIZE.megabytes, message: "is too large (max is #{MAX_SIZE} MB)"
    validate_mime_type_inclusion ALLOWED_TYPES
  end

  process(:store) do |io|
    original = io.download

    size_1500 = resize_to_limit!(original, 1500, 600)
    size_500 = resize_to_limit(size_1500, 500, 500)
    size_300 = resize_to_limit(size_500, 300, 300)

    { original: size_1500, medium: size_500, thumb: size_300 }
  end
end

products/show.html.erb

...
<% if @product.spec_sheet.present? %>
  <div class="col-md-12">
    <%#= image_tag(@product.spec_sheet_cover_url(:original), class: 'border') %>
    <%= link_to @product.spec_sheet_url(:original), :class => 'btn', style: 'width: auto' do %>
          <span><%= image_tag 'pdf-icon.png', style: 'width: 10%;
              position: relative;
              right: 3px;
              bottom: 1px;' %> Collection Brochure</span>
    <% end %>
  </div>
<% end %>
... 

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5 shrine


    【解决方案1】:

    我刚刚发现自己的问题……一直摆在我面前。通过引用:original,我选择了调整大小的版本。但是通过重构这一行,它使:original 大小成为全分辨率。 { original: io, large: size_1500, medium: size_500, thumb: size_300 }

    【讨论】:

    • 我建议从原始 PDF 生成每个缩略图,而不是使用较大的版本来生成较小的版本。每次调整大小都会产生略微模糊的结果,在链接多个调整大小时确实会加起来。我曾一度意识到这一点,并开始在文档中提出反对意见。请注意,ImageProcessing 1.0 在调整大小后开始自动锐化缩略图。因此,如果您看到缩略图明显模糊,我建议您升级 ImageProcessing gem(尽管注意 API 的重大更改)。
    猜你喜欢
    • 2019-05-22
    • 2019-11-27
    • 1970-01-01
    • 2013-11-15
    • 2019-11-06
    • 2012-09-15
    • 2016-10-18
    • 1970-01-01
    • 2021-12-27
    相关资源
    最近更新 更多