Skip to content

Only load uploaded media as image when it is an image - #325

Merged
jannescb merged 2 commits into
litstack:3.xfrom
jannescb:fix/media-repository-pdf-upload
Jun 2, 2026
Merged

Only load uploaded media as image when it is an image#325
jannescb merged 2 commits into
litstack:3.xfrom
jannescb:fix/media-repository-pdf-upload

Conversation

@jannescb

@jannescb jannescb commented Jun 2, 2026

Copy link
Copy Markdown
Collaborator

Summary

MediaRepository::storeMediaToModel currently calls ImageFactory::load(\$request->media->path()) on every uploaded file in order to populate original_dimensions for image collections. The mime-type check on the line below only guards using the result — the load itself runs unconditionally.

For non-image uploads (PDFs, ZIPs, etc.) this throws Spatie\Image\Exceptions\CouldNotLoadImage from GD's imagecreatefromstring() and the upload fails with a 500. As a result non-image files can no longer be attached to media collections (e.g. a \$form->file('file')->accept('application/pdf') PDF field).

Example error from production with a PDF upload to a file collection:

production.ERROR: Could not load image at path `/tmp/phpXXXXXX` :
imagecreatefromstring(): Data is not in a recognized format
 at vendor/spatie/image/src/Drivers/Gd/GdDriver.php:91
 at vendor/spatie/image/src/Image.php:51 (Image::loadFile)
 at vendor/spatie/laravel-medialibrary/src/Support/ImageFactory.php (ImageFactory::load)
 at vendor/litstack/litstack/src/Crud/Repositories/MediaRepository.php:141

Change

Move the ImageFactory::load() call inside the existing Str::startsWith(\$mime, 'image') guard so it only runs when the upload actually is an image. Behavior for image uploads is unchanged.

-        \$image = ImageFactory::load(\$request->media->path());
-
         if (Str::startsWith(\$request->media->getClientMimeType(), 'image')) {
+            \$image = ImageFactory::load(\$request->media->path());
+
             \$customProperties['original_dimensions'] = [
                 'width'  => \$image->getWidth(),
                 'height' => \$image->getHeight(),
             ];
         }

Test plan

  • Upload an image (jpg/png) via a media \$form->image() field — custom_properties.original_dimensions is still populated.
  • Upload a PDF via a \$form->file() media field that accepts application/pdf — request succeeds, file is attached.

jannescb added 2 commits June 2, 2026 10:20
`MediaRepository::storeMediaToModel` called `ImageFactory::load()` on
every uploaded file to capture the original dimensions for image
collections. For non-image uploads (PDFs, ZIPs, etc.) this throws
`Spatie\Image\Exceptions\CouldNotLoadImage` from GD's
`imagecreatefromstring()` and the request fails with a 500, so non-image
files can no longer be attached to media collections.

Move the `ImageFactory::load()` call inside the existing mime-type
guard so it only runs when the upload actually is an image.
@jannescb
jannescb merged commit 36bce32 into litstack:3.x Jun 2, 2026
1 check passed
@jannescb
jannescb deleted the fix/media-repository-pdf-upload branch June 2, 2026 08:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant