Created
November 30, 2012 16:59
-
-
Save bhaity/4176994 to your computer and use it in GitHub Desktop.
zip
This file contains hidden or 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 zip(hash) | |
#this method takes a hash in the format {FIRST_PHOTO_ID => {:dpi=>INT, | |
# :watermark=>BOOL}, | |
# SECOND_PHOTO_ID => {:dpi=>INT, | |
# :watermark=>BOOL} } | |
require 'zip/zip' | |
#new_hash_of_photos = {} | |
hash.each do |id, specs| | |
p = Photo.find_by_id(id) | |
specs[:path] = p.image.url | |
#new_hash_of_photos[p.image.url] = specs #.create_whitebar.filename | |
end | |
puts "*****#{hash}" | |
temp_path = Dir.mktmpdir + "/" | |
%x[mv #{temp_path} #{Rails.root.join('public/uploads/')}] | |
temp_path = Rails.root.join('public/uploads/') + temp_path.split('/').last | |
zip_path = "#{temp_path}/Order_#{self.id.to_s}_BFA.zip" | |
Zip::ZipFile.open(zip_path, Zip::ZipFile::CREATE) do |zipfile| | |
hash.each do |id, specs| | |
photo_filename = specs[:path].split('/').last #/uploads/BFA_1234_123456.jpg -> BFA_1234_123456.jpg | |
current_path_to_file = "#{Rails.root.join('public/uploads/')}#{photo_filename}" | |
new_path_to_file = "#{Rails.root.join('public/uploads/')}purchased2_#{photo_filename}" | |
#%x[gm convert -resize #{specs[:dpi]}x#{specs[:dpi]} #{current_path_to_file} #{new_path_to_file}] | |
##puts "%x[convert -units PixelsPerInch #{current_path_to_file} -density #{specs[:dpi]} #{new_path_to_file}]" | |
%x[convert -units PixelsPerInch #{current_path_to_file} -density #{specs[:dpi]} #{new_path_to_file}] | |
puts ">>>>>>>>#{current_path_to_file}" | |
puts ">>>>>>>>#{new_path_to_file}" | |
newer_path_to_file = apply_watermark_to_img(new_path_to_file, id, specs[:watermark]) | |
zipfile.add(photo_filename, newer_path_to_file) {true} #.add(zip_entry_name, path_to_file) | |
end | |
end | |
##Zipfile created. Move to appropriate folder. | |
%x[mv #{zip_path} #{Rails.root.join('public/uploads/zip/')}] | |
%x[rm #{temp_path}] | |
return "#{Rails.root.join('public/uploads/zip/')}Order_#{self.id.to_s}_BFA.zip" #do sendfile on this path | |
end | |
def apply_watermark_to_img(photo_path, id, watermark) | |
puts ">>>>#{photo_path}" | |
photo = Magick::Image.read(photo_path).first | |
if watermark == true | |
watermark_img = "#{Rails.public_path[0..-7]}"+"app/assets/images/watermark13.png" | |
ptsize = 32 | |
bottomOffset = 70 | |
anno1 = -5 | |
anno2 = 30 | |
watermark_img.annotate(txt, 0,0,5,anno1, "bfa.co/"){ | |
txt.gravity = Magick::WestGravity | |
txt.pointsize = ptsize | |
txt.fill = "white" | |
txt.font = bfa_font | |
} | |
watermark_img.annotate(txt, 0,0,5,anno2, "#{id}"){ | |
txt.gravity = Magick::WestGravity | |
txt.pointsize = ptsize | |
txt.fill = "white" | |
txt.font = bfa_font | |
} | |
result = photo.composite(watermark_img, Magick::SouthEastGravity, 0, bottomOffset, Magick::OverCompositeOp) | |
else | |
result = photo | |
end | |
new_path = "#{Rails.root.join('public/uploads/')}watermarked_#{photo_path.split('/').last}" | |
result.write("#{new_path}") | |
puts "newpath:#{new_path}" | |
return new_path | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment