| 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 : E:/Inetpub/www/certificate/ |
Upload File : |
<?php
// ปิดการแสดง Error ทางเทคนิคของ PHP บนหน้าจอ เพื่อไม่ให้รบกวนการแสดงผล
error_reporting(0);
require 'db.php';
function showCertError($error_msg = "ไม่สามารถดาวน์โหลดเกียรติบัตรได้<br>กรุณาติดต่อเจ้าหน้าที่") {
echo '<!DOCTYPE html>
<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;
}
// ใช้ FPDF แบบเดิม
require('pdf1.84/fpdf.php');
$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);
}
function hex2rgb($hex) {
$hex = str_replace("#", "", $hex);
if(empty($hex)) return array(0,0,0);
if(strlen($hex) == 3) {
$r = hexdec(substr($hex,0,1).substr($hex,0,1));
$g = hexdec(substr($hex,1,1).substr($hex,1,1));
$b = hexdec(substr($hex,2,1).substr($hex,2,1));
} else {
$r = hexdec(substr($hex,0,2));
$g = hexdec(substr($hex,2,2));
$b = hexdec(substr($hex,4,2));
}
return array($r, $g, $b);
}
// ดึงข้อมูลรายชื่อ โยงกับกิจกรรม
$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_size_cert,
e.cert_num_style, e.show_school, e.pos_school_y, e.font_size_school
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);
try {
$pdf = new FPDF( 'L' , 'mm' , 'A4' );
$pdf->SetAutoPageBreak(false);
$pdf->AddPage();
$pdf->AddFont('sarabun','','THSarabun.php');
$pdf->AddFont('sarabunb','','THSarabunB.php');
$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)) {
$pdf->Image('img/'.$custom_bg, 0, 0, 297, 210);
} else {
if($type == "1" && file_exists('img/01.png')) $pdf->Image('img/01.png',0,0,297,210);
elseif($type == "2" && file_exists('img/1_0.png')) $pdf->Image('img/1_0.png',0,0,297,210);
elseif($type == "3" && file_exists('img/3_0.jpg')) $pdf->Image('img/3_0.jpg',0,0,297,210);
}
$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_hex = !empty($rs_cer['font_color_name']) ? $rs_cer['font_color_name'] : '#000000';
$name_rgb = hex2rgb($f_color_hex);
// คำนวณปี พ.ศ. อัตโนมัติจากวันที่จัดกิจกรรม
$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;
}
// =========================================================
// ใช้ iconv แบบมาตรฐาน เพื่อป้องกันตัวอักษรหาย
// =========================================================
// 1. วางชื่อ-นามสกุล
$pdf->SetTextColor($name_rgb[0], $name_rgb[1], $name_rgb[2]);
$pdf->SetXY(49, $pos_name_y);
$pdf->SetFont('sarabunb', '', $f_size_name);
$pdf->Cell(200, 84, iconv('utf-8', 'cp874', $rs_cer['nm']), 0, 1, 'C');
// 2. วางชื่อโรงเรียน
if($rs_cer['show_school'] == 1 && !empty($rs_cer['school'])) {
$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;
$pdf->SetXY(49, $pos_school_y);
$pdf->SetFont('sarabun', '', $f_size_school);
$pdf->SetTextColor($name_rgb[0], $name_rgb[1], $name_rgb[2]);
$pdf->Cell(200, 84, iconv('utf-8', 'cp874', $rs_cer['school']), 0, 1, 'C');
}
// 3. วางเลขที่เกียรติบัตร
$pdf->SetTextColor(0, 0, 0);
$pdf->SetXY($pos_cert_x, $pos_cert_y);
$pdf->SetFont('sarabun', '', $f_size_cert);
$full_cert_text = 'เลขที่ สพม.รบ. ' . $cert_num . '/' . $p_year;
$pdf->Cell(65, 40, iconv('utf-8', 'cp874', $full_cert_text), 0, 1, 'R');
if (ob_get_contents()) { ob_end_clean(); }
$pdf->Output('certificate.pdf', 'I');
} catch (Exception $e) {
showCertError("ระบบสร้าง PDF ผิดพลาด: " . $e->getMessage());
}
?>