Skip to content

Instantly share code, notes, and snippets.

@ibrahima
Forked from bbasseri/image_uploader.rb
Created May 2, 2012 13:59
Show Gist options
  • Save ibrahima/2576703 to your computer and use it in GitHub Desktop.
Save ibrahima/2576703 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"
manipulate! do |image, index|
puts "model id is#{model.id}"
@page = Page.create
image.write(Rails.root.join('public', store_dir, "p#{index}.png"))
@page.image = Rails.root.join('public', store_dir, "p#{index}.png")
@page.save
image
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
@bbasseri
Copy link

bbasseri commented May 3, 2012

Oh I didn't know I could do that with manipulate. Thanks, changed it in my code works well.

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