Sindbad~EG File Manager

Current Path : /home/copmadinaarea/.trash/
Upload File :
Current File : /home/copmadinaarea/.trash/test_image_generation.php

<?php
require_once 'config/config.php';

// Define the image generation functions directly here to avoid path issues
// Function to generate unique tracking code
function generateTrackingCode($type = 'member') {
    $prefix = $type === 'member' ? 'MEM' : 'USR';
    return $prefix . date('Y') . str_pad(mt_rand(1, 999999), 6, '0', STR_PAD_LEFT);
}

// Function to generate barcode (Code 128)
function generateBarcode($code) {
    // Check if GD is available
    if (!extension_loaded('gd')) {
        // Fallback to SVG with better encoding
        return generateBarcodeSVG($code);
    }
    
    try {
        // Create PNG image using GD
        $width = 200;
        $height = 50;
        $image = imagecreate($width, $height);
        
        if (!$image) {
            return generateBarcodeSVG($code);
        }
        
        // Colors
        $white = imagecolorallocate($image, 255, 255, 255);
        $black = imagecolorallocate($image, 0, 0, 0);
        
        // Fill background
        imagefill($image, 0, 0, $white);
        
        // Generate barcode pattern based on code
        $codeLength = strlen($code);
        $x = 10;
        
        for ($i = 0; $i < $codeLength && $x < 180; $i++) {
            $char = ord($code[$i]);
            $pattern = $char % 4; // Simple pattern based on character
            
            for ($j = 0; $j < 3 && $x < 180; $j++) {
                $barWidth = ($pattern & (1 << $j)) ? 3 : 1;
                imagefilledrectangle($image, $x, 5, $x + $barWidth, 35, $black);
                $x += $barWidth + 1;
            }
        }
        
        // Add text
        imagestring($image, 2, 10, 37, substr($code, 0, 20), $black);
        
        // Convert to base64
        ob_start();
        imagepng($image);
        $imageData = ob_get_contents();
        ob_end_clean();
        imagedestroy($image);
        
        if ($imageData) {
            return "data:image/png;base64," . base64_encode($imageData);
        } else {
            return generateBarcodeSVG($code);
        }
        
    } catch (Exception $e) {
        return generateBarcodeSVG($code);
    }
}

// Fallback SVG barcode generation
function generateBarcodeSVG($code) {
    $svg = '<?xml version="1.0" encoding="UTF-8"?>
    <svg width="200" height="50" xmlns="http://www.w3.org/2000/svg">
        <rect width="200" height="50" fill="white"/>
        <g fill="black">';
    
    $x = 10;
    $codeLength = strlen($code);
    
    for ($i = 0; $i < $codeLength && $x < 180; $i++) {
        $char = ord($code[$i]);
        $pattern = $char % 4;
        
        for ($j = 0; $j < 3 && $x < 180; $j++) {
            $barWidth = ($pattern & (1 << $j)) ? 3 : 1;
            $svg .= '<rect x="' . $x . '" y="5" width="' . $barWidth . '" height="30"/>';
            $x += $barWidth + 1;
        }
    }
    
    $svg .= '</g>
        <text x="100" y="45" text-anchor="middle" font-family="monospace" font-size="8" fill="black">' . htmlspecialchars(substr($code, 0, 20)) . '</text>
    </svg>';
    
    return "data:image/svg+xml;base64," . base64_encode($svg);
}

// Function to generate QR code
function generateQRCode($code) {
    // Check if GD is available
    if (!extension_loaded('gd')) {
        return generateQRCodeSVG($code);
    }
    
    try {
        // Create PNG image using GD
        $size = 100;
        $image = imagecreate($size, $size);
        
        if (!$image) {
            return generateQRCodeSVG($code);
        }
        
        // Colors
        $white = imagecolorallocate($image, 255, 255, 255);
        $black = imagecolorallocate($image, 0, 0, 0);
        
        // Fill background
        imagefill($image, 0, 0, $white);
        
        // Generate QR-like pattern based on code
        $codeHash = md5($code);
        $blockSize = 5;
        
        // Corner markers
        imagefilledrectangle($image, 5, 5, 30, 30, $black);
        imagefilledrectangle($image, 70, 5, 95, 30, $black);
        imagefilledrectangle($image, 5, 70, 30, 95, $black);
        
        // Inner corner markers (white)
        imagefilledrectangle($image, 10, 10, 25, 25, $white);
        imagefilledrectangle($image, 75, 10, 90, 25, $white);
        imagefilledrectangle($image, 10, 75, 25, 90, $white);
        
        // Center dots
        imagefilledrectangle($image, 15, 15, 20, 20, $black);
        imagefilledrectangle($image, 80, 15, 85, 20, $black);
        imagefilledrectangle($image, 15, 80, 20, 85, $black);
        
        // Data pattern based on hash
        for ($i = 0; $i < 32; $i++) {
            $hexChar = hexdec($codeHash[$i]);
            for ($bit = 0; $bit < 4; $bit++) {
                if ($hexChar & (1 << $bit)) {
                    $x = 35 + (($i * 2 + $bit) % 6) * $blockSize;
                    $y = 35 + floor(($i * 2 + $bit) / 6) * $blockSize;
                    if ($x < 95 && $y < 95) {
                        imagefilledrectangle($image, $x, $y, $x + $blockSize - 1, $y + $blockSize - 1, $black);
                    }
                }
            }
        }
        
        // Convert to base64
        ob_start();
        imagepng($image);
        $imageData = ob_get_contents();
        ob_end_clean();
        imagedestroy($image);
        
        if ($imageData) {
            return "data:image/png;base64," . base64_encode($imageData);
        } else {
            return generateQRCodeSVG($code);
        }
        
    } catch (Exception $e) {
        return generateQRCodeSVG($code);
    }
}

