Skip to content

getimagesize() warning on fileupload field with SVG files #345

@Daaaaad

Description

@Daaaaad

Context

When using a fileupload field to receive an SVG file, PrestaShop logs a PHP warning each time the block schema is hydrated for the BO panel:

Warning: Cannot use bool as array in
modules/prettyblocks/classes/prettyblocks/core/FieldCore.php on line 465

In dev environments with display_errors = 1, this warning is visible in the BO and pollutes the interface. In production with display_errors = 0, it goes to the error log but spams it on every BO refresh.

Root cause

FieldCore.php, around lines 449–475, in the method that formats a fileupload field value, contains:

if (!empty($value['url'])) {
    $path = \HelperBuilder::pathFormattedFromUrl($value['url']);

    if (file_exists($path)) {
        $size = filesize($path);

        if ($value['mediatype'] == 'image') {
            list($width, $height) = getimagesize($path);
        }
    }
}

getimagesize() is a PHP function intended for raster images (PNG / JPG / GIF / WebP / etc.). For an SVG file (which PrettyBlocks correctly detects as mediatype = 'image' because SVG is image/svg+xml), getimagesize() returns false. The subsequent list(...) = false triggers the "Cannot use bool as array" warning in PHP 7.x and PHP 8.x.

Steps to reproduce

  1. Declare a custom block with a fileupload field in a third-party module:
'icon' => [
    'type' => 'fileupload',
    'label' => 'Icon (SVG)',
    'path' => '$/modules/yourmodule/views/images/',
    'default' => ['url' => ''],
],
  1. In the BO, upload any .svg file (e.g. a Lucide or Heroicons icon).
  2. Save and re-open the block configuration in the BO.
  3. Observe the PHP warning in display_errors / error log.

Suggested fix

Defensive check before destructuring the getimagesize() return value:

if ($value['mediatype'] == 'image') {
    $imageSize = @getimagesize($path);
    if (is_array($imageSize)) {
        list($width, $height) = $imageSize;
    }
}

The is_array guard handles SVG (and any other case where getimagesize fails) without spamming warnings. SVG files won't expose width / height via this code path — but that's expected behavior since SVG is vector and doesn't have intrinsic pixel dimensions in the same way. They can be parsed separately if needed (e.g. via simplexml_load_file to read the viewBox attribute), but a simple skip is a reasonable default.

Why it matters

SVG uploads are a common use case for custom blocks (icons in reassurance bandeau, badges, decorative elements). Third-party module developers should be able to use fileupload for SVG without polluting logs on every BO refresh.

Willing to contribute

Happy to open a PR with the fix above. Let me know if you'd prefer a different defensive style (e.g. early return, ?: null coalescing, etc.) or if SVG-specific handling (reading viewBox for natural dimensions) is desired alongside.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions