Custom code to resize image before upload

Here is my client side solution using the Custom UI Component Builder for anyone who is interested. The build was done using the Backendless blog post as a reference. I used this link to download and install compressorjs as a third party library (following the blog post instructions) for my Custom Component.

Here is my index.js code for the Custom UI Component. For my purposes, I wanted to hide the upload button for a consistent appearance, show an image preview, and compress the file size before upload and rename the file which is included here and will be expanded on below. I’m a self-taught tinkerer so apologies on the front end if the code is a bit messy or redundant! :grin:

import { useState } from 'react'

import Compressor from './lib/compressor';

export default function MyCustomComponent({ component, eventHandlers }) {
  
  const [selectedFile, setSelectedFile] = useState();

  const changeHandler = (event) => {
    
	setSelectedFile(event.target.files[0]);
		
		const file = event.target.files[0];
		
      
      new Compressor(file,{quality:0.6, success(result) {
        
      const lastIndex = file.name.lastIndexOf('.');

      const after = file.name.slice(lastIndex);
      
      const fileName = component.filenamePrefix.concat(after);
      
      const myRenamedFile = new File([result], fileName);

      eventHandlers.imageSelected({ imageFile: myRenamedFile })
      
    }, error(err) {
      //alert(`${err.message}`);
      console.log(err.message);
    },
      
    });
		
	};
	
  return ( 
    
			<input type="file" className="hideUploadButton" name="file" onChange={changeHandler} accept="image/*" />
	
  )
	
}```

Here is the style index.less code used to hide the upload button.

.hideUploadButton {
  display: inherit;
  opacity: 0;
  //background-color: #E79137;
  width: 100%;
    height: 100%;

Under Properties of the Component Builder I use the following setting to allow input of a new filename using Codeless logic.

The image is renamed and then handed back to the imageSelected event in the Component Builder for use to update an overlaid image on the user interface with Codeless logic.

Hopefully this info will be helpful for someone!

I was trying to do the exact same thing for avatars. I ended up using https://crop.guide/. It is an added cost but brings a lot of additional features.

I hope this helps,
Tim

Super useful, guess we will try to do the same and also build this custom component.

I wonder, have you considered publishing this in the Backendless UI component marketplace? Seems like it could help a lot of people, useful for all apps where user adds a thumbnail profile pic!

Hi Tim, curious about your crop guide experience… was easy to add to your Backendless app? Might you spare a minute and describe the steps? Happy with the plugin, and can you recommend?

I am still really liking crop.guide. It’s a 1-minute install. On page load add a custom code block -

The JS grabs the action of the upload button and does its thing.