Server: imagecreatefromstring(): Empty string or invalid image

Created on 6 Dec 2020  路  13Comments  路  Source: nextcloud/server

How to use GitHub

  • Please use the 馃憤 reaction to show that you are affected by the same issue.
  • Please don't comment if you have no relevant information to add. It's just extra noise for everyone subscribed to this issue.
  • Subscribe to receive notifications on status change and new comments.

Steps to reproduce

I believe this error was caused by https://github.com/matiasdelellis/facerecognition retrieving metadata for an image (with corrupt metadata?) in the background.

Expected behaviour

Fail cleanly instead of with an error

Server configuration

Operating system: debian

Web server: nginx

Database: mariadb

PHP version: 7.4

Nextcloud version: 20.0.2

Nextcloud configuration:


Config report

{
    "system": {
        "instanceid": "***REMOVED SENSITIVE VALUE***",
        "passwordsalt": "***REMOVED SENSITIVE VALUE***",
        "secret": "***REMOVED SENSITIVE VALUE***",
        "trusted_domains": [
            "***REMOVED SENSITIVE VALUE***"
        ],
        "datadirectory": "***REMOVED SENSITIVE VALUE***",
        "dbtype": "mysql",
        "version": "20.0.2.2",
        "overwriteprotocol": "https",
        "overwrite.cli.url": "***REMOVED SENSITIVE VALUE***",
        "dbname": "***REMOVED SENSITIVE VALUE***",
        "dbhost": "***REMOVED SENSITIVE VALUE***",
        "dbport": "3306",
        "dbtableprefix": "oc_",
        "mysql.utf8mb4": true,
        "dbuser": "***REMOVED SENSITIVE VALUE***",
        "dbpassword": "***REMOVED SENSITIVE VALUE***",
        "installed": true,
        "mail_from_address": "***REMOVED SENSITIVE VALUE***",
        "mail_smtpmode": "smtp",
        "mail_sendmailmode": "smtp",
        "mail_domain": "***REMOVED SENSITIVE VALUE***",
        "mail_smtpauthtype": "LOGIN",
        "mail_smtpauth": 1,
        "mail_smtphost": "***REMOVED SENSITIVE VALUE***",
        "mail_smtpport": "587",
        "mail_smtpsecure": "tls",
        "mail_smtpname": "***REMOVED SENSITIVE VALUE***",
        "mail_smtppassword": "***REMOVED SENSITIVE VALUE***",
        "maintenance": false,
        "theme": "",
        "loglevel": 3,
        "memcache.local": "\\OC\\Memcache\\APCu",
        "memcache.locking": "\\OC\\Memcache\\Redis",
        "memcache.distributed": "\\OC\\Memcache\\Redis",
        "redis": {
            "host": "***REMOVED SENSITIVE VALUE***",
            "port": 6379
        },
        "updater.release.channel": "stable",
        "trashbin_retention_obligation": "30, 45",
        "integrity.check.disabled": true,
        "app_install_overwrite": [
            "occweb",
            "files_clipboard",
            "activitylog",
            "twofactor_yubikey",
            "keeweb",
            "forms",
            "joplin",
            "drawio",
            "registration",
            "cookbook"
        ],
        "has_rebuilt_cache": true,
        "debug": false,
        "trusted_proxies": "***REMOVED SENSITIVE VALUE***",
        "allow_local_remote_servers": true,
        "enable_previews": true,
        "enabledPreviewProviders": [
            "OC\\Preview\\Movie",
            "OC\\Preview\\PNG",
            "OC\\Preview\\JPEG",
            "OC\\Preview\\GIF",
            "OC\\Preview\\HEIC",
            "OC\\Preview\\BMP",
            "OC\\Preview\\XBitmap",
            "OC\\Preview\\MP3",
            "OC\\Preview\\MP4",
            "OC\\Preview\\TXT",
            "OC\\Preview\\MarkDown",
            "OC\\Preview\\OpenDocument",
            "OC\\Preview\\PDF",
            "OC\\Preview\\Krita"
        ],
        "upgrade.disable-web": true,
        "simpleSignUpLink.shown": false
    }
}

Are you using external storage, if yes which one: local, smb

Are you using encryption: no

Are you using an external user-backend, if yes which one: no

Logs

Nextcloud log (data/nextcloud.log)


Nextcloud log

