Offering the output image for download
Using the web URL.createObjectURL
API we can turn a file object into a URL and offer it as a download to the client.
To prevent memory leaks we have to revoke the URL with the URL.revokeObjectURL
function when we no longer need the image.
Learn more about downloading files in the browser
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="./pintura.css" />
</head>
<style>
.pintura-editor {
height: 600px;
}
</style>
<div id="editor"></div>
<script type="module">
import { appendDefaultEditor } from './pintura.js';
const downloadFile = (file) => {
// Create a hidden link and set the URL using createObjectURL
const link = document.createElement('a');
link.style.display = 'none';
link.href = URL.createObjectURL(file);
link.download = file.name;
// We need to add the link to the DOM for "click()" to work
document.body.appendChild(link);
link.click();
// To make this work on Firefox we need to wait a short moment before clean up
setTimeout(() => {
URL.revokeObjectURL(link.href);
link.parentNode.removeChild(link);
}, 0);
};
const editor = appendDefaultEditor('#editor', { src: 'image.jpeg' });
editor.on('process', (imageState) => {
downloadFile(imageState.dest);
});
</script>
import '@pqina/pintura/pintura.css';
import './App.css';
import { useRef } from 'react';
import { PinturaEditor } from '@pqina/react-pintura';
import { getEditorDefaults } from '@pqina/pintura';
const editorDefaults = getEditorDefaults();
const downloadFile = (file) => {
// Create a hidden link and set the URL using createObjectURL
const link = document.createElement('a');
link.style.display = 'none';
link.href = URL.createObjectURL(file);
link.download = file.name;
// We need to add the link to the DOM for "click()" to work
document.body.appendChild(link);
link.click();
// To make this work on Firefox we need to wait a short moment before clean up
setTimeout(() => {
URL.revokeObjectURL(link.href);
link.parentNode.removeChild(link);
}, 0);
};
function App() {
const editorRef = useRef(null);
const handleEditorProcess = (imageState) => {
downloadFile(imageState.dest);
};
return (
<div className="App">
<PinturaEditor
ref={editorRef}
{...editorDefaults}
src={'image.jpeg'}
onProcess={handleEditorProcess}
/>
</div>
);
}
export default App;
.pintura-editor {
height: 600px;
}
<template>
<div>
<PinturaEditor
ref="editor"
v-bind="editorDefaults"
src="image.jpeg"
v-on:pintura:process="handleEditorProcess($event)"
/>
</div>
</template>
<script>
import { PinturaEditor } from '@pqina/vue-pintura';
import { getEditorDefaults } from '@pqina/pintura';
const downloadFile = function (file) {
// Create a hidden link and set the URL using createObjectURL
const link = document.createElement('a');
link.style.display = 'none';
link.href = URL.createObjectURL(file);
link.download = file.name;
// We need to add the link to the DOM for "click()" to work
document.body.appendChild(link);
link.click();
// To make this work on Firefox we need to wait a short moment before clean up
setTimeout(() => {
URL.revokeObjectURL(link.href);
link.parentNode.removeChild(link);
}, 0);
};
export default {
name: 'App',
components: {
PinturaEditor,
},
data() {
return {
editorDefaults: getEditorDefaults(),
};
},
methods: {
handleEditorProcess: function (imageState) {
this.downloadFile(imageState.dest);
},
},
};
</script>
<style>
@import '@pqina/pintura/pintura.css';
.pintura-editor {
height: 600px;
}
</style>
<script>
import { PinturaEditor } from '@pqina/svelte-pintura';
import { getEditorDefaults } from '@pqina/pintura';
import '@pqina/pintura/pintura.css';
const downloadFile = (file) => {
// Create a hidden link and set the URL using createObjectURL
const link = document.createElement('a');
link.style.display = 'none';
link.href = URL.createObjectURL(file);
link.download = file.name;
// We need to add the link to the DOM for "click()" to work
document.body.appendChild(link);
link.click();
// To make this work on Firefox we need to wait a short moment before clean up
setTimeout(() => {
URL.revokeObjectURL(link.href);
link.parentNode.removeChild(link);
}, 0);
};
let editorDefaults = getEditorDefaults();
let editor;
const handleEditorProcess = (event) => {
const imageState = event.detail;
downloadFile(imageState.dest);
};
</script>
<div>
<PinturaEditor
bind:this={editor}
{...editorDefaults}
src={'image.jpeg'}
on:process={handleEditorProcess}
/>
</div>
<style>
div :global(.pintura-editor) {
height: 600px;
}
</style>
import { Component, ViewChild } from '@angular/core';
import { getEditorDefaults } from '@pqina/pintura';
const downloadFile = (file: File): void => {
// Create a hidden link and set the URL using createObjectURL
const link = document.createElement('a');
link.style.display = 'none';
link.href = URL.createObjectURL(file);
link.download = file.name;
// We need to add the link to the DOM for "click()" to work
document.body.appendChild(link);
link.click();
// To make this work on Firefox we need to wait a short moment before clean up
setTimeout(() => {
URL.revokeObjectURL(link.href);
link.parentNode.removeChild(link);
}, 0);
};
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
@ViewChild('editor') editor?: any;
editorDefaults: any = getEditorDefaults();
handleEditorProcess(imageState: any): void {
this.downloadFile(imageState.dest);
}
}
<pintura-editor
#editor
[options]="editorDefaults"
src="image.jpeg"
(process)="handleEditorProcess($event)"
></pintura-editor>
::ng-deep .pintura-editor {
height: 600px;
}
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { AngularPinturaModule } from '@pqina/angular-pintura';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, AngularPinturaModule],
exports: [AppComponent],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="./pintura/pintura.css" />
</head>
<script src="./jquery.js"></script>
<script src="./jquery-pintura/useEditorWithJQuery-iife.js"></script>
<script src="./pintura/pintura-iife.js"></script>
<style>
.pintura-editor {
height: 600px;
}
</style>
<div id="editor"></div>
<script>
useEditorWithJQuery(jQuery, pintura);
$(function () {
const downloadFile = (file) => {
// Create a hidden link and set the URL using createObjectURL
const link = document.createElement('a');
link.style.display = 'none';
link.href = URL.createObjectURL(file);
link.download = file.name;
// We need to add the link to the DOM for "click()" to work
document.body.appendChild(link);
link.click();
// To make this work on Firefox we need to wait a short moment before clean up
setTimeout(() => {
URL.revokeObjectURL(link.href);
link.parentNode.removeChild(link);
}, 0);
};
var editor = $('#editor').pinturaDefault({ src: 'image.jpeg' });
editor.on('pintura:process', function (event) {
const imageState = event.detail;
downloadFile(imageState.dest);
});
});
</script>