rokka-io / imagine-vips

libvips adapter for php imagine
https://rokka.io
Other
41 stars 8 forks source link

Issue with loading animated gif file from buffer #22

Open yellow1912 opened 2 years ago

yellow1912 commented 2 years ago

For some reason, when loading gif from string my loader is VipsForeignLoadNsgifBuffer instead of VipsForeignLoadGifBuffer. The n option is not applied correctly.

The following addition fixes the issue (for now):

protected function getLoadOptions($loader, $loadOptions = [])
    {
        $options = [];
        switch ($loader) {
            case 'VipsForeignLoadJpegFile':
            case 'VipsForeignLoadJpegBuffer':
                $options['autorotate'] = true;
                break;
            case 'VipsForeignLoadHeifFile':
            case 'VipsForeignLoadHeifBuffer':
                $options['autorotate'] = true;
                $options['n'] = -1; // not sure this should be enabled by default, to discuss
                break;
            case 'VipsForeignLoadGifFile':
            case 'VipsForeignLoadGifBuffer':
            case 'VipsForeignLoadNsgifBuffer':
                $options['n'] = -1; // not sure this should be enabled by default, to discuss
                break;
        }
        $options = array_merge($loadOptions, $options);
        // FIXME: remove not allowed options

        if (isset($options['shrink'])) {
            switch ($loader) {
                case 'VipsForeignLoadJpegFile':
                case 'VipsForeignLoadJpegBuffer':
                case 'VipsForeignLoadWebpFile':
                case 'VipsForeignLoadWebpBuffer':
                    break;
                default:
                    unset($options['shrink']);
               }
        }
        return $options;
    }

My current vips config is:

enable debug: no enable deprecated library components: yes enable modules: no use fftw3 for FFT: no accelerate loops with orc: yes ICC profile support with lcms: yes (lcms2) zlib: yes text rendering with pangocairo: no font file support with fontconfig: RAD load/save: yes Analyze7 load/save: yes PPM load/save: yes GIF load: yes GIF save with cgif: yes EXIF metadata support with libexif: yes JPEG load/save with libjpeg: yes (pkg-config) JXL load/save with libjxl: no (dynamic module: no) JPEG2000 load/save with libopenjp2: no PNG load with libspng: no PNG load/save with libpng: yes (pkg-config libpng >= 1.2.9) quantisation to 8 bit: yes TIFF load/save with libtiff: no image pyramid save: yes HEIC/AVIF load/save with libheif: no (dynamic module: no) WebP load/save with libwebp: yes PDF load with PDFium: no PDF load with poppler-glib: no (dynamic module: no) SVG load with librsvg-2.0: yes EXR load with OpenEXR: no OpenSlide load: no (dynamic module: no) Matlab load with matio: no NIfTI load/save with niftiio: no FITS load/save with cfitsio: no Magick package: none (dynamic module: no) Magick API version: none load with libMagickCore: no save with libMagickCore: no

Should I send a PR for this fix?

chregu commented 2 years ago

A PR would be fine. I actually never used libnsgif, that may be the issue why I didn't catch it. Somehow strange that there are two different loaders for that, but anyway, easy fix and no potential harm.

andi-wr commented 7 months ago

Somehow strange that there are two different loaders for that, but anyway, easy fix and no potential harm.

See here: https://stackoverflow.com/questions/75772029/vips-warning-error-in-tile-0-x-1104-gif-crashing-website-ruby-on-rails#comment133680071_75777349

libvips used to use giflib for loading GIFs, but switched to the (much better) nsgif loader in 8.11