Liipimaginebundle: Applying filter to a SVG file does not work

Created on 2 Jul 2016  路  17Comments  路  Source: liip/LiipImagineBundle

Hi there. I have a serious problem resizing SVG files..
For example, I saved the same file as SVG and as PNG.

Code:

<img src="{{ '/uploads/file.png'|imagine_filter('thumbnail') }}" />
<img src="{{ '/uploads/file.svg'|imagine_filter('thumbnail') }}" />

Thumbnail shows only for the first image.

The error is:
Unable to create image for path "uploads/file.svg" and filter "thumbnail". Message was "Unable to open image ~app/../web/uploads/file.svg"
But the file exists in the filesystem!

Also, the generated path for this SVG file is incorrect:
http://localhost:8000/media/cache/resolve/avatar_thumb/uploads/file.svg
Why 'resolve'?

Instead of:
http://localhost:8000/media/cache/avatar_thumb/uploads/file.svg

Need To Investigate 馃攷

Most helpful comment

This still happens with a relatively clean installation with gd driver on Symfony 5.0

All 17 comments

GD, gmagick driver don't yet support SVG, imagick does. Try changing the driver, other than that the bundle needs to probably support it too, maybe it does (haven't tried svg files yet)

SVGs should be supported, the underlying library supports these and includes them in its test suite.

At a glance this looks like an issue with configuration - Could you provide more information about how you have configured the bundle, and as @deviprsd21 has said the driver you are currently using?

Hi,

I'm having the same issue with applying a filter using twig to a svg. I use the driver imagick.

My config and filter:

liip_imagine:
    driver: imagick
    cache: default
    resolvers:
       default:
          web_path:
            web_root: %kernel.root_dir%/../web
            cache_prefix: /media/cache
    filter_sets:
        cache: ~
        exhibitor_booth:
            filters:
                relative_resize: { widen: 450px }

The image doesn't load in Firefox, and in Chrome the whole page doesn't even load.

Using version 1.6.*

@kimwue This isn't much information to figure out why it isn't working for you.

I am seeing the same issue. Using imagick module 3.1.2 and ImageMagick 6.7.7

How can I help debug?

Found out that the generated cache file is encoded as a Jpeg but has a .svg extension. Does that help?

Same issue here. I can't generate svg thumbnails.

Unable to create image for path "uploads/links/398/734262.svg" and filter "link_medium". Message was "An image could not be created from the given input"

As I use a custom ImagineController I've solved it not applying the filter to svg images on filerAction:

$binary = $this->dataManager->find($filter, $path);
if ($binary->getFormat() === 'svg') {
    $filteredBinary = $binary;
} else {
    $filteredBinary = $this->filterManager->applyFilter($binary, $filter);
}

$this->cacheManager->store(
    $filteredBinary,
    $path,
    $filter,
    $resolver
);

I had a hard time to find a solution. It's a little bit hacky but it works. In fact I create a custom twig filter to return the original image uploaded if the file has an svg extension. And I apply the filter after the imagine_filter. Not very clean. Thx for letting me know if you find a better option.

<?php
// AppBundle\Twig\TwigExtension
namespace AppBundle\Twig;

class TwigExtension extends \Twig_Extension
{    
    public function getFilters(){
        return array(
            new \Twig_SimpleFilter('svg', array($this, 'svgFilter')),
        );
    }

    public function svgFilter($liipFilepath){
    $ext = pathinfo($liipFilepath, PATHINFO_EXTENSION);
        $ext === "svg" ? $response = "yourOriginalDirectoryForYourImages/".basename($liipFilepath) : $response = $liipFilepath;
    return $response;
    }
}

and in a template, just do:

# template.html.twig
<img src="{{asset("yourOriginalDirectoryForYourImages/image.svg")|imagine_filter('yourFilter')|svg}}"/>

The problem is caused by Symfony's MIME type guesser and extension guesser. See https://github.com/symfony/symfony/issues/15460#issuecomment-380138425

Here is the workaround for Symfony: https://gist.github.com/teohhanhui/90bbc4a61888ad2e92a160d8459db300

And the workaround for LiipImagineBundle: https://gist.github.com/teohhanhui/cc0d1913d5d0ef3b4c20a51369732d71 (unless Symfony already guesses the correct MIME type for SVG, you need the above workaround too!)

Tested working with gmagick driver.

Got same issue but on a .jpeg file, caused by gd driver, moved to imagick fixed issue

This seems to have been fixed in https://github.com/symfony/symfony/pull/29936 I have not confirmed, if anyone sees this and is still experiencing issues, please open a new issue and we can look at it with the latest data. Thanks! :)

@michellesanver There were 2 issues. 1 of them (in Symfony) has been fixed. The other 1 is in LiipImagineBundle and has not been fixed.

Thanks, reopening this issue :)

This still happens with a relatively clean installation with gd driver on Symfony 5.0

In my case svg filter with imagick fails with:

must specify image size `/tmp/magick-41957n0pSrtuIVsIM' @ error/mvg.c/ReadMVGImage/186

convert -version
Version: ImageMagick 6.9.10-23 Q16 x86_64 20190101 https://imagemagick.org

SVG is properly listed as supported formats in phpinfo.

Probably connected: https://github.com/ImageMagick/ImageMagick/issues/3117

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fueguino84 picture fueguino84  路  3Comments

chiqui3d picture chiqui3d  路  3Comments

fabianbartsch picture fabianbartsch  路  3Comments

olegrom picture olegrom  路  4Comments

robfrawley picture robfrawley  路  4Comments