+ if (is_null($imageData)) {
+ return null;
+ }
+
+ $extension = pathinfo($uri, PATHINFO_EXTENSION);
+ if ($extension === 'svg') {
+ $extension = 'svg+xml';
+ }
+
+ return 'data:image/' . $extension . ';base64,' . base64_encode($imageData);
+ }
+
+ /**
+ * Check if the given path exists and is accessible in the local secure image system.
+ * Returns false if local_secure is not in use, if the file does not exist, if the
+ * file is likely not a valid image, or if permission does not allow access.
+ */
+ public function pathAccessibleInLocalSecure(string $imagePath): bool
+ {
+ /** @var FilesystemAdapter $disk */
+ $disk = $this->getStorageDisk('gallery');
+
+ if ($this->usingSecureRestrictedImages() && !$this->checkUserHasAccessToRelationOfImageAtPath($imagePath)) {
+ return false;
+ }
+
+ // Check local_secure is active
+ return $this->usingSecureImages()
+ && $disk instanceof FilesystemAdapter
+ // Check the image file exists
+ && $disk->exists($imagePath)
+ // Check the file is likely an image file
+ && strpos($disk->mimeType($imagePath), 'image/') === 0;
+ }
+
+ /**
+ * Check that the current user has access to the relation
+ * of the image at the given path.
+ */
+ protected function checkUserHasAccessToRelationOfImageAtPath(string $path): bool
+ {
+ if (strpos($path, '/uploads/images/') === 0) {
+ $path = substr($path, 15);
+ }
+
+ // Strip thumbnail element from path if existing
+ $originalPathSplit = array_filter(explode('/', $path), function (string $part) {
+ $resizedDir = (strpos($part, 'thumbs-') === 0 || strpos($part, 'scaled-') === 0);
+ $missingExtension = strpos($part, '.') === false;
+
+ return !($resizedDir && $missingExtension);
+ });
+
+ // Build a database-format image path and search for the image entry
+ $fullPath = '/uploads/images/' . ltrim(implode('/', $originalPathSplit), '/');
+ $image = Image::query()->where('path', '=', $fullPath)->first();
+
+ if (is_null($image)) {
+ return false;