Advertisement
jargon

Avatar.php

Jun 11th, 2025
1,242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.90 KB | Gaming | 0 0
  1. <?php
  2. /* "Avatar.php" */
  3.  
  4. header("Access-Control-Allow-Origin: *");
  5. header("Access-Control-Allow-Headers: Content-Type, Origin, Accept");
  6. header("Access-Control-Allow-Methods: GET, OPTIONS");
  7.  
  8. ini_set('display_errors', '0');
  9. error_reporting(0);
  10.  
  11. ob_clean(); // Clear output buffer just in case
  12. header_remove(); // Remove any previously set headers
  13.  
  14. include_once("{$_SERVER['DOCUMENT_ROOT']}/Av-Gen.php");
  15.  
  16. include_once("{$_SERVER['DOCUMENT_ROOT']}/Url Check Class.php");
  17.  
  18. $urlCheck = new UrlCheckClass(['author', 'audience', 'type', 'format'], false);
  19. $urlCheck->validate();
  20. $get = $urlCheck->queryArray;
  21.  
  22. $root = "{$_SERVER['DOCUMENT_ROOT']}/../nsfz-author-cache/authors/images";
  23. $author = basename($get['author'] ?? 'Various');
  24. $audience = $get['audience'] ?? '';
  25. $format = $get['format'] ?? '';
  26. $formatPath = ($format !== '') ? "/$format" : '';
  27.  
  28. if (!preg_match('/^[a-zA-Z0-9 _-]+$/', $author)) {
  29.     serveFallback("$root/Various.png", 404);
  30. }
  31.  
  32. $audience = empty($audience) ? "Under21" : $audience;
  33. $audience = $audience === "MA-21" ? "MA-21" : "Under21";
  34.  
  35. $searchPaths = [
  36.     "",
  37.     "/$audience",
  38.     "/vods",
  39.     "/Under21",
  40.     "/MA-21"
  41. ];
  42.  
  43. foreach ($searchPaths as $folder) {
  44.     $path = "$root$folder/$author.png";
  45.     if (!file_exists($path)) {
  46.         error_log("Avatar not found: $path");
  47.     }
  48.     if (is_file($path)) {
  49.         serveProcessedImage($path);
  50.     }
  51. }
  52.  
  53. serveFallback("$root/Various.png");
  54.  
  55. // Serve a 72x72 processed image with background fill
  56. function serveProcessedImage(string $path): void
  57. {
  58.     global $root;
  59.  
  60.     [$srcW, $srcH] = getimagesize($path);
  61.     $src = @imagecreatefrompng($path);
  62.     if (!$src) {
  63.         serveFallback("$root/Various.png", 500);
  64.     }
  65.    
  66.     imagealphablending($src, false);
  67.     imagesavealpha($src, true);
  68.  
  69.     if ($srcW === 72 && $srcH === 72) {
  70.         serveDirect($src);
  71.     }
  72.  
  73.     // Create transparent 72x72 canvas
  74.     $canvas = imagecreatetruecolor(72, 72);
  75.     imagealphablending($canvas, false);
  76.     imagesavealpha($canvas, true);
  77.     $transparent = imagecolorallocatealpha($canvas, 0, 0, 0, 127);
  78.     imagefilledrectangle($canvas, 0, 0, 71, 71, $transparent);
  79.  
  80.     // Scale to fit (preserving aspect ratio)
  81.     $scale = min(72 / $srcW, 72 / $srcH);
  82.     $dstW = (int) round($srcW * $scale);
  83.     $dstH = (int) round($srcH * $scale);
  84.     $dstX = (int) ((72 - $dstW) / 2);
  85.     $dstY = (int) ((72 - $dstH) / 2);
  86.  
  87.     imagecopyresampled(
  88.         $canvas, $src,
  89.         $dstX, $dstY, 0, 0,
  90.         $dstW, $dstH,
  91.         $srcW, $srcH
  92.     );
  93.  
  94.     serveDirect($canvas);
  95. }
  96.  
  97. // Serve raw PNG from GD
  98. function serveDirect($gdImage): void
  99. {
  100.     http_response_code(200);
  101.     header('Content-Type: image/png');
  102.     imagepng($gdImage);
  103.     imagedestroy($gdImage);
  104.     exit;
  105. }
  106.  
  107. // Serve fallback image as-is
  108. function serveFallback(string $path, int $code = 200): void
  109. {
  110.     if (!is_file($path)) {
  111.         http_response_code(404);
  112.         exit;
  113.     }
  114.     http_response_code($code);
  115.     header('Content-Type: image/png');
  116.     header('Content-Length: ' . filesize($path));
  117.     readfile($path);
  118.     exit;
  119. }
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement