<p>You could use my <a href="https://github.com/aelvan/Preparse-Field-Craft" rel="nofollow">Preparse Field plugin</a> to generate transforms when an entry (or any other element) is saved. </p>

<p>Create a preparse field and create some twig that makes the same transforms that you do in your template. Don't care about the output, the only thing you want it to do is generate the images on save. Assuming you attach the field to an entry, and you have a field named <code>myImageField</code>, the code could look something like this:</p>

<pre><code>{% if entry.myImageField | length %}
  {% for image in entry.myImageField %}
    {{ image.getUrl({ width: 1600 })
    {{ image.getUrl({ width: 1200 })
    {{ image.getUrl({ width: 800 })
    {{ image.getUrl({ width: 400 })
  {% endfor %}
{% endif %}
</code></pre>

<p>When you save the entry, this code will run, and create any new image transforms. When the entry is displayed in the front-end, the image transforms will already have been created.</p>

<p>Beware that this will not actually increase the speed of the transforms, only shift the time it takes from the front-end user over to the admin. But I find that this is more acceptable in most of my use-cases.</p>

CC BY-SA 3.0
2016-05-16T08:32:37.067Z
2016-05-16T08:32:37.067Z
André Elvan
<p>The best advice I could find on this is in another SE page: <a href="https://craftcms.stackexchange.com/questions/7140/any-way-to-manually-generate-a-transform-on-an-asset-source">Any way to manually generate a transform on an asset source?</a></p>

<p>Seems to boil down to two things:</p>

<ul>
<li><p>Brad suggests rightly that trying to do this all at once can be a heavy job, and so to ask Craft to do so may be asking for trouble, e.g. timeouts on PHP, cutouts on web hoster runaway safeties, etc..</p></li>
<li><p>OTOH the always thoughtful @carlcs offers an offline script suggestion as to how to do this, provided you monitor and arrange batch sizes that would work.</p></li>
</ul>

<p>I'm thinking this might be another job best approached as a yiic plugin, so that you would call it from the *nix command line rather than over the web. This would give more scope as to how long the job could run, possibly, and anyway be a more convenient place to arrange the job as a series of batches, if the size of it makes that needed. </p>

<p>Yiic commands run the whole Craft armature, so you can still program the main action in a template.</p>

CC BY-SA 3.0
2016-05-15T19:27:02.030Z
2016-05-15T19:27:02.030Z
2017-04-13T12:51:00.160Z
narration_sd
<p>You can set transforms in the CMS, then in your template you call the actual name of the transform you make in the dashboard instead, you still use <code>getUrl</code>, but you can generate the assets with "Update Image Assets" on the settings page in the dashboard.</p>

<p>read more here: <a href="https://craftcms.com/docs/image-transforms#applying-cp-defined-transforms-to-images" rel="nofollow">https://craftcms.com/docs/image-transforms#applying-cp-defined-transforms-to-images</a></p>

<p>note*: The fixed nav covers this area slightly now...</p>

<p>Also, I think I remember hearing/reading it's better for performance for assets not to be cached with varnish too.</p>

CC BY-SA 3.0
2016-05-15T19:11:48.607Z
I think you'll find that the button you're referring to is called 'Update Asset Indexes', and that it does _not_ generate the transform assets. What it does is index images you may have added by ftp etc. rather than by uploading the through the Control Panel. Indexing doesn't generate the transformed results.
narration_sd
CC BY-SA 3.0
2016-05-15T19:41:49.550Z
Sorry to touch on another, but I'm not sure what you were getting at in talking about not caching when using Varnish. The transformed images aren't caching; they're the smaller image originals. Also, in a cached site you'd likely want images to be on a CDN for best local access, so that Varnish wouldn't be involved with them, no?
narration_sd
CC BY-SA 3.0
2016-05-15T18:42:49.980Z
2016-05-15T18:42:49.980Z
joep
<p>I have a site where I serve quite the amount of images which are saved on Google Cloud Storage. I have set <a href="https://craftcms.com/docs/config-settings#generateTransformsBeforePageLoad" rel="nofollow">generateTransformsBeforePageLoad</a> to true in my general config because I'm caching the output for <a href="https://www.fastly.com/" rel="nofollow">Fastly</a> (based on Varnish). The downside is that on first request it's awfully slow and at one point even returns a 503 error.  </p>

<p>Is there a way that assets can be transformed before <code>getUrl()</code> is being called? Other solutions are more than welcome. </p>

<image-transforms><varnish>
Is there a way to manually trigger an asset transform before calling getUrl()?
# Is there a way to manually trigger an asset transform before calling getUrl()?

(Asked by: username_0 on 2015-04-21)

I have a site where I serve quite the amount of images which are saved on Google Cloud Storage. I have set [generateTransformsBeforePageLoad](https://craftcms.com/docs/config-settings#generateTransformsBeforePageLoad) to true in my general config because I'm caching the output for [Fastly](https://www.fastly.com/) (based on Varnish). The downside is that on first request it's awfully slow and at one point even returns a 503 error.

Is there a way that assets can be transformed before `getUrl()` is being called? Other solutions are more than welcome.

**Tags:** image-transforms, varnish

---

## 3 answers

### Answer by username_1 on 2016-05-16. Score: 3

You could use my Preparse Field plugin to generate transforms when an entry (or any other element) is saved. 
Create a preparse field and create some twig that makes the same transforms that you do in your template. Don't care about the output, the only thing you want it to do is generate the images on save. Assuming you attach the field to an entry, and you have a field named myImageField, the code could look something like this:
{% if entry.myImageField | length %}
  {% for image in entry.myImageField %}
    {{ image.getUrl({ width: 1600 })
    {{ image.getUrl({ width: 1200 })
    {{ image.getUrl({ width: 800 })
    {{ image.getUrl({ width: 400 })
  {% endfor %}
{% endif %}

When you save the entry, this code will run, and create any new image transforms. When the entry is displayed in the front-end, the image transforms will already have been created.
Beware that this will not actually increase the speed of the transforms, only shift the time it takes from the front-end user over to the admin. But I find that this is more acceptable in most of my use-cases.


---

### Answer by username_2 on 2016-05-15. Score: 2

The best advice I could find on this is in another SE page: [Any way to manually generate a transform on an asset source?](https://craftcms.stackexchange.com/questions/7140/any-way-to-manually-generate-a-transform-on-an-asset-source)

Seems to boil down to two things:

* Brad suggests rightly that trying to do this all at once can be a heavy job, and so to ask Craft to do so may be asking for trouble, e.g. timeouts on PHP, cutouts on web hoster runaway safeties, etc..
* OTOH the always thoughtful @carlcs offers an offline script suggestion as to how to do this, provided you monitor and arrange batch sizes that would work.

I'm thinking this might be another job best approached as a yiic plugin, so that you would call it from the \*nix command line rather than over the web. This would give more scope as to how long the job could run, possibly, and anyway be a more convenient place to arrange the job as a series of batches, if the size of it makes that needed.

Yiic commands run the whole Craft armature, so you can still program the main action in a template.


---

### Answer by username_3 on 2016-05-15. Score: -2

You can set transforms in the CMS, then in your template you call the actual name of the transform you make in the dashboard instead, you still use `getUrl`, but you can generate the assets with "Update Image Assets" on the settings page in the dashboard.

read more here: <https://craftcms.com/docs/image-transforms#applying-cp-defined-transforms-to-images>

note\*: The fixed nav covers this area slightly now...

Also, I think I remember hearing/reading it's better for performance for assets not to be cached with varnish too.

> **Comments:**
> **username_2**: I think you'll find that the button you're referring to is called 'Update Asset Indexes', and that it does _not_ generate the transform assets. What it does is index images you may have added by ftp etc. rather than by uploading the through the Control Panel. Indexing doesn't generate the transformed results.  
> **username_2**: Sorry to touch on another, but I'm not sure what you were getting at in talking about not caching when using Varnish. The transformed images aren't caching; they're the smaller image originals. Also, in a cached site you'd likely want images to be on a CDN for best local access, so that Varnish wouldn't be involved with them, no?  

---

