How to get file path to transformed image? #11408
-
Use case: Create and copy special transforms of selected images and package them into a native app, using a local transform filesystem. This gets more complicated in Craft 4, eg. in Craft 3 there was Thrown together some wild things in a behavior, learned a lot I never wanted to know, probably missing better approaches, finally feeling uncomfortable with doing all this in detail by myself.
So it would be great to have a public method like /Mel PS. And it doesn't feel clean to use http calls. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We should make this easier down the road for sure. For now, you can expose namespace my\namespace;
use craft\elements\Asset;
use craft\imagetransforms\ImageTransformer;
use craft\models\ImageTransformIndex;
class MyImageTransformer extends ImageTransformer
{
public function getTransformSubpath(Asset $asset, ImageTransformIndex $transformIndex): string
{
return parent::getTransformSubpath($asset, $transformIndex);
}
} Then you can have Craft use your own namespace my\namespace;
use Craft;
use craft\imagetransforms\ImageTransformer;
use craft\models\ImageTransformIndex;
// ...
public function init()
{
// ...
Craft::$container->set(
ImageTransformer::class,
fn($container, $params, $config) => new MyImageTransformer($config),
);
} |
Beta Was this translation helpful? Give feedback.
We should make this easier down the road for sure. For now, you can expose
getTransformSubpath()
as a public method by providing your ownImageTransformer
class that extendscraft\imagetransforms\ImageTransformer
, like so:Then you can have Craft use your own
ImageTransformer
class instead ofcarft\imagetransforms\ImageTransformer