croxton / imgixer

Generate Imgix URLs in Craft 3
MIT License
11 stars 4 forks source link

Light-switch on asset to control auto-enhance within the config file #10

Closed terryupton closed 2 days ago

terryupton commented 1 year ago

Hey @croxton Hope you are well.

I have a light-switch on each asset that is defaulted to on to auto enhance images in imgix. For most images, this is great and the auto enhancement looks good. However, there is the odd instance where the original image looks better than the enhanced version. So I wanted to offload this control on a per image basis via the CP.

What I am trying to do is something similar to what I have done in ImagerX before and this is to set the default params depending on if this image field is on or off. However, I am not sure if this is possible with imgixer or if I have just not applied the correct code/syntax.

Here is what I have tried:

$default_params = array(
  'provider'      => 'imgix',
  'endpoint'      => getenv('IMGIX_URL'),
  'privateKey'    => getenv('IMGIX_KEY'),
  'signed'        => true,
  'defaultParams' => array(
    'auto' => static function ($asset) {
      if ($asset instanceof \craft\elements\Asset) {
        if ($asset->getFieldValue('autoEnhanceImage') === false) {
          return 'compress,format';
        }
      }
      return 'compress,format,enhance';
    },
    'fit'  => 'crop',
    'crop' => 'focalpoint',
    'ar'   => '8:5',
    'step' => '100',
    'q'    => '80'
  ));

Could you let me know if this is achievable and if it is where I am going wrong> if it is not, is this a complex addition or anything you would consider?

I appreciate, I can do this via twig, but was hoping for a 'silver bullet' approach.

FYI - I am on this branch: "croxton/imgixer": "dev-feature/imagekit as 2.0.0-beta.1",

Thanks, Terry

croxton commented 1 year ago

Hi Terry, I'm afraid that won't work with Imgixer, sorry. It's a cool idea though, and I'll consider hoew to implement something similar as a feature request for a future version.

For now I think your best bet would be to set the value of auto at the template level in twig.

Fyi the latest version is tagged as 2.1.0-beta.1

terryupton commented 1 year ago

Thanks Mark. 👍🏻