Documentation

Configuration

Although CropGuide fields are meant to be configured using the customer dashboard we can also configure them with JavaScript.

Limiting CropGuide to a subset of fields

By default CropGuide will intercept all files selected on the page. We can configure it to target a subset of fields by setting the fields property on the $cropguide global variable.

<script>
    window.$cropguide = window.$cropguide || {
        fields: [
            /* Active fields here */
        ],
    };
</script>
<script defer src="https://cdn.crop.guide/loader/l.js?c=123ABC"></script>

One field

Let’s imagine we have the the following form and want to limit CropGuide to the profile picture field.

<form>
    <input type="file" />

    <fieldset>
        <input type="file" id="profile-picture" accept="image/*" />
    </fieldset>
</form>

This tells CropGuide to only intercept files that are added to the fields in the fields array, #profile-picture matches the id attribute of our file input.

<script>
    window.$cropguide = window.$cropguide || {
        fields: ['#profile-picture'],
    };
</script>
<script defer src="https://cdn.crop.guide/loader/l.js?c=123ABC"></script>

Multiple fields

The fields array can have multiple field selectors, this will select the profile-picture and document-photo field.

<script>
    window.$cropguide = window.$cropguide || {
        fields: ['#profile-picture', '#document-photo'],
    };
</script>
<script defer src="https://cdn.crop.guide/loader/l.js?c=123ABC"></script>

Instead of targetting specific fields we can also allow groups of fields.

<script>
    window.$cropguide = window.$cropguide || {
        fields: ['fieldset'],
    };
</script>
<script defer src="https://cdn.crop.guide/loader/l.js?c=123ABC"></script>

Here we tell CropGuide to only intercept files added to file inputs inside a <fieldset> element.

Overriding field properties

We can further adjust the fields property to pass custom editor properties to the fields.

Setting a different crop aspect ratio

Suppose one or more of our file input fields need a different crop aspect ratio.

We can achieve this by adjusting our settings object like shown below.

The #profile-picture field still receives the configuration we defined in the CropGuide admin, for the #document-photo field we overwrite the crop aspect ratio setting it to 3:4.

<script>
    window.$cropguide = window.$cropguide || {
        fields: [
            // Pass default configuration to profile picture
            '#profile-picture',

            // Override crop aspect for document photo
            {
                selector: '#document-photo',
                editor: {
                    imageCropAspectRatio: 3 / 4,
                },
            },
        ],
    };
</script>
<script defer src="https://cdn.crop.guide/loader/l.js?c=123ABC"></script>

The following properties are available for configuration.

<script>
    window.$cropguide = window.$cropguide || {
        fields: [
            // Pass default configuration to profile picture
            '#profile-picture',

            // Override crop aspect for document photo
            {
                selector: '#document-photo',

                // Field configuration
                field: {
                    // 'circle', defaults to undefined
                    guide: 'circle',

                    // 'circle', defaults to undefined
                    mask: 'circle',

                    // convert HEIC images to JPEGs, defaults to undefined
                    convertHeic: true,
                },

                // Editor configuration
                editor: {
                    // A Number, defaults to undefined
                    imageCropAspectRatio: 3 / 4,

                    // A Size, defaults to { width: 1, height: 1 }
                    imageCropMinSize: {
                        width: 100,
                        height: 100,
                    },

                    // An image writer configuration, defaults to undefined
                    imageWriter: {
                        // 'image/jpeg', 'image/png', or 'image/webp' or don't set
                        mimeType: 'image/jpeg',

                        // number between 0 and 1, or don't set
                        quality: 1

                        // a TargetSize or don't set
                        targetSize: {

                            // a number, or don't set
                            width: 100,

                            // a number, or don't set
                            height: 100,

                            // 'contain', 'cover', or 'force', or don't set
                            fit: 'contain',

                            // a boolean or don't set
                            upscale: false
                        }
                    }
                },
            },
        ],
    };
</script>
<script defer src="https://cdn.crop.guide/loader/l.js?c=123ABC"></script>