Skip to content

Instantly share code, notes, and snippets.

@bbasseri
Created April 28, 2012 09:10
Show Gist options
  • Save bbasseri/2517376 to your computer and use it in GitHub Desktop.
Save bbasseri/2517376 to your computer and use it in GitHub Desktop.
if file uploaded is pdf, then convert each page to a PNG, and store in new models.
include CarrierWave::RMagick
include Magick
storage :file
version :thumb, :if => :pdf? do
process :resize_to_fill => [400,500]
end
version :profile do
# process :convert => 'png'
process :resize_to_fit => [200,300]
end
protected
def pdf?(new_file)
new_file.content_type.include? 'pdf'
converting(new_file)
end
def converting(new_file)
i = 0
manipulate! do |image|
pdf = ImageList.new image.filename
png = pdf[i]
png.write("#{Rails.root}/public/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}/p#{i+1}.png")
@page = Page.create()
@page.presentation_id = "#{model.id}"
@page.image = "p#{i+1}.png" #Need this image_url to be the same location as the original file uploaded (same folder)
@page.save
i = i + 1
png
end
end
@bokor
Copy link

bokor commented May 21, 2012

to make it store in the correct location, I think you have to write to carrier wave cache then run store! and it would store it properly. There are a few articles that I think might help.

https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Detect-a-new-file-in-a-mounted-uploader

https://github.com/jnicklas/carrierwave/wiki/How-to%3A-create-a-thumbnail-with-both-color-and-grayscale-versions-in-one-image

also if you look at how the converts usually work, you have to yield the new object, but you have many. So you might have to look at the carrier wave code to see what the yield sets and you would have to loop through and create those yourself.

def blahblah
      manipulate! do |img|
        img.write(current_path)
        img = yield(img) if block_given?
        img
      end
    end

The issue is your not dealing with a single file after you turn a pdf into multiple files, so it is tricky. I wish I had more time to help but it has been tough finishing up some stuff...hope this helps a bit :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment