2023-07-16 03:31:33 +02:00
|
|
|
<script lang="ts">
|
|
|
|
import { AssetResponseDto, api } from '@api';
|
2023-07-25 07:17:59 +02:00
|
|
|
import { createEventDispatcher, onMount } from 'svelte';
|
2023-07-16 03:31:33 +02:00
|
|
|
import { notificationController, NotificationType } from './notification/notification';
|
|
|
|
import { handleError } from '$lib/utils/handle-error';
|
|
|
|
import domtoimage from 'dom-to-image';
|
|
|
|
import PhotoViewer from '../asset-viewer/photo-viewer.svelte';
|
|
|
|
import BaseModal from './base-modal.svelte';
|
|
|
|
import Button from '../elements/buttons/button.svelte';
|
|
|
|
|
|
|
|
export let asset: AssetResponseDto;
|
|
|
|
|
|
|
|
const dispatch = createEventDispatcher();
|
|
|
|
let imgElement: HTMLDivElement;
|
|
|
|
|
2023-07-25 07:17:59 +02:00
|
|
|
onMount(() => {
|
|
|
|
imgElement.style.width = '100%';
|
|
|
|
});
|
|
|
|
|
2023-07-16 03:31:33 +02:00
|
|
|
const hasTransparentPixels = async (blob: Blob) => {
|
|
|
|
const img = new Image();
|
|
|
|
img.src = URL.createObjectURL(blob);
|
|
|
|
await img.decode();
|
|
|
|
const canvas = document.createElement('canvas');
|
|
|
|
canvas.width = img.width;
|
|
|
|
canvas.height = img.height;
|
|
|
|
const ctx = canvas.getContext('2d');
|
|
|
|
if (!ctx) {
|
|
|
|
throw new Error('Could not get canvas context.');
|
|
|
|
}
|
|
|
|
ctx.drawImage(img, 0, 0);
|
|
|
|
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
|
|
|
const data = imageData?.data;
|
|
|
|
if (!data) {
|
|
|
|
throw new Error('Could not get image data.');
|
|
|
|
}
|
|
|
|
for (let i = 0; i < data.length; i += 4) {
|
|
|
|
if (data[i + 3] < 255) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleSetProfilePicture = async () => {
|
|
|
|
try {
|
|
|
|
const blob = await domtoimage.toBlob(imgElement);
|
|
|
|
if (await hasTransparentPixels(blob)) {
|
|
|
|
notificationController.show({
|
|
|
|
type: NotificationType.Error,
|
|
|
|
message: 'Profile pictures cannot have transparent pixels. Please zoom in and/or move the image.',
|
|
|
|
timeout: 3000,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const file = new File([blob], 'profile-picture.png', { type: 'image/png' });
|
|
|
|
await api.userApi.createProfileImage({ file });
|
|
|
|
notificationController.show({
|
|
|
|
type: NotificationType.Info,
|
|
|
|
message: 'Profile picture set.',
|
|
|
|
timeout: 3000,
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
handleError(err, 'Error setting profile picture.');
|
|
|
|
}
|
|
|
|
dispatch('close');
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<BaseModal on:close>
|
|
|
|
<svelte:fragment slot="title">
|
2023-07-18 20:19:39 +02:00
|
|
|
<span class="flex place-items-center gap-2">
|
2023-07-16 03:31:33 +02:00
|
|
|
<p class="font-medium">Set profile picture</p>
|
|
|
|
</span>
|
|
|
|
</svelte:fragment>
|
2023-07-18 20:19:39 +02:00
|
|
|
<div class="flex place-items-center items-center justify-center">
|
2023-07-16 03:31:33 +02:00
|
|
|
<div
|
2023-07-18 20:19:39 +02:00
|
|
|
class="relative flex aspect-square w-1/2 overflow-hidden rounded-full border-4 border-immich-primary bg-immich-dark-primary dark:border-immich-dark-primary dark:bg-immich-primary"
|
2023-07-16 03:31:33 +02:00
|
|
|
>
|
|
|
|
<PhotoViewer bind:element={imgElement} {asset} />
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-07-18 20:19:39 +02:00
|
|
|
<span class="flex justify-end p-4">
|
2023-07-16 03:31:33 +02:00
|
|
|
<Button on:click={handleSetProfilePicture}>
|
|
|
|
<p>Set as profile picture</p>
|
|
|
|
</Button>
|
|
|
|
</span>
|
2023-07-18 20:19:39 +02:00
|
|
|
<div class="mb-2 flex max-h-[400px] flex-col" />
|
2023-07-16 03:31:33 +02:00
|
|
|
</BaseModal>
|