spacecatninja / craft-imager-x

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

A Few Questions #250

Closed A1Allan closed 9 months ago

A1Allan commented 10 months ago
I'm submitting a...

Description

Hi, I'm new to using Imager-X and have a few questions that I didn't see in the FAQ or troubleshooting. 1. Is there a way to assign a path for specific transforms or sizes? For example, using a named transform to go to place images transformed with it into a folder like 'imager/400w/' ? I tried what I thought would work based on what I saw in the documentation, but it seemed like the configuration changes only work at the config level and not the override (or defaults) level for an individual transform. 2. How do I apply two named transforms to an image? Based on https://imager-x.spacecat.ninja/usage/ and https://imager-x.spacecat.ninja/usage/named-transforms.html I would expect something like this to work, but it doesn't seem to work for me. `{% set newBaseImage = craft.imagerx.transformImage(image, 'defaultTransform') %}` `{% set transformedImages = craft.imagerx.transformImage(newBaseImage, 'hexImage') %}` defaultTransform and hexImage both work by themselves. So ` {% set transformedImages = craft.imagerx.transformImage(image, 'hexImage') %}` and ` {% set transformedImages = craft.imagerx.transformImage(image, 'hexImage') %}` work without an error. I just can't seem to do a named transform on an image that was transformed? For context what I was using for testing: ``` 'defaultTransform' => [ 'displayName' => 'Default 1000w transform', 'transforms' => [ ['width' => 1000] ] ] ``` Also, replacing 'defaultTransform' with width: 1000 renders without an error. 3. Is there a good way to nest named transforms by width? I'm trying to be able to setup a group of named transforms that can be called by other transforms. So, something like this (but, actually functional)... `['width' => 400, 'transforms' => '400wheadhex']` ### Additional info - Imager version & edition: 4.2.3 PRO - Imager transformer: craft - Craft version: 4.5.11.1 PRO - PHP version: 8.0.30 - Image driver & version: Imagick 3.7.0 (ImageMagick 6.9.11-60)
aelvan commented 10 months ago

Hi,

I'll try to answer as best I can.

1) No, the path inside the imagerSystemPath is in a fixed format, except the first segment which may or may not be the handle for the assets volume if you toggle addVolumeToPath. The reason for this is that Imager needs control over the path to ensure that transforms are unique, and to facilitate cache breaking. The filename can be customized using filenamePattern

2) I'm not 100% sure what you're trying to achieve here, but the following:

{% set newBaseImage = craft.imagerx.transformImage(image, 'defaultTransform') %}
{% set transformedImages = craft.imagerx.transformImage(newBaseImage, 'hexImage') %}

Should work from a technical standpoint, except that if defaultTransform returns an array of transformed images, which your excerpt from the config indicates, it doesn't.. Because transformImage takes in one image source. So try changing the second line to {% set transformedImages = craft.imagerx.transformImage(newBaseImage[0], 'hexImage') %} and see what happens.

To explain this further, the second parameter of transformImage, and the transforms parameter of the named transform config, can either be an array of transforms, or a single one. And imager will return either an array of transformed images, or a single transformed image, depending on this.

Example:

'singleTransform' => [
    'transforms' => [
        'width' => 1000
    ]
],
'multipleTransforms' => [
    'transforms' => [
        ['width' => 1000],
        ['width' => 1200],
        ['width' => 1400],
    ]
]
{% set transformedSingleImage = craft.imagerx.transformImage(image, 'singleTransform') %}
{# transformedSingleImage is now a single transformed image model #}

{% set multipleTransformedImages = craft.imagerx.transformImage(image, 'multipleTransforms') %}
{# multipleTransformedImages is now an array of transformed image models #}

Not sure if that solves it for you or not. If you still can't get the end result that you want, please provide some more details about what you're trying to achieve.

3) The only way to nest or extend named transforms at the moment, is using the technique shown in the docs you linked to (in the "Nested named transforms" section). This lets you override defaults, for instance format or aspect ratio, but not the sizes themselves.

But as with the previous question, I'm a bit fuzzy on why you'd want to do this, so any added context would be needed to suggest workarounds or alternate solutions.

Hope this helps!

A1Allan commented 10 months ago

Hi @aelvan thank you for the fast response! I really appreciate it.

  1. Thank you! That was my understanding, but I wanted to make sure that I wasn't missing something in the documentation.
  2. Thank you for that. The [0] did the trick!
  3. I guess this might be an implicit feature request. Basically, I want the ability to create named transforms and then apply them to certain sizes within a named transform. Let me know if I should create a new thread about the request outlining it in more detail (or if it's a "No, definitely not gonna build that.").
aelvan commented 10 months ago

Great that you got a step further!

Regarding 3), I'm still not sure what you're trying to achieve. In the example you gave (['width' => 400, 'transforms' => '400wheadhex']), what's the resulting transform(s) of this? Two transforms, one with width 400px, and one that's exactly the same as 400wheadhex? Or one transform where width in 400wheadhex is overwritten by 'width' => 400? Or something else?

A1Allan commented 10 months ago

I think I can actually accomplish my goal with the automatic transforms and the sequential transforms (using [0]). What I had in mind was the ability to have set named transforms for specific widths. My client has a lot of similar images, but they don't all need the same transforms. One set of images might need transforms A, B, and C while another similar set of images would need A, C, and D. So, I could have aNamedTransform that would do A,B, and C and another that would kick off A,C, and D. But, instead of using named transforms, I'm using automatic transforms based on the fields where the images would be used. So far, I haven't seen any exceptions to this strategy.

I also found an unrelated error, but I don't think it's an actual bug. On local, I don't have a copy of every image (even though they have sizes and take up bytes). When I try to execute a transform on it I get an error...

image

It does have a size.

image

So, I didn't expect the error.

image

If I download the real asset from the live site and upload it then the transform executes without an issue.