Search code examples
vips

Getting error while try to write gif image to buffer


I am using PHP libvips library when I am using this function writeToBuffer for write to buffer the image it gives me below types of error.

Fatal error: Uncaught Jcupitt\Vips\Exception: magicksave_buffer: libMagick error: no decode delegate for this image format `' @ error/blob.c/ImagesToBlob/2413

Note - This error occurs only when my image type is gif

$imagePathInfo = pathinfo($inputFileName);
$imgExtension = $imagePathInfo['extension'];
$img = Vips\Image::newFromFile($inputFileName, ['access' => 'sequential']);
$img = $img->writeToBuffer('.' . $imgExtension);

Image which I am trying


Solution

  • By default libvips will only load the first frame of an animation. To load all frames, set the n parameter (number of pages) to -1. Use:

    $img = Vips\Image::newFromFile($inputFileName, [
        'access' => 'sequential', 
        'n' => -1
    ]);
    

    libvips 8.11 uses imagemagick to write GIF images. You need to tell imagemagick what format to write in with the format parameter, eg.:

    $img = $img->writeToBuffer('.' . $imgExtension, [
        'format' => $imgExtension
    ]);
    

    libvips 8.12 (due by the end of Nov 2021) will automatically pass the correct format value to imagemagick, and also has a dedicated and much faster GIF writer.