Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* "Avatar.php" */
- header("Access-Control-Allow-Origin: *");
- header("Access-Control-Allow-Headers: Content-Type, Origin, Accept");
- header("Access-Control-Allow-Methods: GET, OPTIONS");
- ini_set('display_errors', '0');
- error_reporting(0);
- ob_clean(); // Clear output buffer just in case
- header_remove(); // Remove any previously set headers
- include_once("{$_SERVER['DOCUMENT_ROOT']}/Av-Gen.php");
- include_once("{$_SERVER['DOCUMENT_ROOT']}/Url Check Class.php");
- $urlCheck = new UrlCheckClass(['author', 'audience', 'type', 'format'], false);
- $urlCheck->validate();
- $get = $urlCheck->queryArray;
- $root = "{$_SERVER['DOCUMENT_ROOT']}/../nsfz-author-cache/authors/images";
- $author = basename($get['author'] ?? 'Various');
- $audience = $get['audience'] ?? '';
- $format = $get['format'] ?? '';
- $formatPath = ($format !== '') ? "/$format" : '';
- if (!preg_match('/^[a-zA-Z0-9 _-]+$/', $author)) {
- serveFallback("$root/Various.png", 404);
- }
- $audience = empty($audience) ? "Under21" : $audience;
- $audience = $audience === "MA-21" ? "MA-21" : "Under21";
- $searchPaths = [
- "",
- "/$audience",
- "/vods",
- "/Under21",
- "/MA-21"
- ];
- foreach ($searchPaths as $folder) {
- $path = "$root$folder/$author.png";
- if (!file_exists($path)) {
- error_log("Avatar not found: $path");
- }
- if (is_file($path)) {
- serveProcessedImage($path);
- }
- }
- serveFallback("$root/Various.png");
- // Serve a 72x72 processed image with background fill
- function serveProcessedImage(string $path): void
- {
- global $root;
- [$srcW, $srcH] = getimagesize($path);
- $src = @imagecreatefrompng($path);
- if (!$src) {
- serveFallback("$root/Various.png", 500);
- }
- imagealphablending($src, false);
- imagesavealpha($src, true);
- if ($srcW === 72 && $srcH === 72) {
- serveDirect($src);
- }
- // Create transparent 72x72 canvas
- $canvas = imagecreatetruecolor(72, 72);
- imagealphablending($canvas, false);
- imagesavealpha($canvas, true);
- $transparent = imagecolorallocatealpha($canvas, 0, 0, 0, 127);
- imagefilledrectangle($canvas, 0, 0, 71, 71, $transparent);
- // Scale to fit (preserving aspect ratio)
- $scale = min(72 / $srcW, 72 / $srcH);
- $dstW = (int) round($srcW * $scale);
- $dstH = (int) round($srcH * $scale);
- $dstX = (int) ((72 - $dstW) / 2);
- $dstY = (int) ((72 - $dstH) / 2);
- imagecopyresampled(
- $canvas, $src,
- $dstX, $dstY, 0, 0,
- $dstW, $dstH,
- $srcW, $srcH
- );
- serveDirect($canvas);
- }
- // Serve raw PNG from GD
- function serveDirect($gdImage): void
- {
- http_response_code(200);
- header('Content-Type: image/png');
- imagepng($gdImage);
- imagedestroy($gdImage);
- exit;
- }
- // Serve fallback image as-is
- function serveFallback(string $path, int $code = 200): void
- {
- if (!is_file($path)) {
- http_response_code(404);
- exit;
- }
- http_response_code($code);
- header('Content-Type: image/png');
- header('Content-Length: ' . filesize($path));
- readfile($path);
- exit;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement