Last active
August 29, 2015 14:03
-
-
Save f3lan/f48b0de6c6529d445934 to your computer and use it in GitHub Desktop.
webshop_image_converter.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Image Converter for the NILE Webshop 3.0 | |
# | |
# Converts the Name in the new stadard formated Name. | |
# Converts the jpgs to png | |
# | |
class WebshopImageConverter | |
attr_accessor :file, :style_number, :back, :color, :file_type | |
def initialize(file) | |
@file = file | |
@style_number, @back, @color, @file_type = | |
file.match(/([F|S|H|W]\d+[A|B|C]?)(R?)_(.*).(\w{3})/i).captures | |
@style_number.upcase | |
@color.downcase | |
end | |
def convert | |
if file_type =~ /[jpe*g]/i | |
to_png | |
else | |
rename | |
end | |
create_thumbnail if front? | |
clean_up | |
end | |
private | |
def to_png | |
puts "...converted to png #{file} ~> #{new_file_name}.png" | |
system("convert -depth 24 #{file} #{new_file_name}.png") | |
end | |
def rename | |
puts "...renamed #{file} ~> #{new_file_name}.#{file_type}" | |
system("mv #{file} #{new_file_name}.#{file_type}") | |
end | |
def create_thumbnail | |
puts "...created thumbnail #{file} ~> #{style_number}_thumb_#{color}.png" | |
system("convert -resize 231 -depth 24 #{file} #{style_number}_thumb_#{color}.png") | |
end | |
def clean_up | |
puts "...cleanup: #{file}" | |
system("rm #{file}") | |
end | |
def front? | |
back.empty? | |
end | |
def new_file_name | |
if front? | |
return "#{style_number}_front_#{color}" | |
else | |
return "#{style_number}_back_#{color}" | |
end | |
end | |
end | |
puts "...remove 'klein' images" | |
system("rm *klein*") | |
puts "...remove '.gross' out of filename" | |
images = Dir["*.{png,PNG,jpg,jpeg,JPEG, JPG}"] | |
images.each do |image| | |
puts image.gsub(".gross","") | |
system("mv #{image} #{image.gsub(".gross","")}") | |
end | |
images.each do |image| | |
puts "Converts #{image}" | |
converter = WebshopImageConverter.new(image) | |
converter.convert | |
end | |
puts "Converted #{images.count} files." | |
puts "Goodbye!!!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment