Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PDF to image conversion timeout #53

Closed
xevix opened this issue Mar 17, 2016 · 2 comments
Closed

PDF to image conversion timeout #53

xevix opened this issue Mar 17, 2016 · 2 comments

Comments

@xevix
Copy link

xevix commented Mar 17, 2016

I've managed to get PDF conversion of the first page to an image to work with the following in process.

require "image_processing/mini_magick"

class ImageUploader < Shrine
  MAX_IMAGE_SIZE_MB = 50

  include ImageProcessing::MiniMagick

  plugin :determine_mime_type
  plugin :remove_attachment
  plugin :store_dimensions
  plugin :validation_helpers
  plugin :versions, names: [:original, :thumb, :large]
  plugin(:default_url) { |_| '/img/preview-not-available.jpg' }

  Attacher.validate do
    validate_max_size MAX_IMAGE_SIZE_MB.megabytes, message: "is too large (max is #{MAX_IMAGE_SIZE_MB} MB)"
    validate_mime_type_inclusion ['image/jpeg', 'image/png', 'image/gif', 'application/pdf']
  end

  def process(io, context)
    case context[:phase]
      when :store
        if io.mime_type == 'application/pdf'
          # NOTE: `convert!` calls `format` which defaults to copying page 0 only
          png_file = convert!(io.download, "png")
          thumb = resize_to_limit(png_file, 200, 200)
        else
          thumb = resize_to_limit!(large, 200, 200)
        end

        {original: io, thumb: thumb}
    end
  end
end

However, for a relatively larger PDF (600+ pages), this code is timing out on Rails after about 1 minute. Attempting imagemagick manually on the command line works in about 1-2 seconds.

$ convert "emacs.pdf[0]" emacs.png

Am I doing something wrong?

Related to #52 in that it's the same scenario.

PDF: https://www.gnu.org/software/emacs/manual/pdf/emacs.pdf

@janko
Copy link
Member

janko commented Mar 17, 2016

Aha, I think I know where the problem is here. You want to convert only the first page of the PDF, correct? The default behaviour of image_processing is that it converts all the pages. This behaviour is probably strange, especially because MiniMagick's default is that it only converts the first page (the page "0"), so this default behaviour will likely change in the near future (to MiniMagick's).

I've just pushed to image_processing the ability to supply the page number, so if you pull from master you can rewrite it with:

png_file = convert!(io.download, "png", 0)

Alternatively, if you would rather keep the RubyGems version of image_processing, you can write:

png_file = with_minimagick(io.download) { |pdf| pdf.format("png") }

@xevix
Copy link
Author

xevix commented Mar 18, 2016

Ah, I fell for the old "the default of my friend is not the default I give my friend" trap, haha. Yes, I agree that sticking to the first page is a nicer default. For now I'll stick to the RubyGems version code above (which works great). Thanks!

@xevix xevix closed this as completed Mar 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants