Repositories | Polyback | Log in to see private repositories


polyback-website / 1229fe4649cb3ec2444c7fef95f4bc3c95be66fc / thumbs/asset.php

3,921 bytes | parent directory

<?php require $_SERVER["DOCUMENT_ROOT"]."/api/private/core.php";

use pizzaboxer\ProjectPolygon\Database;
use pizzaboxer\ProjectPolygon\Thumbnails;

$ResX = $_GET['x'] ?? $_GET['wd'] ?? $_GET["width"] ?? 420;
$ResY = $_GET['y'] ?? $_GET['ht'] ?? $_GET["height"] ?? 420;
$ForceRoblox = isset($_GET["roblox"]) && $_GET["roblox"] == "true";

$AssetID = $_GET['id'] ?? $_GET['assetId'] ?? $_GET['aid'] ?? 0;
$AssetVersionID = $_GET['assetversionid'] ?? $_GET['assetVersionId'] ?? 0;

// ?roblox=1: the id is a Roblox id (the archive), never a Polygon asset
$AssetInfo = isset($_GET["roblox"]) ? false : Database::singleton()->run(
	"SELECT * FROM assets WHERE id = :AssetID", 
	[":AssetID" => $AssetID]
)->fetch(\PDO::FETCH_OBJ);

// Not a Polygon asset: thumbnails for the archived Roblox dump come from our own store
// (the archived thumbnail, or the image itself for image assets). Roblox is never asked.
if (!$AssetInfo)
{
	$Dump = $AssetID && ctype_digit((string) $AssetID)
		? Database::singleton()->run("SELECT kind, sha256, thumbnail_sha256 FROM roblox_assets WHERE id = :id", [":id" => $AssetID])->fetch(\PDO::FETCH_OBJ)
		: false;
	$Sha = $Dump ? ($Dump->thumbnail_sha256 ?: (in_array($Dump->kind, ["png", "jpeg", "gif", "bmp"]) ? $Dump->sha256 : null)) : null;
	if (!$Sha || !preg_match('/^[0-9a-f]{64}$/', $Sha)) die(http_response_code(404));
	$Path = \pizzaboxer\ProjectPolygon\Polygon::GetSharedResource("assets/rbxdump/" . substr($Sha, 0, 2) . "/{$Sha}");
	if (!file_exists($Path)) die(http_response_code(404));
	header("Cache-Control: max-age=3600");
	// crop to the requested shape (e.g. 768x432 game cards) when asked for one
	$w = (int) $ResX; $h = (int) $ResY;
	if ($w > 0 && $h > 0 && $w <= 1920 && $h <= 1920 && function_exists("imagecreatefromstring"))
	{
		$src = @imagecreatefromstring(file_get_contents($Path));
		if ($src)
		{
			$sw = imagesx($src); $sh = imagesy($src);
			$scale = max($w / $sw, $h / $sh);
			$cw = (int) round($w / $scale); $ch = (int) round($h / $scale);
			$dst = imagecreatetruecolor($w, $h);
			imagealphablending($dst, false);
			imagesavealpha($dst, true);
			imagecopyresampled($dst, $src, 0, 0, (int) (($sw - $cw) / 2), (int) (($sh - $ch) / 2), $w, $h, $cw, $ch);
			header("Content-Type: image/png");
			imagepng($dst);
			die();
		}
	}
	header("Content-Type: " . (mime_content_type($Path) ?: "image/png"));
	header("Content-Length: " . filesize($Path));
	readfile($Path);
	die();
}

if (false)
{
	if (($AssetID && is_numeric($AssetID)) || ($AssetVersionID && is_numeric($AssetVersionID)))
	{
		// thumbnailasset.ashx died and it was like the only endpoint 
		// that supported fetching from asset version ids so here we are

		$context  = stream_context_create([
			"http" =>[
				"method"  => "GET",
				"header"  => "User-Agent: Roblox/WinInet",
				"ignore_errors" => true
		    ]
		]);

		$parameters = [
			"assetId" => 0,
			"assetVersionId" => 0,
			"width" => $ResX,
			"height" => $ResY,
			"imageFormat" => "Png",
			"thumbnailFormatId" => 296,
			"overrideModeration" => "false"
		];

		if ($AssetID)
		{
			$parameters["assetId"] = $AssetID;
		}
		else if ($AssetVersionID)
		{
			$parameters["assetVersionId"] = $AssetVersionID;
		}

		$parametersHttp = http_build_query($parameters);

		// alternate URL: https://assetgame.roblox.com/asset/request-thumbnail-fix?
		$serviceRequest = file_get_contents("https://assetgame.roblox.com/Thumbs/Asset.asmx/RequestThumbnail_v2?{$parametersHttp}", false, $context); $responseHeaders = http_get_last_response_headers() ?? [""];

		if ($responseHeaders[0] == "HTTP/1.1 200 OK")
		{
			$serviceResponse = json_decode($serviceRequest);
			http_response_code(302);
			header("Location: {$serviceResponse->d->url}");
		}
		else
		{
			header($responseHeaders[0]);
			die();
		}
	}

	die();
}

if ([$ResX, $ResY] == [420, 230])
{
	$ResX = 768;
	$ResY = 432;
}

redirect(Thumbnails::GetAsset($AssetInfo, $ResX, $ResY));