Skip to content

Instantly share code, notes, and snippets.

@yortz
Created December 1, 2010 17:57
Show Gist options
  • Save yortz/723906 to your computer and use it in GitHub Desktop.
Save yortz/723906 to your computer and use it in GitHub Desktop.
image_uploader.rb
# encoding: utf-8
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
def store_dir
if "#{version_name}" == ""
"images/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}/#{model.name_file_name}"
else
"images/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}/#{version_name}_#{model.name_file_name}"
end
end
def cache_dir
"#{RAILS_ROOT}/public/images/#{model.class.to_s.underscore}/#{mounted_as}"
end
def default_url
"/images/no_image_#{version_name}.png"
end
# Create different versions of your uploaded files
process :resize_to_fit => [800, 600]
process :optimize_image
process :watermark => "#{RAILS_ROOT}/public/images/neonisi-watermark.png"
version :thumb do
process :resize_to_fit => [80, 80]
process :optimize_image
end
version :medium do
process :resize_to_fit => [160, 160]
process :optimize_image
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
def filename
return nil if @original_filename.blank?
name = model["temp_#{self.mounted_as}_name"]
name.present? ? name : renaming
end
private
def renaming
# store the image name based on random words in the item_detail title
if model.item_detail.present?
model.splitted_title ||= model.item_detail.title.split(/[^-a-zA-Z0-9]/).delete_if{ |w| w == '' or w == '-' }.map!{ |w| w + '-' }
words = model.splitted_title.sort_by { rand }
model["temp_#{self.mounted_as}_name"] = words[0..4].to_s[0..-2] + '.jpg'
else
item_detail = ItemDetail.find_by_internal_id(model.temporary_file_name)
model.splitted_title ||= item_detail.title.split(/[^-a-zA-Z0-9]/).delete_if{ |w| w == '' or w == '-' }.map!{ |w| w + '-' }
words = model.splitted_title.sort_by { rand }
newname = words[0..4].to_s[0..-2] + '.jpg'
model["temp_#{self.mounted_as}_name"] = newname
model.name_file_name = newname
model.name_content_type = 'jpg'
end
end
def watermark(path_to_file)
manipulate! do |img|
img = img.composite(MiniMagick::Image.open(path_to_file), "jpg") do |c|
c.gravity "SouthEast"
end
end
end
def optimize_image
manipulate! do |img|
img.format('JPEG')
img = img.auto_orient
img = img.quality 80
img
end
end
end
require 'uuid'
class ItemsController < ApplicationController
after_filter :rename_files_and_cache, :only => [:create]
def rename_files_and_cache
@item.images.each do |image|
dir = image.name.cache_dir + "/" + image.name_cache_name.gsub(/(\/.*)/, "")
$images = Dir.entries(dir)
File.rename(dir + "/" + $images[2], dir + "/" + "#{image.name_file_name}")
File.rename(dir + "/" + $images[3], dir + "/" + "medium_#{image.name_file_name}")
File.rename(dir + "/" + $images[4], dir + "/" + "thumb_#{image.name_file_name}")
directory = image.name_cache_name.gsub(/(\/.*)/, "")
new_cache_name = "/" + directory + "/" + image.name_file_name
image.update_attribute("name_new_cache_name", new_cache_name)
end
end
def new
@uuid = UUID.new
@temp_name = @uuid.generate(:compact).object_id
@item = Item.new
@item.build_item_detail
@item.build_dropship_item if @dropship
@item.clone_from_other_item(params[:i]) if params[:i]
@item.set_from_user(current_user)
@item.clone_from_template(params[:t]) if params[:t] and !params[:i]
5.times { @item.images.build }
end
def create
@item = Item.new(params[:item])
unless @dropship
@item.user = current_user
else
@item.dropship_item_attributes = params[:item][:dropship_item_attributes]
@item.synchronized_shop_id = params[:item][:synchronized_shop_id]
end
if @item.save
redirect_to_with(:notice, @dropship ? dropship_items_url : item_url(current_language, current_country, @item), Item.successfully_created)
else
render 'new'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment