Browse Source

Support uploading .png wallpapers

master
Eduard Kuzmenko 2 years ago
parent
commit
c53466f1fd
  1. 12
      src/components/sidebarLeft/tabs/background.ts
  2. 9
      src/helpers/canvas/scaleMediaElement.ts

12
src/components/sidebarLeft/tabs/background.ts

@ -29,6 +29,9 @@ import choosePhotoSize from "../../../lib/appManagers/utils/photos/choosePhotoSi
import { STATE_INIT, Theme } from "../../../config/state"; import { STATE_INIT, Theme } from "../../../config/state";
import themeController from "../../../helpers/themeController"; import themeController from "../../../helpers/themeController";
import requestFile from "../../../helpers/files/requestFile"; import requestFile from "../../../helpers/files/requestFile";
import { renderImageFromUrlPromise } from "../../../helpers/dom/renderImageFromUrl";
import scaleMediaElement from "../../../helpers/canvas/scaleMediaElement";
import { MediaSize } from "../../../helpers/mediaSize";
export default class AppBackgroundTab extends SliderSuperTab { export default class AppBackgroundTab extends SliderSuperTab {
private grid: HTMLElement; private grid: HTMLElement;
@ -108,6 +111,15 @@ export default class AppBackgroundTab extends SliderSuperTab {
private onUploadClick = () => { private onUploadClick = () => {
requestFile('image/x-png,image/png,image/jpeg').then(async(file) => { requestFile('image/x-png,image/png,image/jpeg').then(async(file) => {
if(file.name.endsWith('.png')) {
const img = document.createElement('img');
const url = URL.createObjectURL(file);
await renderImageFromUrlPromise(img, url, false);
const mimeType = 'image/jpeg';
const {blob} = await scaleMediaElement({media: img, size: new MediaSize(img.naturalWidth, img.naturalHeight), mimeType});
file = new File([blob], file.name.replace(/\.png$/, '.jpg'), {type: mimeType});
}
const wallPaper = await this.managers.appDocsManager.prepareWallPaperUpload(file); const wallPaper = await this.managers.appDocsManager.prepareWallPaperUpload(file);
const uploadPromise = this.managers.appDocsManager.uploadWallPaper(wallPaper.id); const uploadPromise = this.managers.appDocsManager.uploadWallPaper(wallPaper.id);
const uploadDeferred: CancellablePromise<any> = appDownloadManager.getNewDeferredForUpload(file.name, uploadPromise); const uploadDeferred: CancellablePromise<any> = appDownloadManager.getNewDeferredForUpload(file.name, uploadPromise);

9
src/helpers/canvas/scaleMediaElement.ts

@ -2,14 +2,15 @@ import type { MediaSize } from "../mediaSize";
export default function scaleMediaElement(options: { export default function scaleMediaElement(options: {
media: CanvasImageSource, media: CanvasImageSource,
mediaSize: MediaSize, mediaSize?: MediaSize,
boxSize: MediaSize, boxSize?: MediaSize,
quality?: number, quality?: number,
mimeType?: 'image/jpeg' | 'image/png' mimeType?: 'image/jpeg' | 'image/png',
size?: MediaSize
}): Promise<{blob: Blob, size: MediaSize}> { }): Promise<{blob: Blob, size: MediaSize}> {
return new Promise((resolve) => { return new Promise((resolve) => {
const canvas = document.createElement('canvas'); const canvas = document.createElement('canvas');
const size = options.mediaSize.aspectFitted(options.boxSize); const size = options.size ?? options.mediaSize.aspectFitted(options.boxSize);
canvas.width = size.width * window.devicePixelRatio; canvas.width = size.width * window.devicePixelRatio;
canvas.height = size.height * window.devicePixelRatio; canvas.height = size.height * window.devicePixelRatio;
const ctx = canvas.getContext('2d'); const ctx = canvas.getContext('2d');

Loading…
Cancel
Save