// Fallback SVG QR code generation
function generateQRCodeSVG($code) {
    $codeHash = md5($code);
    
    $svg = '<?xml version="1.0" encoding="UTF-8"?>
    <svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
        <rect width="100" height="100" fill="white"/>
        <g fill="black">
            <!-- Corner markers -->
            <rect x="5" y="5" width="25" height="25"/>
            <rect x="70" y="5" width="25" height="25"/>
            <rect x="5" y="70" width="25" height="25"/>
            
            <!-- Inner corner markers -->
            <rect x="10" y="10" width="15" height="15" fill="white"/>
            <rect x="75" y="10" width="15" height="15" fill="white"/>
            <rect x="10" y="75" width="15" height="15" fill="white"/>
            
            <rect x="15" y="15" width="5" height="5" fill="black"/>
            <rect x="80" y="15" width="5" height="5" fill="black"/>
            <rect x="15" y="80" width="5" height="5" fill="black"/>';
    
    // Data pattern based on hash
    for ($i = 0; $i < 16; $i++) {
        $hexChar = hexdec($codeHash[$i]);
        for ($bit = 0; $bit < 4; $bit++) {
            if ($hexChar & (1 << $bit)) {
                $x = 35 + (($i * 2 + $bit) % 6) * 5;
                $y = 35 + floor(($i * 2 + $bit) / 6) * 5;
                if ($x < 95 && $y < 95) {
                    $svg .= '<rect x="' . $x . '" y="' . $y . '" width="5" height="5"/>';
                }
            }
        }
    }
    
    $svg .= '</g></svg>';
    
    return "data:image/svg+xml;base64," . base64_encode($svg);
}

$testCode = $_GET['code'] ?? 'MEM2025123456';
$type = $_GET['type'] ?? 'barcode';

header('Content-Type: text/html');

echo "<!DOCTYPE html>
<html>
<head>
    <title>Image Generation Test</title>
    <style>
        body { font-family: Arial, sans-serif; padding: 20px; }
        .test-box { border: 1px solid #ddd; padding: 20px; margin: 20px 0; }
        img { border: 1px solid #ccc; margin: 10px; }
        .code-display { background: #f5f5f5; padding: 10px; font-family: monospace; }
    </style>
</head>
<body>";

echo "<h1>Image Generation Test</h1>";
echo "<p>Testing code: <strong>$testCode</strong></p>";

if ($type === 'barcode' || $type === 'both') {
    echo "<div class='test-box'>";
    echo "<h2>Barcode Test</h2>";
    
    try {
        $barcode = generateBarcode($testCode);
        echo "<p>Generation successful!</p>";
        echo "<img src='$barcode' alt='Barcode' style='max-width: 200px;'>";
        echo "<div class='code-display'>Data URL: " . substr($barcode, 0, 100) . "...</div>";
    } catch (Exception $e) {
        echo "<p style='color: red;'>Error: " . $e->getMessage() . "</p>";
    }
    
    echo "</div>";
}

if ($type === 'qrcode' || $type === 'both') {
    echo "<div class='test-box'>";
    echo "<h2>QR Code Test</h2>";
    
    try {
        $qrcode = generateQRCode($testCode);
        echo "<p>Generation successful!</p>";
        echo "<img src='$qrcode' alt='QR Code' style='width: 100px; height: 100px;'>";
        echo "<div class='code-display'>Data URL: " . substr($qrcode, 0, 100) . "...</div>";
    } catch (Exception $e) {
        echo "<p style='color: red;'>Error: " . $e->getMessage() . "</p>";
    }
    
    echo "</div>";
}

echo "<div class='test-box'>";
echo "<h2>System Info</h2>";
echo "<p>GD Extension: " . (extension_loaded('gd') ? 'Available' : 'Not Available') . "</p>";
echo "<p>PHP Version: " . phpversion() . "</p>";
echo "</div>";

echo "<div class='test-box'>";
echo "<h2>Test Links</h2>";
echo "<p><a href='?code=$testCode&type=barcode'>Test Barcode Only</a></p>";
echo "<p><a href='?code=$testCode&type=qrcode'>Test QR Code Only</a></p>";
echo "<p><a href='?code=$testCode&type=both'>Test Both</a></p>";
echo "<p><a href='modules/membership/codes.php'>← Back to Codes Page</a></p>";
echo "</div>";

echo "</body></html>";
?>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists