| Server IP : 139.59.63.204 / Your IP : 216.73.217.62 Web Server : Apache/2.4.58 (Ubuntu) System : Linux ubuntu-s-1vcpu-1gb-blr1-01 6.8.0-110-generic #110-Ubuntu SMP PREEMPT_DYNAMIC Thu Mar 19 15:09:20 UTC 2026 x86_64 User : root ( 0) PHP Version : 8.3.6 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/abyogasms.com/app/Http/Controllers/ |
Upload File : |
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class CertificateController extends Controller
{
public function downloadCertificate(Request $request)
{
try {
$user = DB::table('freemembers')->where('mobile','=',$request->mobile)->first();
$templatePath = public_path('images/cert/free.jpeg');
if (!file_exists($templatePath)) {
return response()->json(['error' => 'Certificate template not found'], 404);
}
// Load the certificate template image
$image = imagecreatefromjpeg($templatePath);
if (!$image) {
return response()->json(['error' => 'Failed to load certificate template'], 500);
}
// Get image dimensions
$imageWidth = imagesx($image);
$imageHeight = imagesy($image);
// Allocate color for text - matching certificate template color
$textColor = imagecolorallocate($image, 120, 81, 45); // Brown/tan color for certificates
//dd($textColor);
// Try to use TrueType font for better appearance
$fontPath = public_path('assets/fonts/static/Lora-Bold.ttf');
$fontSize = 25;
$angle = 0;
// Get text bounding box for TrueType font if available
if (file_exists($fontPath)) {
$bbox = imagettfbbox($fontSize, $angle, $fontPath, $user->title.' '.$user->fullname);
$textWidth = abs($bbox[4] - $bbox[0]);
$textHeight = abs($bbox[5] - $bbox[1]);
// Center horizontally
$x = ($imageWidth - $textWidth) / 2;
// Position vertically - slightly above center
$y = ($imageHeight / 2) - 3;
// Add text using TrueType font
imagettftext($image, $fontSize, $angle, $x, $y, $textColor, $fontPath, $user->title.' '.$user->fullname);
/** Registration Number */
$x = ($imageWidth/2) + 170;
$y = ($imageHeight / 2) + 130;
imagettftext($image, $fontSize, $angle, $x, $y, $textColor, $fontPath, $user->registration_number);
/** Date */
//$fontPath = public_path('assets/fonts/static/Lora-Regular.ttf');
$x = ($imageWidth/2) + 100;
$y = ($imageHeight / 2) + 200;
imagettftext($image, $fontSize, $angle, $x, $y, $textColor, $fontPath, date('d/m/Y', strtotime($user->created_at)));
/** Date */
$fontSize = 20;
$x = ($imageWidth/5) - 130;
$y = ($imageHeight / 2) + 280;
imagettftext($image, $fontSize, $angle, $x, $y, $textColor, $fontPath, date('d/m/Y', strtotime($user->created_at)));
} else {
// Fallback to built-in font if TrueType not available
$fontSize = 5;
$fontWidth = imagefontwidth($fontSize);
$fontHeight = imagefontheight($fontSize);
$textWidth = strlen($user->title.' '.$user->fullname) * $fontWidth;
$x = ($imageWidth - $textWidth) / 2;
$y = ($imageHeight / 2) - ($fontHeight / 2);
imagestring($image, $fontSize, $x, $y, $user->title.' '.$user->fullname, $textColor);
}
// Generate filename
$filename = 'certificate_' . $user->id . '_' . time() . '.jpg';
// Output the image
header('Content-Type: image/jpeg');
header('Content-Disposition: attachment; filename="' . $filename . '"');
imagejpeg($image, null, 95);
imagedestroy($image);
exit;
} catch (\Exception $e) {
return response()->json(['error' => 'Failed to generate certificate: ' . $e->getMessage()], 500);
}
}
public function generateCertificate()
{
try {
$user = auth()->user();
$templatePath = public_path('images/cert/free.jpeg');
if (!file_exists($templatePath)) {
return response()->json(['error' => 'Certificate template not found'], 404);
}
// Load the certificate template image
$image = imagecreatefromjpeg($templatePath);
if (!$image) {
return response()->json(['error' => 'Failed to load certificate template'], 500);
}
// Get image dimensions
$imageWidth = imagesx($image);
$imageHeight = imagesy($image);
// Allocate color for text - matching certificate template color
$textColor = imagecolorallocate($image, 120, 81, 45); // Brown/tan color for certificates
//dd($textColor);
// Try to use TrueType font for better appearance
$fontPath = public_path('assets/fonts/static/Lora-Bold.ttf');
$fontSize = 25;
$angle = 0;
// Get text bounding box for TrueType font if available
if (file_exists($fontPath)) {
$bbox = imagettfbbox($fontSize, $angle, $fontPath, $user->name);
$textWidth = abs($bbox[4] - $bbox[0]);
$textHeight = abs($bbox[5] - $bbox[1]);
// Center horizontally
$x = ($imageWidth - $textWidth) / 2;
// Position vertically - slightly above center
$y = ($imageHeight / 2) - 3;
// Add text using TrueType font
imagettftext($image, $fontSize, $angle, $x, $y, $textColor, $fontPath, $user->name);
/** Registration Number */
$x = ($imageWidth/2) + 170;
$y = ($imageHeight / 2) + 130;
imagettftext($image, $fontSize, $angle, $x, $y, $textColor, $fontPath, $user->registration_number);
/** Date */
//$fontPath = public_path('assets/fonts/static/Lora-Regular.ttf');
$x = ($imageWidth/2) + 100;
$y = ($imageHeight / 2) + 200;
imagettftext($image, $fontSize, $angle, $x, $y, $textColor, $fontPath, date('d/m/Y', strtotime($user->renew_date)));
/** Date */
$fontSize = 20;
$x = ($imageWidth/5) - 130;
$y = ($imageHeight / 2) + 280;
imagettftext($image, $fontSize, $angle, $x, $y, $textColor, $fontPath, date('d/m/Y', strtotime($user->renew_date)));
} else {
// Fallback to built-in font if TrueType not available
$fontSize = 5;
$fontWidth = imagefontwidth($fontSize);
$fontHeight = imagefontheight($fontSize);
$textWidth = strlen($user->name) * $fontWidth;
$x = ($imageWidth - $textWidth) / 2;
$y = ($imageHeight / 2) - ($fontHeight / 2);
imagestring($image, $fontSize, $x, $y, $user->name, $textColor);
}
// Generate filename
$filename = 'certificate_' . $user->id . '_' . time() . '.jpg';
// Output the image
header('Content-Type: image/jpeg');
header('Content-Disposition: attachment; filename="' . $filename . '"');
imagejpeg($image, null, 95);
imagedestroy($image);
exit;
} catch (\Exception $e) {
return response()->json(['error' => 'Failed to generate certificate: ' . $e->getMessage()], 500);
}
}
public function generateIDCard()
{
try {
$user = auth()->user();
$frontTemplatePath = public_path('images/cert/idfront.jpeg');
$backTemplatePath = public_path('images/cert/idback.jpeg');
if (!file_exists($frontTemplatePath)) {
return response()->json(['error' => 'ID card front template not found'], 404);
}
if (!file_exists($backTemplatePath)) {
return response()->json(['error' => 'ID card back template not found'], 404);
}
// Create front image
$frontImage = imagecreatefromjpeg($frontTemplatePath);
if (!$frontImage) {
return response()->json(['error' => 'Failed to load ID card front template'], 500);
}
// Get image dimensions
$imageWidth = imagesx($frontImage);
$imageHeight = imagesy($frontImage);
// Add profile image if exists
if ($user->profileimage) {
$profileImagePath = Storage::disk('public')->path($user->profileimage);
if (file_exists($profileImagePath)) {
// Load profile image
$profileImage = null;
$ext = strtolower(pathinfo($profileImagePath, PATHINFO_EXTENSION));
if ($ext === 'jpeg' || $ext === 'jpg') {
$profileImage = imagecreatefromjpeg($profileImagePath);
} elseif ($ext === 'png') {
$profileImage = imagecreatefrompng($profileImagePath);
} elseif ($ext === 'gif') {
$profileImage = imagecreatefromgif($profileImagePath);
}
if ($profileImage) {
// Circle dimensions
$circleRadius = 180;
$circleDiameter = $circleRadius * 2;
// Position on ID card
$circleX = 50;
$circleY = 293;
// Get profile image dimensions
$profileWidth = imagesx($profileImage);
$profileHeight = imagesy($profileImage);
// Create a temporary image for the circular profile
$tempCircle = imagecreatetruecolor($circleDiameter, $circleDiameter);
// Resize profile image to circle size
imagecopyresampled($tempCircle, $profileImage, 0, 0, 0, 0,
$circleDiameter, $circleDiameter, $profileWidth, $profileHeight);
// Apply circular mask by only copying pixels that are within the circle
for ($x = 0; $x < $circleDiameter; $x++) {
for ($y = 0; $y < $circleDiameter; $y++) {
$distance = sqrt(pow($x - $circleRadius, 2) + pow($y - $circleRadius, 2));
if ($distance <= $circleRadius) {
// Inside circle - copy pixel directly
$pixelColor = imagecolorat($tempCircle, $x, $y);
imagesetpixel($frontImage, $circleX + $x, $circleY + $y, $pixelColor);
}
}
}
imagedestroy($profileImage);
imagedestroy($tempCircle);
}
}
}
// Allocate color for text - dark color for ID card
$textColor = imagecolorallocate($frontImage, 0, 0, 0);
// Try to use TrueType font
$fontPath = public_path('assets/fonts/static/Lora-Bold.ttf');
$fontSize = 25;
$angle = 0;
// Add text to front image
if (file_exists($fontPath)) {
$bbox = imagettfbbox($fontSize, $angle, $fontPath, $user->name);
$textWidth = abs($bbox[4] - $bbox[0]);
// Position to the right of the profile image
$x = ($imageWidth / 2) + 60;
$y = ($imageHeight / 2) - 55;
imagettftext($frontImage, $fontSize, $angle, $x, $y, $textColor, $fontPath, $user->name);
$y = ($imageHeight / 2);
imagettftext($frontImage, $fontSize, $angle, $x, $y, $textColor, $fontPath, $user->designation);
$y = ($imageHeight / 2) + 58;
imagettftext($frontImage, $fontSize, $angle, $x, $y, $textColor, $fontPath, $user->mobile);
$y = ($imageHeight / 2) + 112;
imagettftext($frontImage, $fontSize, $angle, $x, $y, $textColor, $fontPath, $user->email);
$y = ($imageHeight / 2) + 170;
imagettftext($frontImage, $fontSize, $angle, $x, $y, $textColor, $fontPath, $user->address);
} else {
// Fallback to built-in font
$fontSize = 5;
$fontWidth = imagefontwidth($fontSize);
$textWidth = strlen($user->name) * $fontWidth;
$x = ($imageWidth - $textWidth) / 2;
$y = ($imageHeight / 2);
imagestring($frontImage, $fontSize, $x, $y, $user->name, $textColor);
}
// Create combined image (front and back stacked vertically)
$backImage = imagecreatefromjpeg($backTemplatePath);
if (!$backImage) {
imagedestroy($frontImage);
return response()->json(['error' => 'Failed to load ID card back template'], 500);
}
// Get back image dimensions
$backWidth = imagesx($backImage);
$backHeight = imagesy($backImage);
// Create new image to combine front and back (stacked vertically)
$combinedImage = imagecreatetruecolor($imageWidth, $imageHeight + $backHeight);
// Copy front image to top
imagecopy($combinedImage, $frontImage, 0, 0, 0, 0, $imageWidth, $imageHeight);
// Copy back image to bottom
imagecopy($combinedImage, $backImage, 0, $imageHeight, 0, 0, $backWidth, $backHeight);
// Generate filename
$filename = 'idcard_' . $user->id . '_' . time() . '.jpg';
// Output the combined image
header('Content-Type: image/jpeg');
header('Content-Disposition: attachment; filename="' . $filename . '"');
imagejpeg($combinedImage, null, 95);
// Clean up
imagedestroy($frontImage);
imagedestroy($backImage);
imagedestroy($combinedImage);
exit;
} catch (\Exception $e) {
return response()->json(['error' => 'Failed to generate ID card: ' . $e->getMessage()], 500);
}
}
public function freeregister(Request $request) {
$validated = $request->validate([
'title' => 'required|string',
'name' => 'required|string|max:255',
'email' => 'required|email|unique:freemembers,email',
'mobile' => 'required|numeric|unique:freemembers,mobile',
'state_id' => 'required|integer',
'district_id' => 'required|integer',
]);
$memebr = DB::table('freemembers')->insertGetId([
'title' => $request->title,
'fullname' => $request->name,
'email' => $request->email,
'mobile' => $request->mobile,
'state' => $request->state_id,
'district' => $request->district_id,
'created_at' => now(),
'updated_at' => now()
]);
if ($memebr) {
$affectedRows = DB::table('freemembers')
->where('id', $memebr) // Finds the specific record
->update([
'registration_number' => $memebr + 26999,
'updated_at' => now() // Query Builder requires updating this manually
]);
}
if ($request->wantsJson()) {
return response()->json([
'status' => 'Registration completed successfully!'
], 200);
}
return redirect()->back()->with('status', 'Registration completed successfully!');
}
}