<?php
// quick test: cek writable, allow_url_fopen, fetch google chart via file_get_contents & cURL, dan GD availability

$dir = __DIR__ . '/Uploads/qrcodes';
echo "<pre>Folder check: $dir\n";
echo "Exists: " . (is_dir($dir) ? 'YES' : 'NO') . "\n";
echo "Is writable: " . (is_writable($dir) ? 'YES' : 'NO') . "\n\n";

echo "PHP settings:\n";
echo "allow_url_fopen: " . (ini_get('allow_url_fopen') ? 'ON' : 'OFF') . "\n";
echo "GD installed: " . (function_exists('imagepng') ? 'YES' : 'NO') . "\n\n";

$url = 'https://chart.googleapis.com/chart?cht=qr&chs=200x200&chl=' . urlencode('https://example.com') . '&choe=UTF-8';
echo "Testing fetch via file_get_contents...\n";
$ok1 = @file_get_contents($url);
echo $ok1 ? "file_get_contents OK, length: " . strlen($ok1) . "\n" : "file_get_contents FAILED\n";

echo "\nTesting fetch via cURL...\n";
if (function_exists('curl_version')) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $ok2 = curl_exec($ch);
    $err = curl_error($ch);
    curl_close($ch);
    if ($ok2) echo "cURL OK, length: " . strlen($ok2) . "\n";
    else echo "cURL FAILED, error: $err\n";
} else {
    echo "cURL not available\n";
}

echo "\nTry create temp image via GD (imagepng)...\n";
$img = imagecreatetruecolor(20,20);
$white = imagecolorallocate($img,255,255,255);
$black = imagecolorallocate($img,0,0,0);
imagefill($img,0,0,$white);
imagefilledrectangle($img,2,2,18,18,$black);
$fn = $dir . '/test_write.png';
$w = @imagepng($img, $fn);
imagedestroy($img);
echo $w ? "imagepng write OK to $fn\n" : "imagepng write FAILED\n";

echo "\nDone.\n</pre>";
