Created
August 6, 2013 15:12
-
-
Save RoryMacDonald/6165380 to your computer and use it in GitHub Desktop.
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
def generate_product(product = nil, product_type = nil, s3_bucket) | |
sFramePath = layers_path + "displace/" | |
b_lampshade = false | |
if File.directory? sFramePath | |
b_lampshade = true | |
zoomPxOffsetX = zoomPxOffsetX + (setupPxOffsetX2/4) | |
zoomPxOffsetX2 = zoomPxOffsetX2/2 | |
end | |
read_image_path = "http:" + "//s3-eu-west-1.amazonaws.com/surfaceview" + "/images/" + collection_item.id.to_s + "/original/" + collection_item.image_file_name | |
Delayed::Worker.logger.info "[product_job::generate_product] Read Image: " + read_image_path | |
#####img = Magick::Image.read(File.expand_path(Rails.root.to_s + "/public/system/images/" + collection_item.id.to_s + "/original/" + collection_item.image_file_name)).first | |
img = Magick::ImageList.new | |
urlimage = open(read_image_path) | |
img.from_blob(urlimage.read) | |
if product_type.nil? or (!product_type.nil? and product_type.short_code != "CO") | |
img = img.crop(zoomPxOffsetX.to_i, zoomPxOffsetY.to_i, zoomPxOffsetX2.to_i, zoomPxOffsetY2.to_i) | |
end | |
content_width = img.columns | |
content_height = img.rows | |
Delayed::Worker.logger.info "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Cropper: end" | |
# Set scale | |
if File.exists? layers_path + "scale.txt" | |
mmpx = File.read(layers_path + "scale.txt") | |
mmpx = mmpx.to_f | |
if b_lampshade | |
img = img.resize((mmWidth/2)*mmpx, mmHeight*mmpx) | |
elsif product_type.nil? or (!product_type.nil? and product_type.short_code != "CO") | |
img = img.resize(mmWidth*mmpx, mmHeight*mmpx) | |
end | |
if (product_type.nil? and product.product_type.short_code == "CO") or (!product_type.nil? and product_type.short_code == "CO") | |
new_img = Magick::Image.new(mmWidthRef*mmpx, mmHeightRef*mmpx) do | |
self.background_color = 'transparent' | |
end | |
img = new_img.composite(img, Magick::CenterGravity, Magick::CopyCompositeOp) | |
end | |
content_width = img.columns | |
content_height = img.rows | |
end | |
Delayed::Worker.logger.info "[product_job::generate_product] scale (scale.txt): " + mmpx.inspect | |
Delayed::Worker.logger.info "[product_job::generate_product] content_width: " + content_width.inspect | |
Delayed::Worker.logger.info "[product_job::generate_product] content_height: " + content_height.inspect | |
if product_type.nil? or (!product_type.nil? and product_type.short_code != "AM" and product_type.short_code != "M" and product_type.short_code != "CO") | |
# Multiply (In) | |
sFramePath = layers_path + "multiply_in/" | |
if File.directory? sFramePath | |
img = frame_builder("multiply", img, sFramePath, sProductPath, content_width, content_height) | |
end | |
# Mask | |
sFramePath = layers_path + "mask/" | |
if File.directory? sFramePath | |
img = frame_builder("mask", img, sFramePath, sProductPath, content_width, content_height) | |
end | |
# Opaque frame | |
sFramePath = layers_path + "opaque/" | |
if File.directory? sFramePath | |
img = frame_builder("opaque", img, sFramePath, sProductPath, content_width, content_height) | |
end | |
# Displacement Map | |
sFramePath = layers_path + "displace/" | |
if File.directory? sFramePath | |
disp_map = Magick::Image.new(content_width, content_height) { self.background_color = '#808080' } | |
disp_map = frame_builder("opaque", disp_map, sFramePath, sProductPath, content_width, content_height) | |
#disp_map = overlay_builder("displace", disp_map, sFramePath, sProductPath, content_width, content_height) | |
#disp_map.write(config.file_import_path_temp + @count.to_s + "-displace.png") | |
img = img.displace(disp_map, content_width/2.7, 0) | |
content_width = img.columns-((content_width/6)+(content_width/6)) | |
img = img.crop((content_width/4), 0, content_width, content_height) | |
end | |
# Overlay (In) | |
sFramePath = layers_path + "overlay_in/" | |
if File.directory? sFramePath | |
img = overlay_builder("in", img, sFramePath, sProductPath, content_width, content_height) | |
end | |
# Tiles | |
sFramePath = layers_path + "tiles/" | |
b_ceramic_tiles = false | |
if File.directory? sFramePath | |
b_ceramic_tiles = true | |
mTile = mmpx * 100 | |
numRows = (mmWidth / mTile).to_i | |
numColumns = (mmHeight / mTile).to_i | |
content_width = numRows*100 | |
content_height = numColumns*100 | |
img = img.resize(content_width, content_height) | |
img = tile_builder(img, sFramePath, sProductPath, content_width, content_height, numRows, numColumns) | |
end | |
# Frame | |
sFramePath = layers_path + "frame/" | |
if File.directory? sFramePath | |
img = frame_builder("frame", img, sFramePath, sProductPath, content_width, content_height) | |
end | |
# Multiply (Out) | |
sFramePath = layers_path + "multiply_out/" | |
if File.directory? sFramePath | |
img = frame_builder("multiply", img, sFramePath, sProductPath, img.columns, img.rows) | |
end | |
# Overlay (Out) | |
sFramePath = layers_path + "overlay_out/" | |
if File.directory? sFramePath | |
img = overlay_builder("full", img, sFramePath, sProductPath, img.columns, img.rows) | |
end | |
# Random | |
if product_type.nil? | |
sFramePath = layers_path + "random/" | |
if File.directory? sFramePath | |
count_random = 0 | |
for i in 1..9 | |
if File.directory? sFramePath + i.to_s + "/" | |
count_random += 1 | |
end | |
end | |
if count_random > 1 | |
r = 1+rand(count_random-1) | |
else | |
r = 1 | |
end | |
img = frame_builder("opaque", img, sFramePath + r.to_s + "/", sProductPath, img.columns, img.rows) | |
end | |
end | |
end | |
# Generate final image and thumbnail | |
Delayed::Worker.logger.info "[product_job::generate_product] Generate final image and thumbnail..." | |
img = img.trim(false) | |
if product_type.nil? | |
filename = "product.png" | |
#####img.write(sProductPath + 'original/' + filename) | |
Delayed::Worker.logger.info "[product_job::generate_product] Write Image: " + sProductPath + 'original/' + filename | |
img.format = "png" | |
s3_file = s3_bucket.objects[sProductPath + 'original/' + filename] | |
s3_file.write(img.to_blob, { :acl => :public_read }) | |
else | |
filename = "playroom.png" | |
playroomWidth = (3000*mmpx) + (img.columns-content_width) | |
playroomHeight = (2000*mmpx) + (img.rows-content_height) | |
playroom = Magick::Image.new(playroomWidth, playroomHeight) do | |
self.background_color = 'transparent' | |
end | |
playroom = playroom.composite(img, Magick::CenterGravity, Magick::CopyCompositeOp) | |
playroom.change_geometry!(IMAGE_SIZES['product_playroom']) { |cols, rows, playroom| | |
playroom = playroom.resize(cols, rows) | |
playroom = playroom.trim(false) | |
#####playroom.write(sProductPath + 'original/' + filename) | |
Delayed::Worker.logger.info "[product_job::generate_product] Write Image: " + sProductPath + 'original/' + filename | |
playroom.format = "png" | |
s3_file = s3_bucket.objects[sProductPath + 'original/' + filename] | |
s3_file.write(playroom.to_blob, { :acl => :public_read }) | |
} | |
end | |
if product_type.nil? | |
#Extra Images for Products only | |
Delayed::Worker.logger.info "[product_job::generate_product] Extra Images for Products only..." | |
thumb = img.change_geometry!(IMAGE_SIZES['product_thumbnail']) { |cols, rows, img| | |
thumb = img.resize(cols, rows) | |
#####thumb.write(sProductPath + 'thumb/' + filename) | |
Delayed::Worker.logger.info "[product_job::generate_product] Write Image: " + sProductPath + 'thumb/' + filename | |
thumb.format = "png" | |
s3_file = s3_bucket.objects[sProductPath + 'thumb/' + filename] | |
s3_file.write(thumb.to_blob, { :acl => :public_read }) | |
} | |
# Generate JPEG from PNG | |
original_img_path = sProductPath + 'original/' + filename | |
thumbnail_img_path = sProductPath + 'thumb/' + filename | |
thumbnail_jpg_img_path = thumbnail_img_path.gsub('png','jpg') | |
######Delayed::Worker.logger.info ">>> convert '#{original_img_path}' -background '#E9E9E9' -flatten -trim -resize '151x' '#{thumbnail_jpg_img_path}'" | |
#system "convert '#{original_img_path}' -background '#E9E9E9' -flatten '#{thumbnail_jpg_img_path}'" | |
#####system "convert '#{original_img_path}' -background '#E9E9E9' -flatten -trim -resize '151x' '#{thumbnail_jpg_img_path}'" | |
original_temp_path = Rails.root.join('tmp', 'product_job').to_s + "/" + Time.new.to_time.to_i.to_s + '/original/' | |
thumb_temp_path = Rails.root.join('tmp', 'product_job').to_s + "/" + Time.new.to_time.to_i.to_s + '/thumb/' | |
original_temp_file = original_temp_path + filename | |
thumb_temp_file = thumb_temp_path + filename.gsub('png','jpg') | |
FileUtils.mkdir_p original_temp_path | |
FileUtils.mkdir_p thumb_temp_path | |
original_temp = Magick::ImageList.new | |
urlimage = open("http:" + "//s3-eu-west-1.amazonaws.com/surfaceview" + "/" + original_img_path) | |
original_temp.from_blob(urlimage.read) | |
original_temp.write(original_temp_file) | |
Delayed::Worker.logger.info "[product_job::generate_product] convert '#{original_temp_file}' -background '#E9E9E9' -flatten -trim -resize '151x' '#{thumb_temp_file}'" | |
system "convert '#{original_temp_file}' -background '#E9E9E9' -flatten -trim -resize '151x' '#{thumb_temp_file}'" | |
thumb_temp_file = Magick::Image.read(thumb_temp_file).first | |
Delayed::Worker.logger.info "[product_job::generate_product] Write Image: " + thumbnail_jpg_img_path | |
thumb_temp_file.format = "jpg" | |
s3_file = s3_bucket.objects[thumbnail_jpg_img_path] | |
s3_file.write(thumb_temp_file.to_blob, { :acl => :public_read }) | |
FileUtils.rm_rf(original_temp_path) | |
FileUtils.rm_rf(thumb_temp_path) | |
#jpeg_img = Magick::Image.read(thumbnail_jpg_img_path).first | |
#jpeg_img = jpeg_img.trim() | |
#thumb_jpg = jpeg_img.change_geometry!(IMAGE_SIZES['product_thumbnail']) { |cols, rows, img| | |
# thumb_jpg = jpeg_img.resize(cols, rows) | |
# thumb_jpg.write(thumbnail_jpg_img_path) | |
#} | |
selected = img.change_geometry!(IMAGE_SIZES['product_selected']) { |cols, rows, img| | |
selected = img.resize(cols, rows) | |
#####selected.write(sProductPath + 'selected/' + filename) | |
Delayed::Worker.logger.info "[product_job::generate_product] Write Image: " + sProductPath + 'selected/' + filename | |
selected.format = "png" | |
s3_file = s3_bucket.objects[sProductPath + 'selected/' + filename] | |
s3_file.write(selected.to_blob, { :acl => :public_read }) | |
} | |
checkout = img.change_geometry!(IMAGE_SIZES['product_checkout']) { |cols, rows, img| | |
checkout = img.resize(cols, rows) | |
#####checkout.write(sProductPath + 'checkout/' + filename) | |
Delayed::Worker.logger.info "[product_job::generate_product] Write Image: " + sProductPath + 'checkout/' + filename | |
checkout.format = "png" | |
s3_file = s3_bucket.objects[sProductPath + 'checkout/' + filename] | |
s3_file.write(checkout.to_blob, { :acl => :public_read }) | |
} | |
#Scenario | |
if b_ceramic_tiles | |
img.change_geometry!(IMAGE_SIZES['product_scenario']) { |cols, rows, img| | |
thumb = img.resize(cols, rows) | |
#####thumb.write(sProductPath + 'scenario/' + filename) | |
Delayed::Worker.logger.info "[product_job::generate_product] Write Image: " + sProductPath + 'scenario/' + filename | |
thumb.format = "png" | |
s3_file = s3_bucket.objects[sProductPath + 'scenario/' + filename] | |
s3_file.write(thumb.to_blob, { :acl => :public_read }) | |
} | |
else | |
scenarioWidth = (mmWidthRef*mmpx) + (img.columns-content_width) | |
scenarioHeight = (mmHeightRef*mmpx) + (img.rows-content_height) | |
scenario = Magick::Image.new(scenarioWidth, scenarioHeight) do | |
self.background_color = 'transparent' | |
end | |
scenario = scenario.composite(img, Magick::CenterGravity, Magick::CopyCompositeOp) | |
scenario.change_geometry!(IMAGE_SIZES['product_scenario']) { |cols, rows, scenario| | |
scenario = scenario.resize(cols, rows) | |
scenario = scenario.trim(false) | |
######scenario.write(sProductPath + 'scenario/' + filename) | |
Delayed::Worker.logger.info "[product_job::generate_product] Write Image: " + sProductPath + 'scenario/' + filename | |
scenario.format = "png" | |
s3_file = s3_bucket.objects[sProductPath + 'scenario/' + filename] | |
s3_file.write(scenario.to_blob, { :acl => :public_read }) | |
} | |
end | |
end | |
rescue Exception => e | |
Delayed::Worker.logger.info "[product_job::generate_product] inspect: " + e.inspect | |
Delayed::Worker.logger.info "[product_job::generate_product] exception: " + e.to_s | |
return false | |
end | |
return true | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment