download($sourceUrl); if ($source === null) { return null; } $resized = $this->resize($source); if ($resized === null) { return null; } return $this->store($resized, $token); } catch (Throwable) { return null; } } private function download(string $url): ?string { $response = Http::timeout(30)->get($url); if (! $response->successful()) { return null; } $body = $response->body(); return strlen($body) > self::MAX_SOURCE_BYTES ? null : $body; } private function resize(string $source): ?string { $info = @getimagesizefromstring($source); if ($info === false) { return null; } [$width, $height] = $info; if ($width < 1 || $height < 1 || $width * $height > self::MAX_SOURCE_PIXELS) { return null; } if ($width <= self::MAX_WIDTH) { return $source; } $image = @imagecreatefromstring($source); if ($image === false) { return null; } $targetHeight = (int) max(1, round($height * (self::MAX_WIDTH / $width))); $resized = imagescale($image, self::MAX_WIDTH, $targetHeight); imagedestroy($image); if ($resized === false) { return null; } ob_start(); try { imagejpeg($resized, null, self::JPEG_QUALITY); } finally { $bytes = (string) ob_get_clean(); imagedestroy($resized); } return $bytes === '' ? null : $bytes; } private function store(string $bytes, string $token): ?string { $response = (new LemmyRequest($this->instance, $token)) ->postMultipart('pictrs/image', 'images[]', $bytes, 'thumbnail.jpg'); if (! $response->successful()) { return null; } $file = $response->json('files.0.file'); if (! is_string($file) || $file === '') { return null; } // $instance is a full scheme-qualified URL — platform_accounts.instance_url is validated as a URL. return sprintf('%s/pictrs/image/%s', rtrim($this->instance, '/'), $file); } }