Using The CropGuide JavaScript API

CropGuide fires a selection of events to make it easier to integrate CropGuide with your webpage.

Use the file input field below and see the CropGuide events and their contents logged to your browser developer console.

We can listen for CropGuide events on the document object, we don’t have to wait for CropGuide to load.

<input type="file" />

    document.addEventListener('$cropguide:process', (e) => {
        console.log('The source image', e.detail.src);
        console.log('The output image', e.detail.dest);

See below for an overview of all the events CropGuide fires.



$cropguide:init is fired when CropGuide initialises, if this event doesn’t fire then either CropGuide is not supported on the browser or the script failed to load.


$cropguide:load is fired when CropGuide is ready.


$cropguide:open fires when the editor modal opens.


$cropguide:close fires when the editor modal closes.


$cropguide:intercept fires when CropGuide intercepts a file selection. The detail property of the event is set to the original event object.

document.addEventListener('$cropguide:intercept', (e) => {
    console.log('The original file input event', e.detail);


$cropguide:process fires when CropGuide has generated a new image, the detail property of the event is set to { src, dest } an object containing both the original file and the resulting image.


$cropguide:invalid fires when CropGuide determines the selected image is too small to process. The detail property of the event is set to { src } an object containing the original image file.


$cropguide:error fires when an error has occurred. The detail property of the event is set to { src, error } an object containing the original image file and an error object.


$cropguide:ignore fires when the editor ignores a file, for example when it’s not an image. The detail property is set to { src }, src being the file object that is ignored.


In the example below we use CropGuide to offer the resulting image as a download to the user.

<input type="file" />

    document.addEventListener('$cropguide:process', (e) => {

    function downloadFile(file) {
        // Create a link and set the URL using `createObjectURL`
        const link = document.createElement('a'); = 'none';
        link.href = URL.createObjectURL(file); =;

        // It needs to be added to the DOM so it can be clicked

        // To make this work on Firefox we need to wait
        // a little while before removing it.
        setTimeout(() => {
        }, 0);