1

I have a site where I serve quite the amount of images which are saved on Google Cloud Storage. I have set generateTransformsBeforePageLoad to true in my general config because I'm caching the output for Fastly (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.

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.

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?

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.

-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.

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. – narration_sd May 15 '16 at 19:11
  • 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 May 15 '16 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.