Programmatically generating an image
We can use the processImage
function to generate an image without showing or loading the user interface.
Just like with the editor factory methods (openEditor
, appendEditor
, etc) we need to supply the processImage
function with an imageReader
to read image data and an imageWriter
to write the output image (if we want to generate an output file).
import {
processImage,
createDefaultImageReader,
createDefaultImageWriter,
} from './pintura.js';
// Create a square crop and scale down image without using the editor UI
processImage('./my-image.jpeg', {
imageReader: createDefaultImageReader(),
imageWriter: createDefaultImageWriter({
targetSize: {
width: 256,
height: 256,
},
}),
imageCropAspectRatio: 1,
}).then(({ dest }) => {
// Show resulting image preview
const preview = new Image();
preview.src = URL.createObjectURL(dest);
document.body.appendChild(preview);
});