Strapi: VipsJpeg: Premature end of input file error

Viewed 246

Problem:

When uploading an image from Strapi (the Media Library (UI)) it shows me the error: VipsJpeg: Premature end of input file.

I tried to change the format from jpg to png but it still shows the same error. On the other hand, I was testing with other images (jpg/png format). and it uploads correctly.

I would like to know the reason for the error, and if there is any solution. I will appreciate your answer

Uploading image to strapi: Media Library

Image Info

  • JPEG image - 19 KB
  • Dimensions: 750 × 938

enter image description here

Error:

Error: VipsJpeg: Premature end of input file

config/plugins.js

Default configuration, provided by strapi

module.exports = ({ env }) => ({
  upload: {
    config: {
      breakpoints: {
        xlarge: 1920,
        large: 1000,
        medium: 750,
        small: 500,
        xsmall: 64,
      },
    },
  },
});

1 Answers

It Looks like this is an issue in sharp , Strapi server stop when it encounter this issue and because of that it return network error 502 . to solve it in my code i had to extend the plugin-upload code and to change this const in that plugin to stop causing server to fail , then image can uploaded successfully , maybe without proper optimization but without failing . on node_module/@strapi/plugin-upload/server/services/image-manipulation.js change this const

const transformer = sharp();

to this

const transformer = sharp({ failOnError: false });

Steps to Apply it to Strapi as plugin extension :

1 - extend the plugin-upload plugin by creating ( upload ) folder within src/extensions folder ( create extensions folder if it does not exist ) 2 - Create a file named ( strapi-server.js ) with this content to overwrite the image-manupilation service :

'use strict';

const imageManipulate = require('./server/services/image-manipulation')

module.exports = (plugin) => {

  plugin.services['image-manipulation'] = imageManipulate;

  return plugin;
};

3 - Copy ( utils folder + services/image-manipulation.js) from node_module/@strapi/plugin-upload/server to your extensions folder .

4 - Edit transformer const in image-manipulation.js as discussed before .


For more information about the sharp issue see here : https://github.com/lovell/sharp/issues/1859

For more information about Extending Strapi Plugin see here : https://docs.strapi.io/developer-docs/latest/development/plugins-extension.html

Related