97 lines
3.1 KiB
PHP
97 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\StoreSettingsMediaRequest;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use RuntimeException;
|
|
|
|
class SettingsMediaController extends Controller
|
|
{
|
|
private const BACKGROUND_MAX_SIDE = 1600;
|
|
|
|
private const BACKGROUND_QUALITY = 86;
|
|
|
|
public function store(StoreSettingsMediaRequest $request): JsonResponse
|
|
{
|
|
$processed = $this->processBackground($request->file('file'));
|
|
$path = $this->backgroundPath((int) $request->user()->id);
|
|
|
|
Storage::disk('local')->put($path, $processed['contents']);
|
|
|
|
return response()->json([
|
|
'data' => [
|
|
'url' => '/settings/media/background?v='.time(),
|
|
'width' => $processed['width'],
|
|
'height' => $processed['height'],
|
|
'mimeType' => 'image/jpeg',
|
|
'size' => strlen($processed['contents']),
|
|
],
|
|
], 201);
|
|
}
|
|
|
|
public function show(Request $request): Response
|
|
{
|
|
$path = $this->backgroundPath((int) $request->user()->id);
|
|
|
|
abort_unless(Storage::disk('local')->exists($path), 404);
|
|
|
|
return response(Storage::disk('local')->get($path), 200, [
|
|
'Content-Type' => 'image/jpeg',
|
|
'Cache-Control' => 'private, max-age=604800',
|
|
]);
|
|
}
|
|
|
|
public function destroy(Request $request): JsonResponse
|
|
{
|
|
Storage::disk('local')->delete($this->backgroundPath((int) $request->user()->id));
|
|
|
|
return response()->json(null, 204);
|
|
}
|
|
|
|
private function backgroundPath(int $userId): string
|
|
{
|
|
return "settings-media/{$userId}/background.jpg";
|
|
}
|
|
|
|
/**
|
|
* @return array{contents: string, width: int, height: int}
|
|
*/
|
|
private function processBackground(UploadedFile $file): array
|
|
{
|
|
$source = imagecreatefromstring((string) file_get_contents($file->getRealPath()));
|
|
|
|
if (! $source) {
|
|
throw new RuntimeException('The uploaded background image could not be processed.');
|
|
}
|
|
|
|
$sourceWidth = imagesx($source);
|
|
$sourceHeight = imagesy($source);
|
|
$scale = min(1, self::BACKGROUND_MAX_SIDE / max($sourceWidth, $sourceHeight));
|
|
$width = max(1, (int) round($sourceWidth * $scale));
|
|
$height = max(1, (int) round($sourceHeight * $scale));
|
|
|
|
$canvas = imagecreatetruecolor($width, $height);
|
|
$background = imagecolorallocate($canvas, 255, 255, 255);
|
|
imagefilledrectangle($canvas, 0, 0, $width, $height, $background);
|
|
imagecopyresampled($canvas, $source, 0, 0, 0, 0, $width, $height, $sourceWidth, $sourceHeight);
|
|
|
|
imagedestroy($source);
|
|
|
|
ob_start();
|
|
imagejpeg($canvas, null, self::BACKGROUND_QUALITY);
|
|
$contents = (string) ob_get_clean();
|
|
imagedestroy($canvas);
|
|
|
|
return [
|
|
'contents' => $contents,
|
|
'width' => $width,
|
|
'height' => $height,
|
|
];
|
|
}
|
|
}
|