<p>No, Craft will not be able to generate JPGs from SVGs out of the box (I tested it), but it should be relatively easy to write a plugin handling it.</p>

<p>As GD doesn't support SVG, ImageMagick support is a must. Craft actually <em>does</em> use ImageMagick, if its installed on your server – so your first step would be to check for ImageMagick support. An easy way to do that, is to use Craft's <a href="http://buildwithcraft.com/classreference/services/ImagesService#isImagick-detail" rel="nofollow noreferrer">ImagesService</a>.</p>

<p>From there, the actual PHP code generating the JPG should be relatively straight-forward – check <a href="https://stackoverflow.com/questions/10289686/rendering-an-svg-file-to-a-png-or-jpeg-in-php">this Stack Overflow thread</a> for a concise example (not tested the mentioned code myself).</p>

<p>Note: Be advised that Craft actually implements all of its image manipulation through the <a href="https://imagine.readthedocs.org/en/latest/" rel="nofollow noreferrer">Imagine</a> library – I'm pretty sure Imagine would handle the generation, but if you need to work with ImageMagick directly you could do</p>

<pre><code>$factory = new \Imagine\Imagick\Imagine();
$svg = $factory-&gt;open( $path );
$imagick = $svg-&gt;getImagick();
</code></pre>

<p>Finally, when it comes to implementing the JPG generation you have a few options – for example you could <a href="https://cameronspear.com/blog/craft-cms-twig-filter-extension/" rel="nofollow noreferrer">create a Twig filter</a>:</p>

<pre><code>&lt;img src="{{ image | svgFallback }}" /&gt;
</code></pre>

<p>You could also implement it as a <a href="http://buildwithcraft.com/docs/plugins/variables" rel="nofollow noreferrer">template variable method</a>:</p>

<pre><code>&lt;img src="{{ craft.yourPluginName.getSvgFallback( image ) }}" /&gt;
</code></pre>

<p>...or you could even hook into <a href="http://buildwithcraft.com/classreference/services/AssetsService#events" rel="nofollow noreferrer">AssetsService events</a> if you needed to generate the fallback automatically.</p>

<p>Updated answer: The original question asks if "image transforms can be modified to handle this". The short answer is no – ideally there would be a <code>beforeTransformApplied</code> hook or something like that, but as of 2.3 there isn't (see <a href="https://craftcms.stackexchange.com/questions/1025/is-it-possible-to-hook-into-image-transforms">this thread</a> for reference). My opinion is that your best bet is creating a variable method or Twig filter, as outlined above.</p>

CC BY-SA 3.0
2015-02-23T01:05:43.943Z
2015-02-23T08:22:02.007Z
2017-05-23T12:38:53.957Z
Mats Mikkel Rummelhoff
<p>I'm wondering how I can dynamically generate a JPG fallback image from an SVG. Can image transforms be modified to handle this?</p>

CC BY-SA 3.0
2015-01-09T09:34:48.527Z
I don't believe so, because technically speaking SVG's aren't 'images' in the true sense of the word. A reasonable workaround would be to add a 'JPG fallback' asset field to your assets, so when an SVG is uploaded you can select the file you want to fallback to from there.
Jamie Wade
CC BY-SA 3.0
2015-01-09T16:57:56.403Z
@JamieWade Unfortunately that solution won't work in my case. Based on a little more research ImageMagick is able to do the operation server side, but that isn't the library Craft uses. I wonder if there is a way for Craft to plug into a different image processor?
kepano
<image-transforms>
Dynamically generating a JPG from SVG
# Dynamically generating a JPG from SVG

(Asked by: username_0 on 2015-01-09)

I'm wondering how I can dynamically generate a JPG fallback image from an SVG. Can image transforms be modified to handle this?

**Tags:** image-transforms

> **Comments:**
> **username_1**: I don't believe so, because technically speaking SVG's aren't 'images' in the true sense of the word. A reasonable workaround would be to add a 'JPG fallback' asset field to your assets, so when an SVG is uploaded you can select the file you want to fallback to from there.  
> **username_0**: @JamieWade Unfortunately that solution won't work in my case. Based on a little more research ImageMagick is able to do the operation server side, but that isn't the library Craft uses. I wonder if there is a way for Craft to plug into a different image processor?  

---

## 1 answers

### Answer by username_2 on 2015-02-23. Score: 4

No, Craft will not be able to generate JPGs from SVGs out of the box (I tested it), but it should be relatively easy to write a plugin handling it.
As GD doesn't support SVG, ImageMagick support is a must. Craft actually does use ImageMagick, if its installed on your server – so your first step would be to check for ImageMagick support. An easy way to do that, is to use Craft's ImagesService.
From there, the actual PHP code generating the JPG should be relatively straight-forward – check this Stack Overflow thread for a concise example (not tested the mentioned code myself).
Note: Be advised that Craft actually implements all of its image manipulation through the Imagine library – I'm pretty sure Imagine would handle the generation, but if you need to work with ImageMagick directly you could do
$factory = new \Imagine\Imagick\Imagine();
$svg = $factory->open( $path );
$imagick = $svg->getImagick();

Finally, when it comes to implementing the JPG generation you have a few options – for example you could create a Twig filter:


You could also implement it as a template variable method:


...or you could even hook into AssetsService events if you needed to generate the fallback automatically.
Updated answer: The original question asks if "image transforms can be modified to handle this". The short answer is no – ideally there would be a beforeTransformApplied hook or something like that, but as of 2.3 there isn't (see this thread for reference). My opinion is that your best bet is creating a variable method or Twig filter, as outlined above.


---

