@ -1766,57 +1766,91 @@ export default {
applyFilterToMedia ( ) {
/ / t h i s i s w h e r e t h e m a g i c h a p p e n s
var ua = navigator . userAgent . toLowerCase ( ) ;
if ( ua . indexOf ( 'firefox' ) == - 1 && ua . indexOf ( 'chrome' ) == - 1 ) {
this . isPosting = false ;
swal ( 'Oops!' , 'Your browser does not support the filter feature.' , 'error' ) ;
this . page = 3 ;
return ;
}
let count = this . media . filter ( m => m . filter _class ) . length ;
if ( count ) {
this . page = 'filteringMedia' ;
this . filteringRemainingCount = count ;
this . $nextTick ( ( ) => {
this . isFilteringMedia = true ;
this . media . forEach ( ( media , idx ) => this . applyFilterToMediaSave ( media , idx ) ) ;
Promise . all ( this . media . map ( media => {
return this . applyFilterToMediaSave ( media ) ;
} ) ) . catch ( err => {
console . error ( err ) ;
swal ( 'Oops!' , 'An error occurred while applying filters to your media. Please refresh the page and try again. If the problem persist, please try a different web browser.' , 'error' ) ;
} ) ;
} )
} else {
this . page = 3 ;
}
} ,
a pplyFilterToMediaSave( media , idx ) {
a sync a pplyFilterToMediaSave( media ) {
if ( ! media . filter _class ) {
return ;
}
let self = this ;
let data = null ;
const canvas = document . createElement ( 'canvas' ) ;
const ctx = canvas . getContext ( '2d' ) ;
let image = document . createElement ( 'img' ) ;
/ / L o a d i m a g e
const image = document . createElement ( 'img' ) ;
image . src = media . url ;
image . addEventListener ( 'load' , e => {
await new Promise ( ( resolve , reject ) => {
image . addEventListener ( 'load' , ( ) => resolve ( ) ) ;
image . addEventListener ( 'error' , ( ) => {
reject ( new Error ( 'Failed to load image' ) ) ;
} ) ;
} ) ;
/ / C r e a t e c a n v a s
let canvas ;
let usingOffscreenCanvas = false ;
if ( 'OffscreenCanvas' in window ) {
canvas = new OffscreenCanvas ( image . width , image . height ) ;
usingOffscreenCanvas = true ;
} else {
canvas = document . createElement ( 'canvas' ) ;
canvas . width = image . width ;
canvas . height = image . height ;
ctx . filter = App . util . filterCss [ media . filter _class ] ;
ctx . drawImage ( image , 0 , 0 , image . width , image . height ) ;
ctx . save ( ) ;
canvas . toBlob ( function ( blob ) {
data = new FormData ( ) ;
data . append ( 'file' , blob ) ;
data . append ( 'id' , media . id ) ;
axios . post ( '/api/compose/v0/media/update' , data )
. then ( res => {
self . media [ idx ] . is _filtered = true ;
self . updateFilteringMedia ( ) ;
} ) . catch ( err => {
} ) ;
} , media . mime , 0.9 ) ;
} ) ;
ctx . clearRect ( 0 , 0 , image . width , image . height ) ;
}
/ / D r a w i m a g e w i t h f i l t e r t o c a n v a s
const ctx = canvas . getContext ( '2d' ) ;
if ( ! ctx ) {
throw new Error ( 'Failed to get canvas context' ) ;
}
if ( ! ( 'filter' in ctx ) ) {
throw new Error ( 'Canvas filter not supported' ) ;
}
ctx . filter = App . util . filterCss [ media . filter _class ] ;
ctx . drawImage ( image , 0 , 0 , image . width , image . height ) ;
ctx . save ( ) ;
/ / C o n v e r t c a n v a s t o b l o b
let blob ;
if ( usingOffscreenCanvas ) {
blob = await canvas . convertToBlob ( {
type : media . mime ,
quality : 1 ,
} ) ;
} else {
blob = await new Promise ( resolve => {
canvas . toBlob ( blob => {
if ( blob ) {
resolve ( blob ) ;
} else {
reject (
new Error ( 'Failed to convert canvas to blob' ) ,
) ;
}
} , media . mime , 1 ) ;
} ) ;
}
/ / U p l o a d b l o b / U p d a t e m e d i a
const data = new FormData ( ) ;
data . append ( 'file' , blob ) ;
data . append ( 'id' , media . id ) ;
await axios . post ( '/api/compose/v0/media/update' , data ) ;
media . is _filtered = true ;
this . updateFilteringMedia ( ) ;
} ,
updateFilteringMedia ( ) {