代码之家  ›  专栏  ›  技术社区  ›  Charles Smith

神龛制作模糊的PDF文件上传

  •  0
  • Charles Smith  · 技术社区  · 7 年前

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

    初始值设定项/圣地.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
    

    图像上载程序.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
    

    产品/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 %>
    ... 
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Charles Smith    7 年前

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