[PHP] Error: Error: imagecreatefromstring(): Empty string or invalid image at /var/www/html/lib/private/legacy/OC_Image.php#624 at <<closure>>

 0. <<closure>>
    OC\Log\ErrorHandler::onError(2, "imagecreatefrom ... e", "/var/www/html/l ... p", 624, {imagePath: "/tm ... e})
 1. /var/www/html/lib/private/legacy/OC_Image.php line 624
    imagecreatefromstring("")
 2. /var/www/html/apps/facerecognition/lib/Helper/TempImage.php line 112
    OC_Image->loadFromFile("/tmp/oc_tmp_2yq4y5")
 3. /var/www/html/apps/facerecognition/lib/Helper/TempImage.php line 71
    OCA\FaceRecognition\Helper\TempImage->prepareImage()
 4. /var/www/html/apps/facerecognition/lib/BackgroundJob/Tasks/ImageProcessingTask.php line 212
    OCA\FaceRecognition\Helper\TempImage->__construct("/tmp/oc_tmp_2yq4y5", "image/png", 1737600, 512)
 5. /var/www/html/apps/facerecognition/lib/BackgroundJob/Tasks/ImageProcessingTask.php line 123
    OCA\FaceRecognition\BackgroundJob\Tasks\ImageProcessingTask->getTempImage(OCA\FaceRecognit ... 9})
 6. /var/www/html/apps/facerecognition/lib/BackgroundJob/BackgroundService.php line 120
    OCA\FaceRecognition\BackgroundJob\Tasks\ImageProcessingTask->execute(OCA\FaceRecognit ... }})
 7. /var/www/html/apps/facerecognition/lib/Command/BackgroundCommand.php line 138
    OCA\FaceRecognition\BackgroundJob\BackgroundService->execute(1500, false, null, null)
 8. /var/www/html/3rdparty/symfony/console/Command/Command.php line 255
    OCA\FaceRecognition\Command\BackgroundCommand->execute(Symfony\Componen ... {}, Symfony\Componen ... {})
 9. /var/www/html/3rdparty/symfony/console/Application.php line 1000
    Symfony\Component\Console\Command\Command->run(Symfony\Componen ... {}, Symfony\Componen ... {})
10. /var/www/html/3rdparty/symfony/console/Application.php line 271
    Symfony\Component\Console\Application->doRunCommand(OCA\FaceRecognit ... {}, Symfony\Componen ... {}, Symfony\Componen ... {})
11. /var/www/html/3rdparty/symfony/console/Application.php line 147
    Symfony\Component\Console\Application->doRun(Symfony\Componen ... {}, Symfony\Componen ... {})
12. /var/www/html/lib/private/Console/Application.php line 215
    Symfony\Component\Console\Application->run(Symfony\Componen ... {}, Symfony\Componen ... {})
13. /var/www/html/console.php line 100
    OC\Console\Application->run()
14. /var/www/html/occ line 11
    require_once("/var/www/html/console.php")

1. to develop bug

All 13 comments

You reported this kind of problem to https://github.com/matiasdelellis/facerecognition/issues?

Fail cleanly instead of with an error

Mind to explain? https://github.com/nextcloud/server/blob/master/console.php seems to catch the error and print it to console. What else do you need? Such error handling needs to be done by the app itself because the server / console component don't know how to proceed.

Such error handling needs to be done by the app itself because the server

I don't know about this error specifically but there have been many related errors where the author said it needed fixing by core. I am not familiar with error handling in PHP so I'll submit an issue there as well.

cc @matiasdelellis what fix for core do you have in mind?

I can see some of these too when previews can't be generated for some reason.


Nextcloud log

