require ‘RMagick’
include Magick
…
result = Magick::Image.new(1000, 725){ self.background_color = ‘#FFFFFF’ }
watermark = Magick::Image.read(“#{RAILS_ROOT}/public/logo/watermark.png”).first
logo = Magick::Image.read(“#{RAILS_ROOT}/public/logo/logo_xxx.png”).first
signature1 = Magick::Image.read(“#{RAILS_ROOT}/public/logo/sig1.png”).first
signature2 = Magick::Image.read(“#{RAILS_ROOT}/public/logo/sig2.png”).first
student_img = Magick::Image.read(“#{RAILS_ROOT}/public/images/gui/user_90x90.png”).first
result = result.composite(watermark, Magick::CenterGravity, 0, 0, Magick::OverCompositeOp)
result = result.composite(logo, Magick::NorthGravity, 0, 20, Magick::OverCompositeOp)
result = result.composite(signature1, Magick::NorthGravity, -250, 600, Magick::OverCompositeOp)
result = result.composite(signature2, Magick::NorthGravity, 250, 600, Magick::OverCompositeOp)
result = result.composite(student_img, Magick::NorthGravity, 345, 50, Magick::OverCompositeOp)
line1 = Draw.new
result.annotate(line1, 1000,30, 0, 200, “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”){
line1.font = “#{RAILS_ROOT}/public/fonts/TH_Niramit_AS_Bold.ttf”
line1.gravity = Magick::NorthGravity
line1.pointsize = 18
line1.fill = “#000000″
line1.font_weight = Magick::BoldWeight
}
line2 = Draw.new
result.annotate(line2, 1000,30, 0, 240, “วุฒิบัตรฉบับนี้ให้ไว้เพื่อแสดงว่า”){
line2.font = “#{RAILS_ROOT}/public/fonts/TH_Niramit_AS_Bold.ttf”
line2.gravity = Magick::NorthGravity
line2.pointsize = 18
line2.fill = “#000000″
line2.font_weight = Magick::BoldWeight
}
student_name = Draw.new
result.annotate(student_name, 1000,30, 0, 280, “#{courses_student.prefix} #{courses_student.firstname} #{courses_student.lastname}”){
student_name.font = “#{RAILS_ROOT}/public/fonts/TH_Niramit_AS_Bold.ttf”
student_name.gravity = Magick::NorthGravity
student_name.pointsize = 18
student_name.fill = “#000000″
student_name.font_weight = Magick::BoldWeight
}
detail = Draw.new
result.annotate(detail, 1000,30, 0, 340, “ได้เข้าฝึกอบรม #{certificate_template.detail}”){
detail.font = “#{RAILS_ROOT}/public/fonts/TH_Niramit_AS_Bold.ttf”
detail.gravity = Magick::NorthGravity
detail.pointsize = 18
detail.fill = “#000000″
detail.font_weight = Magick::BoldWeight
}
if courses_student.is_certified
detail2 = Draw.new
result.annotate(detail, 1000,30, 0, 370, “และผ่านเกณฑ์การประเมินผลสัมฤทธิ์การอบรม”){
detail2.font = “#{RAILS_ROOT}/public/fonts/TH_Niramit_AS_Bold.ttf”
detail2.gravity = Magick::NorthGravity
detail2.pointsize = 18
detail2.fill = “#000000″
detail2.font_weight = Magick::BoldWeight
}
end
line3 = Draw.new
result.annotate(line3, 1000,30, 0, 4100, “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”){
line3.font = “#{RAILS_ROOT}/public/fonts/TH_Niramit_AS_Bold.ttf”
line3.gravity = Magick::NorthGravity
line3.pointsize = 18
line3.fill = “#000000″
line3.font_weight = Magick::BoldWeight
}
line4 = Draw.new
result.annotate(line4, 1000,30, 0, 440, “xxxxxxxxxxxxxxxxxxxxx”){
line4.font = “#{RAILS_ROOT}/public/fonts/TH_Niramit_AS_Bold.ttf”
line4.gravity = Magick::NorthGravity
line4.pointsize = 18
line4.fill = “#000000″
line4.font_weight = Magick::BoldWeight
}
line5 = Draw.new
result.annotate(line5, 1000,30, 0, 470, “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx “){
line5.font = “#{RAILS_ROOT}/public/fonts/TH_Niramit_AS_Bold.ttf”
line5.gravity = Magick::NorthGravity
line5.pointsize = 18
line5.fill = “#000000″
line5.font_weight = Magick::BoldWeight
}
line6 = Draw.new
result.annotate(line6, 1000,30, 0, 500, “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”){
line6.font = “#{RAILS_ROOT}/public/fonts/TH_Niramit_AS_Bold.ttf”
line6.gravity = Magick::NorthGravity
line6.pointsize = 18
line6.fill = “#000000″
line6.font_weight = Magick::BoldWeight
}
thai_mon = ["", "มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม" "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม"]
date = Draw.new
result.annotate(date, 1000,30, 0, 540, “ให้ไว้ ณ วันที่ #{Date.today.day} เดือน #{thai_mon[Date.today.mon]} พ.ศ. #{Date.today.year + 543}”){
date.font = “#{RAILS_ROOT}/public/fonts/TH_Niramit_AS_Bold.ttf”
date.gravity = Magick::NorthGravity
date.pointsize = 18
date.fill = “#000000″
date.font_weight = Magick::BoldWeight
}
sig2_name = Draw.new
result.annotate(line6, 1000,30, 250, 668, “(xxxxxxxxxxxxxxxxxxxxxxxxxxxxx)”){
sig2_name.font = “#{RAILS_ROOT}/public/fonts/TH_Niramit_AS_Bold.ttf”
sig2_name.gravity = Magick::NorthGravity
sig2_name.pointsize = 18
sig2_name.fill = “#000000″
sig2_name.font_weight = Magick::BoldWeight
}
sig2_desc = Draw.new
result.annotate(line6, 1000,30, 250, 688, “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”){
sig2_desc.font = “#{RAILS_ROOT}/public/fonts/TH_Niramit_AS_Bold.ttf”
sig2_desc.gravity = Magick::NorthGravity
sig2_desc.pointsize = 18
sig2_desc.fill = “#000000″
sig2_desc.font_weight = Magick::BoldWeight
}
filename = “#{directory}/cert_#{@course.id}_#{courses_student.student_id}_#{courses_student.student_id}.jpg”
result.write(filename)