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

Version from original file instead of other version #1602

Closed
tomek-railwaymen opened this issue Mar 18, 2015 · 3 comments
Closed

Version from original file instead of other version #1602

tomek-railwaymen opened this issue Mar 18, 2015 · 3 comments
Labels

Comments

@tomek-railwaymen
Copy link

I have the following code in my uploader:

  version :large do
    process :convert_to_png

    def full_filename(for_file = model.pdf.file)
      "large-#{File.basename(for_file, '.pdf')}.png"
    end

    def convert_to_png
      manipulate! do |img|
        img.format 'png' do |cmd|
          cmd.merge! ['-density', '200', '-quality', '100']
        end
        img = yield(img) if block_given?
        img
      end
    end
  end

  version :thumb, from_version: :large do
    process resize_to_fit: [200, 200]

    def full_filename(for_file = model.pdf.file)
      "thumb-#{File.basename(for_file, '.pdf')}.png"
    end
  end

Unfortunately thumb version is created based on original file (always in PDF format). That file is broken because it is PDF as well (instead of PNG, like large version).

I noticed that large version is generated by command like convert /tmp/mini_magick20150318-4571-1ma89u5.pdf[0] -density 200 -quality 100 /tmp/mini_magick20150318-4571-ifujor.png and thumb uses PDF file: mogrify -resize 200x200 /tmp/mini_magick20150318-4571-5or0in.pdf

@cipater
Copy link

cipater commented Sep 16, 2015

+1 from_version doesn't appear to be working. Which is to say that all versions look to be built off the original rather than the version specified by from_version.

Given

  process resize_to_limit: [1600, 1600]
  process :store_geometry

  version :optimized do
    process :pngquant
    process :optimize
    process :store_geometry
  end

  version :large, from_version: :optimized do
    process resize_to_limit: [1400, 1400]
    process :store_geometry
  end

The original file is 2.5MB. The optimized version is 900KB. And the large version is 1.8MB.

If the large version were in fact built from the optimized version, I would expect it to be smaller than 900KB, not twice the size.

@cianooooo
Copy link

cianooooo commented Jun 2, 2017

Any updates on this? I'm still having this issue on v1.1.0

When uploading a PDF and wanting multiple PNG thumbnails, it would be nice if this code would work;

 version :thumb_512, if: :thumbnailable? do
    process efficient_conversion: [512, 512]
    def full_filename (for_file = model.attached_file.file)
      super.chomp(File.extname(super)) + '.png'
    end
  end

  version :thumb_256, from_version: :thumb_512 do
    process resize_to_fit: [256, 256]
  end

  protected

  def efficient_conversion(width, height)
    manipulate! do |img|
      img.format('png')
      img.combine_options do |c|
        c.fuzz      "3%"
        c.trim
        c.resize    "#{width}x#{height}>"
        c.resize    "#{width}x#{height}<"
      end
      img
    end
  end

  def thumbnailable?(file)
    file.content_type.start_with?('image') || file.content_type == 'application/pdf'
  end

:thumb_512 will create a 512x512 PNG file of the cover page of the PDF with the appropriate file extension thanks to the naming block.

What I would expect to happen with :thumb256 is that it would process the 512x512 PNG with .png file extension. Instead what it does using this block of code is generate a PDF, ie. it looks like it's working on the original file, rather than the :thumb_512 version.

So what I end up having to do is do multiple conversions of PDF to PNG for each thumbnail I want, and repeat the full_filename method in each version. Needless to say, the resulting code looks unnecessarily repetitive as well as generating an unnecessary load on the server due to the multiple conversions, rather than just one.

@mshibuya
Copy link
Member

Fixed by #1879.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants