How to add a overlay parameter to preview image in widget

I’m trying to add an overlay parameter to the preview image in the tab effects panel. This is to show a bleed that gets applied to the image while printing. I have already implemented this feature for one of our products, but that has a different workflow.
You can try here: https://www.tinyme.com.au/photo-studio/index/studio/?id=2858
Overlay applied


Overlay stays after the effects, which is what I want in the new build as well.

The dotted lines are an overlay image: https://ucarecdn.com/0e4af643-1d09-4a2b-b4d3-9810931320b5/-/preview/-/rotate/270/-/scale_crop/420x420/center/-/overlay/56412582-5c20-4b6e-9e9f-888ed73d32cd/420x420/-/preview/1162x693/-/setfill/ffffff/-/format/jpeg/-/progressive/yes/

But now with the new implementation (New product: https://www.tinyme.com.au/personalised-photo-jigsaw-puzzle), I want to set this overlay after the photo being uploaded into the widget. So I tried

  singleWidget.onDialogOpen(function (dialog) {
    dialog.onTabVisibility(function (tab) {
        if (tab == 'preview') {
            Promise.all(dialog.fileColl.__items).then(fileInfo => {
                var imageSize = fileInfo[0].originalImageInfo.height +'x'+ fileInfo[0].originalImageInfo.width;
                var bleedOverlay = '-/overlay/' + bleedUuid + '/' + imageSize + '/';
                var imageWithBleed = fileInfo[0].cdnUrl + bleedOverlay;
                fileInfo[0].cdnUrl = imageWithBleed;
                fileInfo[0].originalUrl = imageWithBleed;

                dialog.dialogElement.find('.uploadcare-tab-effects--image').attr('src', imageWithBleed);

            });
        }
    });
});

But this only works for the first preview load, as soon as you so an edit/effect the overlay will go way.
In the previous build, I set the value of the widget to have the overlay, but in this case, I don’t see a logical way to set the value after the widget is loaded. Any idea how to overcome this issue?
Another challenge I’ll be facing with the overlay is switched the overlay image based on the crop aspect ratio. Because we’ll be offering 2-3 aspect ratios, so depending on the crop I may need to switch the overlay UUID.

Hi @dev1,

Thank you for the question! I couldn’t get the overlay visible even on the first preview in the second product, but I’ve written another code snippet to make the overlay appear on the preview image every time.

uploadcare.registerTab("preview", uploadcareTabEffects);
var overlay = {
  '1:1': '0bdb41a9-1b88-4c26-97b4-06b6fba5940a',
  '2:3': '06282412-32b1-4072-b640-13a0fbe5d434'
};

var currentCrop = '1:1'

// detects current crop preset
document.addEventListener('click', function (e) {
  if (e.target.classList.contains('uploadcare--crop-sizes__item') || e.target.classList.contains('uploadcare--crop-sizes__icon')) {
    currentCrop = document.querySelector('.uploadcare--crop-sizes__item_current').dataset.caption;
  }
});

// draws overlay
var widget = uploadcare.Widget("[role=uploadcare-uploader]");
widget.onDialogOpen(function (dialog) {
  dialog.dialogElement[0].addEventListener('load', function (e) {
    var tab = dialog.dialogElement[0].querySelector('.uploadcare-tab-effects--title').innerText;
    if (e.target.classList.contains('uploadcare--preview__image') && !e.target.style.length && tab !== 'Crop') {
      if (e.target.src.indexOf('overlay') < 0) {
        e.target.src += '-/overlay/' + overlay[currentCrop] + '/100px100p/center/';
      }
    }
  }, true);
});

There are a lot of checks and “if” statements in the code because the crop UI is a part of the preview tab, and when you click the crop icon, there’s no tab switch occurs, so using the dialog.onTabVisibility won’t work in this case. Also, there’s no way to get the current crop preset from the widget’s API, so in my code, I 'm using a click event listener to detect clicks on crop preset buttons and read the corresponding caption to get the preset selected.

Check out a live version here.

Hope that helps.

Awesome!! Thanks for the solution. I’m working on implementing this solution. Looks like with a rotation applied its not working properly. Wonder we can apply a rotation to the overlay as well.
One other thing is that with a scenario where there is one aspect ratio, the widget apply the aspect ratio as soon as file is uploaded. So there is no way to the user to rotate the original image, and then crop. Is there a way to overcome this issue?

Unfortunately, there’s no way to apply rotation to the overlay image. Even if you add the rotate operation after the overlay one, it will be applied to the base image only. I’ll add a feature request on your behalf, and hopefully, a way to rotate the overlay image will be implemented in the furure.

If your crop settings contain the “free” preset, no aspect ratios will be applied to the uploaded image by default. A user will need to click the crop icon and crop the image manually. Otherwise, the image is not cropped. For example,

data-crop="3:4, 16:9, free"

Hi Alex
Thanks, for raising a feature request for the rotate, that’ll be a very helpful feature to have. So as a solution I have created a function to rotate the overlay after calculating the aspect ratio manually.
Thanks for your help

Regards
Thanura

1 Like