Skip to content

Instantly share code, notes, and snippets.

@bbasseri
Created April 30, 2012 15:38
Show Gist options
  • Save bbasseri/2559394 to your computer and use it in GitHub Desktop.
Save bbasseri/2559394 to your computer and use it in GitHub Desktop.
PDF to pngs
# encoding: utf-8
class ImageUploader < CarrierWave::Uploader::Base
attr_reader :file
include CarrierWave::RMagick
include Magick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :profile do
process :resize_to_fit => [200,300]
end
version :thumb do
if :pdf?
process :converting => :file
end
process :resize_to_fill => [400,500]
end
protected
def pdf?(new_image)
if current_path.include? 'pdf'
puts "it is pdf"
true
else
puts "it is not pdf"
false
end
end
def converting(new_image)
puts "Im in converting"
i = 0
manipulate! do |image|
pdf = ImageList.new image.filename
jpg = pdf[i]
puts "model id is#{model.id}"
@page = Page.create
jpg.write("#{Rails.root}/public/uploads/page/#{mounted_as}/#{@page.id}/p#{i+1}.png")
@page.image = "#{Rails.root}/public/uploads/page/#{mounted_as}/#{@page.id}/p#{i+1}.png"
@page.save
i = i + 1
jpg
end
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
# version :thumb do
# process :scale => [50, 50]
# end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_white_list
# %w(jpg jpeg gif png)
# end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end
@ibrahima
Copy link

ibrahima commented May 2, 2012

Hmm, I tried to simplify this a little bit, because manipulate! actually gives you the individual frame. See https://gist.github.com/2576703/ebf14152ad47a51ef535385cea2b573e8d0e2519 (I'm bad at forking gists lol).

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