{
  "reqId": "JmwQCaPiW1S6AQJpEPEL",
  "level": 3,
  "time": "2020-12-07T21:42:00+01:00",
  "app": "PHP",
  "method": "GET",
  "url": "/core/preview?fileId=230947&c=5fce937330e7f&x=442&y=442&forceIcon=0",
  "message": {
    "Exception": "Error",
    "Message": "imagecreatefromstring(): Empty string or invalid image at /lib/private/legacy/OC_Image.php#624",
    "Code": 0,
    "Trace": [
      {
        "function": "onError",
        "class": "OC\\Log\\ErrorHandler",
        "type": "::"
      },
      {
        "file": "/lib/private/legacy/OC_Image.php",
        "line": 624,
        "function": "imagecreatefromstring"
      },
      {
        "file": "/lib/private/Preview/Image.php",
        "line": 52,
        "function": "loadFromFile",
        "class": "OC_Image",
        "type": "->"
      },
      {
        "file": "/lib/private/Preview/GeneratorHelper.php",
        "line": 63,
        "function": "getThumbnail",
        "class": "OC\\Preview\\Image",
        "type": "->"
      },
      {
        "file": "/lib/private/Preview/Generator.php",
        "line": 244,
        "function": "getThumbnail",
        "class": "OC\\Preview\\GeneratorHelper",
        "type": "->"
      },
      {
        "file": "/lib/private/Preview/Generator.php",
        "line": 140,
        "function": "getMaxPreview",
        "class": "OC\\Preview\\Generator",
        "type": "->"
      },
      {
        "file": "/lib/private/Preview/Generator.php",
        "line": 109,
        "function": "generatePreviews",
        "class": "OC\\Preview\\Generator",
        "type": "->"
      },
      {
        "file": "/lib/private/PreviewManager.php",
        "line": 190,
        "function": "getPreview",
        "class": "OC\\Preview\\Generator",
        "type": "->"
      },
      {
        "file": "/core/Controller/PreviewController.php",
        "line": 170,
        "function": "getPreview",
        "class": "OC\\PreviewManager",
        "type": "->"
      },
      {
        "file": "/core/Controller/PreviewController.php",
        "line": 143,
        "function": "fetchPreview",
        "class": "OC\\Core\\Controller\\PreviewController",
        "type": "->"
      },
      {
        "file": "/lib/private/AppFramework/Http/Dispatcher.php",
        "line": 169,
        "function": "getPreviewByFileId",
        "class": "OC\\Core\\Controller\\PreviewController",
        "type": "->"
      },
      {
        "file": "/lib/private/AppFramework/Http/Dispatcher.php",
        "line": 100,
        "function": "executeController",
        "class": "OC\\AppFramework\\Http\\Dispatcher",
        "type": "->"
      },
      {
        "file": "/lib/private/AppFramework/App.php",
        "line": 152,
        "function": "dispatch",
        "class": "OC\\AppFramework\\Http\\Dispatcher",
        "type": "->"
      },
      {
        "file": "/lib/private/Route/Router.php",
        "line": 308,
        "function": "main",
        "class": "OC\\AppFramework\\App",
        "type": "::"
      },
      {
        "file": "/lib/base.php",
        "line": 1008,
        "function": "match",
        "class": "OC\\Route\\Router",
        "type": "->"
      },
      {
        "file": "/index.php",
        "line": 37,
        "function": "handleRequest",
        "class": "OC",
        "type": "::"
      }
    ],
    "File": "/lib/private/Log/ErrorHandler.php",
    "Line": 91,
    "CustomMessage": "--"
  },
  "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0",
  "version": "20.0.2.2",
  "id": "5fce93c259435"
}

See the attached image file for reproduction.
https://user-images.githubusercontent.com/12234510/101403545-aefdf300-38d5-11eb-9181-f4a1020157c0.jpeg

cc @rullzer @MorrisJobke I have no way to reproduce yet :(

I have no way to reproduce yet :(

@kesselb even with my attached JPEG? I can reproduce it every time I upload the file trough WebUI.

@acsfer It looks like your image is a webp image with a .jpeg extension, that would explain the error.

EDIT: I cannot reproduce the error with your image (I've uploaded it as .jpeg and .webp). Maybe github has re-encoded the image when you uploaded it?

@Derkades You are right, it is a .webp masked in .jpeg (don't know why)...

Perhaps, imagecreatefromstring() should support it and my host _(GD on php7.4, no Imagick installed)_ supports it too:

GD Support => enabled
GD headers Version => 2.3.0
GD library Version => 2.3.0
FreeType Support => enabled
FreeType Linkage => with freetype
GIF Read Support => enabled
GIF Create Support => enabled
JPEG Support => enabled
PNG Support => enabled
WBMP Support => enabled
XPM Support => enabled
XBM Support => enabled
WebP Support => enabled
BMP Support => enabled
TGA Read Support => enabled

Is the preview generated on your side?

It shows a preview for the "jpeg" image, not for the webp image:
image

echo "<?php var_dump(gd_info());" | php
array(14) {
  ["GD Version"]=>
  string(26) "bundled (2.1.0 compatible)"
  ["FreeType Support"]=>
  bool(true)
  ["FreeType Linkage"]=>
  string(13) "with freetype"
  ["GIF Read Support"]=>
  bool(true)
  ["GIF Create Support"]=>
  bool(true)
  ["JPEG Support"]=>
  bool(true)
  ["PNG Support"]=>
  bool(true)
  ["WBMP Support"]=>
  bool(true)
  ["XPM Support"]=>
  bool(false)
  ["XBM Support"]=>
  bool(true)
  ["WebP Support"]=>
  bool(true)
  ["BMP Support"]=>
  bool(true)
  ["TGA Read Support"]=>
  bool(true)
  ["JIS-mapped Japanese Font Support"]=>
  bool(false)
}

I'm on S3 as primary, maybe this affects somehow the preview generation too?

I am using standard storage, local external storage and smb external storage. In this case (my original error) it looks like the file was in SMB external storage and downloaded to /tmp for analysis

Was this page helpful?
0 / 5 - 0 ratings