| Server IP : 172.67.187.206 / Your IP : 172.71.28.156 Web Server : Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/5.6.30 System : Windows NT WIN-ECQAAA40806 6.2 build 9200 (Windows Server 2012 Standard Edition) i586 User : SYSTEM ( 0) PHP Version : 5.6.30 Disable Function : NONE MySQL : ON | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /Inetpub/www/certificate/ |
Upload File : |
<?php
// ปิดการแสดง Error ทางเทคนิคของ PHP บนหน้าจอ เพื่อไม่ให้รบกวนการแสดงผล
error_reporting(0);
require 'db.php';
// ฟังก์ชันสำหรับแสดงหน้าจอ Error ที่สวยงาม
function showCertError($error_msg = "ไม่สามารถดาวน์โหลดเกียรติบัตรได้<br>กรุณาติดต่อเจ้าหน้าที่") {
echo '<!DOCTYPE html>';
echo '<html lang="th">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ข้อผิดพลาด - ระบบเกียรติบัตร</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
<style>
@import url("https://fonts.googleapis.com/css2?family=Sarabun:wght@300;400;500;600&display=swap");
body { font-family: "Sarabun", sans-serif; background-color: #f4f7f6; display: flex; align-items: center; justify-content: center; height: 100vh; margin: 0; }
.error-card { text-align: center; padding: 40px; border-radius: 15px; background: white; box-shadow: 0 4px 15px rgba(0,0,0,0.05); max-width: 500px; width: 90%; }
</style>
</head>
<body>
<div class="error-card border-top border-4 border-danger">
<i class="fas fa-exclamation-circle text-danger fa-4x mb-3"></i>
<h4 class="fw-bold text-dark mb-3">เกิดข้อผิดพลาด</h4>
<p class="text-danger fs-5 mb-4">'.$error_msg.'</p>
<button onclick="window.close(); window.location.href=\'index.php\';" class="btn btn-secondary px-4 rounded-pill"><i class="fas fa-arrow-left"></i> กลับหน้าแรก</button>
</div>
</body>
</html>';
exit;
}
$enc_cid = isset($_GET['cid']) ? $_GET['cid'] : '';
$cid = decryptData($enc_cid);
if(!$cid) { showCertError("รหัสอ้างอิงไม่ถูกต้อง"); }
// ฟังก์ชันแปลงเลขอารบิกเป็นเลขไทย
function thainumDigit($num){
return str_replace(array( '0' , '1' , '2' , '3' , '4' , '5' , '6' ,'7' , '8' , '9' ),
array( "o" , "๑" , "๒" , "๓" , "๔" , "๕" , "๖" , "๗" , "๘" , "๙" ),
$num);
}
// ดึงข้อมูลรายชื่อ โยงกับกิจกรรม และค่าความหนาอักษร รวมถึงสีโรงเรียนและสีเลขที่
$query = "SELECT n.*, e.event_date, e.template_bg_1, e.template_bg_2, e.template_bg_3,
e.pos_name_y, e.pos_cert_x, e.pos_cert_y,
e.font_size_name, e.font_color_name, e.font_weight_name,
e.font_size_cert, e.font_color_cert, e.font_weight_cert,
e.cert_num_style, e.show_school, e.pos_school_y, e.font_size_school, e.font_color_school, e.font_weight_school, e.font_family
FROM cert_names n
INNER JOIN cert_events e ON n.event_id = e.event_id
WHERE n.id = '$cid'";
$result = mysqli_query($condb, $query);
if(!$result || mysqli_num_rows($result) == 0) { showCertError("ไม่พบข้อมูลรายชื่อในระบบ"); }
$rs_cer = mysqli_fetch_assoc($result);
// เลือก Template พื้นหลัง
$type = $rs_cer['type'];
$custom_bg = '';
if($type == '1') { $custom_bg = $rs_cer['template_bg_1']; }
elseif($type == '2') { $custom_bg = $rs_cer['template_bg_2']; }
elseif($type == '3') { $custom_bg = $rs_cer['template_bg_3']; }
if(!empty($custom_bg) && file_exists('img/'.$custom_bg)) {
$bg_image = 'img/'.$custom_bg;
} else {
if($type == "1" && file_exists('img/01.png')) $bg_image = 'img/01.png';
elseif($type == "2" && file_exists('img/1_0.png')) $bg_image = 'img/1_0.png';
elseif($type == "3" && file_exists('img/3_0.jpg')) $bg_image = 'img/3_0.jpg';
else $bg_image = 'img/01.png';
}
// กำหนดพิกัดและข้อมูลตัวอักษร
$pos_name_y = ($rs_cer['pos_name_y'] > 0) ? $rs_cer['pos_name_y'] : 44;
$pos_cert_x = ($rs_cer['pos_cert_x'] > 0) ? $rs_cer['pos_cert_x'] : 200;
$pos_cert_y = ($rs_cer['pos_cert_y'] > 0) ? $rs_cer['pos_cert_y'] : 10;
$f_size_name = ($rs_cer['font_size_name'] > 0) ? $rs_cer['font_size_name'] : 50;
$f_size_cert = ($rs_cer['font_size_cert'] > 0) ? $rs_cer['font_size_cert'] : 16;
$f_color_name_hex = !empty($rs_cer['font_color_name']) ? $rs_cer['font_color_name'] : '#000000';
$f_color_school_hex = !empty($rs_cer['font_color_school']) ? $rs_cer['font_color_school'] : '#000000';
$f_color_cert_hex = !empty($rs_cer['font_color_cert']) ? $rs_cer['font_color_cert'] : '#000000';
$f_weight_name = !empty($rs_cer['font_weight_name']) ? $rs_cer['font_weight_name'] : 'bold';
$f_weight_school = !empty($rs_cer['font_weight_school']) ? $rs_cer['font_weight_school'] : 'normal';
$f_weight_cert = !empty($rs_cer['font_weight_cert']) ? $rs_cer['font_weight_cert'] : 'normal';
// คำนวณปี พ.ศ. อัตโนมัติจากวันที่จัดกิจกรรม
$event_year_be = (int)date('Y', strtotime($rs_cer['event_date'])) + 543;
$cert_num = $rs_cer['cer_id'];
if($rs_cer['cert_num_style'] == 'thai') {
$cert_num = thainumDigit($cert_num);
$p_year = thainumDigit($event_year_be);
} else {
$p_year = $event_year_be;
}
$full_cert_text = 'เลขที่ สพม.รบ. ' . $cert_num . '/' . $p_year;
// ชื่อไฟล์สำหรับเซฟรูปภาพ
$save_filename = "Certificate_" . trim($rs_cer['nm']) . ".png";
// ---------------------------------------------------------
// ระบบดึง Font จาก Google Fonts แบบ Realtime
// ---------------------------------------------------------
$font_setting = !empty($rs_cer['font_family']) ? $rs_cer['font_family'] : 'sarabun';
$google_font_import = '';
$css_font_name = '"Sarabun", sans-serif';
if($font_setting == 'sarabun') {
$google_font_import = '@import url("https://fonts.googleapis.com/css2?family=Sarabun:wght@400;700&display=swap");';
$css_font_name = '"Sarabun", sans-serif';
} elseif($font_setting == 'angsa') {
$google_font_import = '@import url("https://fonts.googleapis.com/css2?family=Thasadith:wght@400;700&display=swap");';
$css_font_name = '"Thasadith", sans-serif';
} elseif($font_setting == 'charmonman') {
$google_font_import = '@import url("https://fonts.googleapis.com/css2?family=Charmonman:wght@400;700&display=swap");';
$css_font_name = '"Charmonman", cursive';
} elseif($font_setting == 'kanit') {
$google_font_import = '@import url("https://fonts.googleapis.com/css2?family=Kanit:wght@400;700&display=swap");';
$css_font_name = '"Kanit", sans-serif';
}
?>
<!DOCTYPE html>
<html lang="th">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>เกียรติบัตร - <?php echo htmlspecialchars($rs_cer['nm']); ?></title>
<style>
<?php echo $google_font_import; ?>
body {
margin: 0;
padding: 0;
background-color: #525659;
font-family: <?php echo $css_font_name; ?>;
-webkit-print-color-adjust: exact;
print-color-adjust: exact;
}
/* กล่องครอบกระดาษ A4 แนวนอน */
.cert-container {
position: relative;
width: 297mm;
height: 210mm;
background-image: url('<?php echo $bg_image; ?>');
background-size: 100% 100%;
background-repeat: no-repeat;
background-position: center;
margin: 20px auto;
box-shadow: 0 0 20px rgba(0,0,0,0.4);
background-color: #ffffff;
overflow: hidden;
}
/* ชื่อ-สกุล */
.cert-name {
position: absolute;
top: <?php echo $pos_name_y; ?>mm;
left: 49mm;
width: 200mm;
text-align: center;
font-size: <?php echo $f_size_name; ?>pt;
font-weight: <?php echo $f_weight_name; ?>;
color: <?php echo $f_color_name_hex; ?>;
line-height: 1;
white-space: nowrap;
}
/* ชื่อโรงเรียน */
.cert-school {
position: absolute;
<?php
$pos_school_y = ($rs_cer['pos_school_y'] > 0) ? $rs_cer['pos_school_y'] : 55;
$f_size_school = ($rs_cer['font_size_school'] > 0) ? $rs_cer['font_size_school'] : 40;
?>
top: <?php echo $pos_school_y; ?>mm;
left: 49mm;
width: 200mm;
text-align: center;
font-size: <?php echo $f_size_school; ?>pt;
font-weight: <?php echo $f_weight_school; ?>;
color: <?php echo $f_color_school_hex; ?>;
line-height: 1;
white-space: nowrap;
}
/* เลขที่เกียรติบัตร */
.cert-number {
position: absolute;
top: <?php echo $pos_cert_y; ?>mm;
left: <?php echo $pos_cert_x; ?>mm;
width: 65mm;
text-align: right;
font-size: <?php echo $f_size_cert; ?>pt;
font-weight: <?php echo $f_weight_cert; ?>;
color: <?php echo $f_color_cert_hex; ?>;
line-height: 1;
white-space: nowrap;
}
/* แผงปุ่มควบคุมมุมขวาล่าง */
.print-control {
position: fixed;
bottom: 30px;
right: 30px;
z-index: 999;
display: flex;
gap: 15px;
}
.btn-action {
color: #fff;
border: none;
padding: 12px 25px;
border-radius: 30px;
font-size: 15px;
font-weight: bold;
cursor: pointer;
box-shadow: 0 4px 15px rgba(0,0,0,0.3);
display: flex;
align-items: center;
gap: 10px;
font-family: "Sarabun", sans-serif;
transition: 0.2s;
}
.btn-image {
background: #28a745;
}
.btn-image:hover {
background: #1e7e34;
transform: scale(1.05);
}
.btn-print {
background: #212529;
}
.btn-print:hover {
background: #000;
transform: scale(1.05);
}
/* ซ่อนปุ่มเมื่อสั่งพิมพ์ออกทางเครื่องพิมพ์ */
@media print {
@page {
size: A4 landscape;
margin: 0;
}
body {
background-color: #ffffff;
}
.cert-container {
margin: 0;
box-shadow: none;
page-break-after: always;
}
.print-control {
display: none;
}
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>
</head>
<body>
<div class="print-control">
<button class="btn-action btn-image" onclick="downloadAsImage();">
<span>🖼️ บันทึกเป็นรูปภาพ (.PNG)</span>
</button>
<button class="btn-action btn-print" onclick="window.print();">
<span>🖨️ สั่งพิมพ์ / เซฟ PDF</span>
</button>
</div>
<div class="cert-container" id="certBlock">
<div class="cert-name"><?php echo htmlspecialchars($rs_cer['nm']); ?></div>
<?php if($rs_cer['show_school'] == 1 && !empty($rs_cer['school'])): ?>
<div class="cert-school"><?php echo htmlspecialchars($rs_cer['school']); ?></div>
<?php endif; ?>
<div class="cert-number"><?php echo htmlspecialchars($full_cert_text); ?></div>
</div>
<script>
// ฟังก์ชันจัดการจับภาพหน้าจอ HTML และดาวน์โหลดเป็นรูปภาพอัตโนมัติ
function downloadAsImage() {
const certElement = document.getElementById('certBlock');
// แจ้งเตือนผู้ใช้ชั่วคราวระหว่างระบบกำลังประมวลผลไฟล์ภาพความละเอียดสูง
const btn = document.querySelector('.btn-image');
const originalText = btn.innerHTML;
btn.innerHTML = '<span>⏳ กำลังสร้างรูปภาพ...</span>';
btn.disabled = true;
// สั่งให้ html2canvas แปลงบล็อก HTML เป็น Canvas วัตถุ (เพิ่มค่า scale เป็น 2 เพื่อเพิ่มความคมชัดของตัวอักษร)
html2canvas(certElement, {
scale: 2,
useCORS: true, // อนุญาตดึงภาพข้าม Server (กรณีเก็บภาพ Template ไว้อีกที่)
allowTaint: true,
logging: false
}).then(canvas => {
// แปลงข้อมูลกระดาษวาดภาพ Canvas ออกมาเป็น Data URL รูปภาพ PNG
const imageURL = canvas.toDataURL("image/png");
// สร้างลิงก์จำลองเพื่อดาวน์โหลดไฟล์ลงเครื่องผู้ใช้แบบอัตโนมัติ
const downloadLink = document.createElement('a');
downloadLink.href = imageURL;
downloadLink.download = "<?php echo $save_filename; ?>";
// สั่งเรียกคลิกลิงก์จำลองและทำลายวัตถุทิ้ง
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
// คืนค่าปุ่มกดกลับสู่สถานะปกติ
btn.innerHTML = originalText;
btn.disabled = false;
}).catch(error => {
alert('เกิดข้อผิดพลาดในการสร้างไฟล์รูปภาพ กรุณาใช้ปุ่มบันทึกเป็น PDF แทน');
btn.innerHTML = originalText;
btn.disabled = false;
});
}
</script>
</body>
</html>