spacecatninja / craft-imager-x

Image transforms, optimizations and manipulations for your Craft CMS site.
Other
26 stars 16 forks source link

Functional questions about generated file name and cache duration #223

Closed romainpoirier closed 1 year ago

romainpoirier commented 1 year ago
### I'm submitting a...

Description

I've got functional questions to better understand how the plugin works/

How the Imager X image file name is generated bases on the source file? Example: bike.jpg could end up in a bike_5ae402c9837860cd6e4f65e304b3a5fa.jpg file. What is composing the 5ae402c9837860cd6e4f65e304b3a5fa, how this suffix is generated? I'm trying to evaluate if URLs could be easily identified.

What's the duration of a generated Imager X transformed image? When will it be deleted / refreshed, how long is it cached? Can I force a refresh from the Twig craft.imagerx method?

Thank you!

Additional info

aelvan commented 1 year ago

Hi,

Based on the transform parameters passed to Imager, a transform string will be generated. This is a unique string that will retain the information about the transform being done in a truncated, normalized format. For instance, the following transforms generate the following transformString:

{ width: 1600 } = W1600
{ width: 1600, height: 900 } = W1600_H900
{ width: 1600, ratio: 16/9 } = W1600_H900
{ width: 1600, ratio: 16/9, effects: { sharpen: true }  } = W1600_H900_E_sharpen-1

And so on...

How this relates to the filename is based on the filenamePattern config setting. The default value of this is {basename}_{transformString|hash}.{extension}. This means that the basename of the file (in your case bike) is concatenated with a hashed version of the transformString, with an underscore inbetween, and the extension is set to the resulting extension of the transform. You can read more about the different options in the documentation.

The hash is a simple md5 hash, which means that it's quite simple to decrypt, it was not chosen for security in mind, more as a way to create a shorter URL-safe transform string. There is a shortHash method that can be used instead of hash, which incidentally makes the string impossible to decrypt, since it will truncate the original string, and throw away some of the data. This could introduce filename collisions, but unless you do a ton of different transforms on the same assets, it's highly unlikely you'd run into this. But again, this wasn't made to make the transformString hard to decrypt, just to make filenames nicer.

What's the duration of a generated Imager X transformed image? When will it be deleted / refreshed, how long is it cached?

This is determined by the cacheDuration config setting. By default it's 14 days, but in the next release I'll change it to indefinitely (by setting it to false) after considering #214.

Transforms on Assets (ie not external urls) will be deleted if an asset is updated. You can also enable the removeTransformsOnAssetFileops config setting if you want transforms deleted when assets are moved or deleted, just read the warning in the docs to make sure you understand the implications of this.

Can I force a refresh from the Twig craft.imagerx method?

Not really. The if the transform changes, the transformed file will be regenerated of course. But there's not really a way to force a given request. If you describe what you're trying to achieve, maybe I can point you to a solution though?