As the title suggests, I would like my plugin to generate a new image transform automatically (with a few settings) when the plugin is installed.

The purpose is to avoid my plugin from crashing if a transform hasn't already been generated yet (or doesn't have the same handle names).




Have a look at the method actionSaveTransform in the AssetTransformsController.

The code you actually need for your onAfterInstall would be like this:

// Only create if it doesn't exist (by handle)
if (is_null(craft()->assetTransforms->getTransformByHandle($handle))) {
    $transform = new AssetTransformModel();
    $transform->id = $transformId;
    $transform->name = $name;
    $transform->handle = $handle;
    $transform->width = $width;
    $transform->height = $height;
    $transform->mode = $mode;
    $transform->position = $position;
    $transform->quality = $quality;
    $transform->format = $format;

    if(craft()->assetTransforms->saveTransform($transform)) {
        // Success
    else {
        // Error
| improve this answer | |

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.