Make aspect ratio selectable
Demo of Illustrated

Note: as this is a demo, you can't really upload files. However, you can browse for images on your system and test the Uploader widget.


By setting aspectRatio in the Illustrated behavior to an attribute name (as opposed to a float), the aspect ratio is stored in the database.

The option aspectOptions in the Uploader widget contains the list of possible aspect ratios as key-value pairs. The keys hold the aspect ratios, multiplied by 1000. The values hold the descriptions. (The motivation behind the multiplication by 1000 is that HTML doesn't accept floating point numbers as option keys.)

Data in the database

Click on the thumbnail to see the img file(s) on the server.

nameimgaspect
Fire fight, wide5mxt3u.jpg1.778
Fire fight, portraitmwsffe.jpg0.75

Configuration of the model

<?php

class <model> extends \yii\db\ActiveRecord
{
    ...
    public function behaviors()    {
        return [
            [
                'class' => 'sjaakp\illustrated\Illustrated',
                'attributes' => [
                    'img' => [
                        'aspectRatio' => 'aspect'
                    ]
                ]
            ],
            ...        // other behaviors
        ];
    }
    ...
}

Settings of the Uploader widget

<?php
use sjaakp\illustrated\Uploader;
?>

<?php $form = ActiveForm::begin([
    'options' => ['enctype' => 'multipart/form-data']    // important, needed for file upload
]); ?>
    ...        // other form fields
<?= $form->field($model, 'img')->widget(Uploader::className(), [
        'aspectOptions' => [
            429 => 'tower (9x21)',
            563 => 'high (9x16)',
            750 => 'portrait (3x4)',
            1000 => 'square (1x1)',
            1333 => 'landscape (4x3)',
            1778 => 'wide (16x9)',
            2333 => 'cinema (21x9)',
        ],
        'defaultAspect' => 1.0,
        'deleteOptions' => false
    ]) ?>
    ...
<?php ActiveForm::end(); ?>