|
1 | 1 | import { useEffect, useMemo, useState } from 'react' |
2 | 2 | import { Button, InputNumber, message, Radio, Slider, Space, Tabs, Tooltip, Typography, Upload } from 'antd' |
3 | | -import { CaretLeftOutlined, CaretRightOutlined, DeleteOutlined, DownloadOutlined, DragOutlined, FileImageOutlined, LayoutOutlined, PictureOutlined, MergeCellsOutlined } from '@ant-design/icons' |
| 3 | +import { CaretLeftOutlined, CaretRightOutlined, DeleteOutlined, DownloadOutlined, DragOutlined, FileImageOutlined, LayoutOutlined, PictureOutlined, MergeCellsOutlined, PlusOutlined } from '@ant-design/icons' |
4 | 4 | import type { UploadFile } from 'antd' |
5 | 5 | import { parseGIF, decompressFrames } from 'gifuct-js' |
6 | 6 | // @ts-expect-error gifenc has no types |
@@ -62,6 +62,26 @@ function compositeFrame( |
62 | 62 | return buf |
63 | 63 | } |
64 | 64 |
|
| 65 | +function loadImageElement(src: string): Promise<HTMLImageElement> { |
| 66 | + return new Promise((resolve, reject) => { |
| 67 | + const img = new Image() |
| 68 | + img.onload = () => resolve(img) |
| 69 | + img.onerror = () => reject(new Error('load')) |
| 70 | + img.src = src |
| 71 | + }) |
| 72 | +} |
| 73 | + |
| 74 | +function createTransparentPngBlob(width: number, height: number): Promise<Blob> { |
| 75 | + const canvas = document.createElement('canvas') |
| 76 | + canvas.width = Math.max(1, Math.floor(width)) |
| 77 | + canvas.height = Math.max(1, Math.floor(height)) |
| 78 | + const ctx = canvas.getContext('2d') |
| 79 | + ctx?.clearRect(0, 0, canvas.width, canvas.height) |
| 80 | + return new Promise((resolve, reject) => { |
| 81 | + canvas.toBlob((blob) => (blob ? resolve(blob) : reject(new Error('canvas'))), 'image/png') |
| 82 | + }) |
| 83 | +} |
| 84 | + |
65 | 85 | export default function GifFrameConverter() { |
66 | 86 | const { t } = useLanguage() |
67 | 87 | const [activeTab, setActiveTab] = useState<'gif2frames' | 'frames2gif' | 'images2single' | 'simpleStitch'>('images2single') |
@@ -108,6 +128,7 @@ export default function GifFrameConverter() { |
108 | 128 | const [cropRight, setCropRight] = useState(0) |
109 | 129 | const [cropPreviewIndex, setCropPreviewIndex] = useState(0) |
110 | 130 | const [firstImageSize, setFirstImageSize] = useState<{ w: number; h: number } | null>(null) |
| 131 | + const [addingBlankFrame, setAddingBlankFrame] = useState(false) |
111 | 132 |
|
112 | 133 | const [stitchFiles, setStitchFiles] = useState<File[]>([]) |
113 | 134 | const [stitchInputUrls, setStitchInputUrls] = useState<string[]>([]) |
@@ -323,6 +344,32 @@ export default function GifFrameConverter() { |
323 | 344 | } |
324 | 345 | } |
325 | 346 |
|
| 347 | + const addBlankCombineFrame = async () => { |
| 348 | + if (combineFiles.length === 0) return |
| 349 | + setAddingBlankFrame(true) |
| 350 | + try { |
| 351 | + const refSize = |
| 352 | + firstImageSize ?? |
| 353 | + (await (async () => { |
| 354 | + const url = URL.createObjectURL(combineFiles[0]!) |
| 355 | + try { |
| 356 | + const img = await loadImageElement(url) |
| 357 | + return { w: img.naturalWidth, h: img.naturalHeight } |
| 358 | + } finally { |
| 359 | + URL.revokeObjectURL(url) |
| 360 | + } |
| 361 | + })()) |
| 362 | + const blob = await createTransparentPngBlob(refSize.w, refSize.h) |
| 363 | + const nextIndex = combineFiles.length + 1 |
| 364 | + const blankFile = new File([blob], `blank_${String(nextIndex).padStart(3, '0')}.png`, { type: 'image/png' }) |
| 365 | + setCombineFiles((prev) => [...prev, blankFile]) |
| 366 | + } catch (e) { |
| 367 | + message.error(t('imagesToSingleAddBlankFailed') + ': ' + String(e)) |
| 368 | + } finally { |
| 369 | + setAddingBlankFrame(false) |
| 370 | + } |
| 371 | + } |
| 372 | + |
326 | 373 | const downloadCombined = () => { |
327 | 374 | if (!combinedUrl) return |
328 | 375 | const a = document.createElement('a') |
@@ -926,6 +973,14 @@ export default function GifFrameConverter() { |
926 | 973 | <Space wrap align="center" style={{ marginBottom: 12 }}> |
927 | 974 | <Text type="secondary">{t('imagesToSingleCols')}:</Text> |
928 | 975 | <InputNumber min={1} max={64} value={combineCols} onChange={(v) => setCombineCols(v ?? 4)} style={{ width: 72 }} /> |
| 976 | + <Button |
| 977 | + icon={<PlusOutlined />} |
| 978 | + onClick={addBlankCombineFrame} |
| 979 | + disabled={combineFiles.length === 0} |
| 980 | + loading={addingBlankFrame} |
| 981 | + > |
| 982 | + {t('imagesToSingleAddBlank')} |
| 983 | + </Button> |
929 | 984 | </Space> |
930 | 985 | <Space wrap align="center" style={{ marginBottom: 12 }}> |
931 | 986 | <Text type="secondary">{t('imagesToSingleInputMode')}:</Text> |
|
0 commit comments