Repositories | Polyback | Log in to see private repositories


polyback-website / 5d0cdca2e1e9b3c3c09f52c4f48b8cf10042ac86 / api/studio/thumbnail.php

1,438 bytes | parent directory

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

// Set a place's image from Studio.  POST /api/studio/thumbnail?id=<place>, body: PNG or JPEG
// -> {"success", "thumbnail"}
use pizzaboxer\ProjectPolygon\API;
use pizzaboxer\ProjectPolygon\Studio;
use pizzaboxer\ProjectPolygon\Thumbnails;
use Verot\Upload\Upload;

API::initialize(["method" => "POST"]);
$user = Studio::RequireUser();
$place = Studio::RequirePlace($user, $_GET["id"] ?? 0);
$data = file_get_contents("php://input");
if (!$data || strlen($data) > 8 * 1024 * 1024) API::respond(400, false, "Send a PNG or JPEG image of at most 8 MB");
$info = @getimagesizefromstring($data);
if (!$info || !in_array($info[2], [IMAGETYPE_PNG, IMAGETYPE_JPEG], true)) API::respond(400, false, "That isn't a PNG or JPEG image");

foreach ([[768, 432], [420, 420]] as [$w, $h])
{
	$image = new Upload("base64:" . base64_encode($data));
	$image->allowed = ["image/png", "image/jpg", "image/jpeg"];
	$image->image_convert = "png";
	$image->image_ratio_crop = true;
	try
	{
		// fill the shape, cropping what doesn't fit (no letterboxing)
		Thumbnails::UploadAsset($image, $place->id, $w, $h, ["keepRatio" => false]);
	}
	catch (\Throwable $e)
	{
		API::respond(500, false, "Couldn't process the image");
	}
}
Studio::LogStaff($user, $place, "Changed the image of");
API::respondCustom(["status" => 200, "success" => true, "thumbnail" => Thumbnails::GetAsset($place, 768, 432)]);