From 1cac3842ac849eb83faf26a81ba5f4cb91b14db4 Mon Sep 17 00:00:00 2001 From: Adeodonne Date: Wed, 13 Sep 2023 16:27:33 +0300 Subject: [PATCH] add grey filter for source images --- .../FileUploader/FileUploader.component.tsx | 47 +++++++++++++++++-- .../ForFansPage/ForFansMainPage.component.tsx | 1 + .../CategoryAdminModal.component.tsx | 2 + .../PreviewFileModal.component.tsx | 2 +- .../SourceItem/SourceItem.component.tsx | 3 +- 5 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/app/common/components/FileUploader/FileUploader.component.tsx b/src/app/common/components/FileUploader/FileUploader.component.tsx index 91825e192..4b49cbc4f 100644 --- a/src/app/common/components/FileUploader/FileUploader.component.tsx +++ b/src/app/common/components/FileUploader/FileUploader.component.tsx @@ -5,7 +5,7 @@ import Upload, { RcFile, UploadChangeParam, UploadFile, UploadProps } from 'antd import AudiosApi from '@/app/api/media/audios.api'; import ImagesApi from '@/app/api/media/images.api'; import Audio, { AudioCreate } from '@/models/media/audio.model'; -import Image, { ImageCreate } from '@/models/media/image.model'; +import ImageCustom, { ImageCreate } from '@/models/media/image.model'; type UploaderWithoutChildren = Omit; @@ -13,19 +13,57 @@ interface Props extends UploaderWithoutChildren { children: JSX.Element[] | JSX.Element; edgeSwipe?: boolean; uploadTo:'image' | 'audio'; - onSuccessUpload?:(value:Image | Audio)=>void; + greyFilterForImage: boolean; + onSuccessUpload?:(value:ImageCustom | Audio)=>void; } -const FileUploader:React.FC = ({ onSuccessUpload, uploadTo, children, ...uploadProps }) => { +const FileUploader:React.FC = ({ + onSuccessUpload, uploadTo, greyFilterForImage = false, children, ...uploadProps +}) => { const imageDataAsURL = useRef(null); const onUploadChange = (uploadParams: UploadChangeParam>) => { const reader = new FileReader(); reader.onloadend = (obj) => { imageDataAsURL.current = obj.target?.result; + + if (greyFilterForImage) { + const img = new Image(); + img.src = imageDataAsURL.current; + if (img.height > 0 && img.width > 0) { + const canvas = document.createElement('canvas'); + const ctx = canvas.getContext('2d'); + if (ctx !== null) { + canvas.width = img.width; + canvas.height = img.height; + + ctx.drawImage(img, 0, 0); + + const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); + const { data } = imageData; + + for (let i = 0; i < data.length; i += 4) { + const red = data[i]; + const green = data[i + 1]; + const blue = data[i + 2]; + + const grayscale = (red + green + blue) / 3; + + data[i] = grayscale; + data[i + 1] = grayscale; + data[i + 2] = grayscale; + } + + ctx.putImageData(imageData, 0, 0); + + imageDataAsURL.current = canvas.toDataURL('image/png'); + } + } + } }; if (uploadParams.fileList.length === 0) { imageDataAsURL.current = undefined; } else { const file = uploadParams.file.originFileObj as RcFile; + if (file) { reader.readAsDataURL(file); } @@ -35,7 +73,7 @@ const FileUploader:React.FC = ({ onSuccessUpload, uploadTo, children, ... } }; const onFileUpload = (uploadType:'image' | 'audio', uplFile:UploadFile) - :Promise< Image | Audio> => { + :Promise< ImageCustom | Audio> => { if (uploadType === 'audio') { const audio :AudioCreate = { baseFormat: imageDataAsURL.current @@ -53,6 +91,7 @@ const FileUploader:React.FC = ({ onSuccessUpload, uploadTo, children, ... .lastIndexOf('.') + 1, uplFile.name.length), mimeType: uplFile.type!, title: uplFile.name }; + return ImagesApi.create(image); }; const customRequest = async (options:any) => { diff --git a/src/features/AdminPage/ForFansPage/ForFansMainPage.component.tsx b/src/features/AdminPage/ForFansPage/ForFansMainPage.component.tsx index 5fada6b50..441d98b2e 100644 --- a/src/features/AdminPage/ForFansPage/ForFansMainPage.component.tsx +++ b/src/features/AdminPage/ForFansPage/ForFansMainPage.component.tsx @@ -66,6 +66,7 @@ const ForFansMainPage: React.FC = observer(() => { key={`${record.id}${record.image?.id}}`} className="partners-table-logo" src={base64ToUrl(image?.base64, image?.mimeType ?? '')} + style={{ filter: 'grayscale(100%)' }} /> ), }, diff --git a/src/features/AdminPage/ForFansPage/ForFansPage/CategoryAdminModal.component.tsx b/src/features/AdminPage/ForFansPage/ForFansPage/CategoryAdminModal.component.tsx index efbd634af..e337d6074 100644 --- a/src/features/AdminPage/ForFansPage/ForFansPage/CategoryAdminModal.component.tsx +++ b/src/features/AdminPage/ForFansPage/ForFansPage/CategoryAdminModal.component.tsx @@ -160,8 +160,10 @@ const SourceModal: React.FC = ({ label="Картинка: " rules={[{ required: true, message: 'Додайте зображення' }]} getValueFromEvent={getValueFromEvent} + style={{ filter: 'grayscale(100%)' }} > { setFileList(param.fileList); }} diff --git a/src/features/AdminPage/NewStreetcode/MainBlock/PreviewFileModal/PreviewFileModal.component.tsx b/src/features/AdminPage/NewStreetcode/MainBlock/PreviewFileModal/PreviewFileModal.component.tsx index 5b92870df..de9efd2a9 100644 --- a/src/features/AdminPage/NewStreetcode/MainBlock/PreviewFileModal/PreviewFileModal.component.tsx +++ b/src/features/AdminPage/NewStreetcode/MainBlock/PreviewFileModal/PreviewFileModal.component.tsx @@ -57,7 +57,7 @@ const PreviewFileModal = ({ opened, setOpened, file }: Props) => { return (
- {previewImage && uploaded} + {previewImage && uploaded}
); diff --git a/src/features/StreetcodePage/SourcesBlock/SourceItem/SourceItem.component.tsx b/src/features/StreetcodePage/SourcesBlock/SourceItem/SourceItem.component.tsx index c58427920..0481dd8c6 100644 --- a/src/features/StreetcodePage/SourcesBlock/SourceItem/SourceItem.component.tsx +++ b/src/features/StreetcodePage/SourcesBlock/SourceItem/SourceItem.component.tsx @@ -17,7 +17,8 @@ const SourceItem = ({ srcCategory }: Props) => {
setModal('sources', srcCategory.id, true)} - style={{ backgroundImage: `url(${base64ToUrl(srcCategory.image?.base64, srcCategory.image?.mimeType)})` }} + style={{ backgroundImage: `url(${base64ToUrl(srcCategory.image?.base64, srcCategory.image?.mimeType)})`, + filter: 'grayscale(100%)' }} >

{srcCategory.title}