diff --git a/package.json b/package.json index 9a3cab0..3ac3652 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "@types/react": "^18.0.33", "@types/react-dom": "^18.0.11", "antd": "^5.16.4", + "compressorjs": "^1.2.1", "gh-pages": "^6.1.1", "react-draggable": "^4.4.6", "react-i18next": "^14.1.1", diff --git a/src/.umi/core/polyfill.ts b/src/.umi/core/polyfill.ts index 40d9143..841ca61 100644 --- a/src/.umi/core/polyfill.ts +++ b/src/.umi/core/polyfill.ts @@ -216,5 +216,5 @@ import "/Users/xutaotao/Documents/s/XTools/node_modules/core-js/modules/web.url. import "/Users/xutaotao/Documents/s/XTools/node_modules/core-js/modules/web.url-search-params.delete.js"; import "/Users/xutaotao/Documents/s/XTools/node_modules/core-js/modules/web.url-search-params.has.js"; import "/Users/xutaotao/Documents/s/XTools/node_modules/core-js/modules/web.url-search-params.size.js"; -import '/Users/xutaotao/Documents/s/XTools/node_modules/regenerator-runtime/runtime.js'; +import '/Users/xutaotao/Documents/s/XTools/node_modules/@umijs/preset-umi/node_modules/regenerator-runtime/runtime.js'; export {}; diff --git a/src/.umi/core/route.tsx b/src/.umi/core/route.tsx index 1d38d75..4c23f8c 100644 --- a/src/.umi/core/route.tsx +++ b/src/.umi/core/route.tsx @@ -19,11 +19,12 @@ if (process.env.NODE_ENV === 'development') { import React from 'react'; export async function getRoutes() { - const routes = {"jsonFormatting":{"path":"jsonFormatting","id":"jsonFormatting","parentId":"@@/global-layout"},"imageSlicing":{"path":"imageSlicing","id":"imageSlicing","parentId":"@@/global-layout"},"index":{"path":"/","id":"index","parentId":"@@/global-layout"},"docs":{"path":"docs","id":"docs","parentId":"@@/global-layout"},"home":{"path":"home","id":"home","parentId":"@@/global-layout"},"@@/global-layout":{"id":"@@/global-layout","path":"/","isLayout":true}} as const; + const routes = {"jsonFormatting":{"path":"jsonFormatting","id":"jsonFormatting","parentId":"@@/global-layout"},"imageCompress":{"path":"imageCompress","id":"imageCompress","parentId":"@@/global-layout"},"imageSlicing":{"path":"imageSlicing","id":"imageSlicing","parentId":"@@/global-layout"},"index":{"path":"/","id":"index","parentId":"@@/global-layout"},"docs":{"path":"docs","id":"docs","parentId":"@@/global-layout"},"home":{"path":"home","id":"home","parentId":"@@/global-layout"},"@@/global-layout":{"id":"@@/global-layout","path":"/","isLayout":true}} as const; return { routes, routeComponents: { 'jsonFormatting': React.lazy(() => import(/* webpackChunkName: "src__pages__jsonFormatting" */'../../../src/pages/jsonFormatting.tsx')), +'imageCompress': React.lazy(() => import(/* webpackChunkName: "src__pages__imageCompress" */'../../../src/pages/imageCompress.tsx')), 'imageSlicing': React.lazy(() => import(/* webpackChunkName: "src__pages__imageSlicing" */'../../../src/pages/imageSlicing.tsx')), 'index': React.lazy(() => import(/* webpackChunkName: "src__pages__index" */'../../../src/pages/index.tsx')), 'docs': React.lazy(() => import(/* webpackChunkName: "src__pages__docs" */'../../../src/pages/docs.tsx')), diff --git a/src/assets/img/imageCompress.svg b/src/assets/img/imageCompress.svg new file mode 100644 index 0000000..4a9b1c6 --- /dev/null +++ b/src/assets/img/imageCompress.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/layouts/index.tsx b/src/layouts/index.tsx index f1c4442..a00309e 100644 --- a/src/layouts/index.tsx +++ b/src/layouts/index.tsx @@ -9,6 +9,7 @@ import { Outlet, useLocation, history } from "umi"; import { OnSelectedData } from "@douyinfe/semi-ui/lib/es/navigation"; import Logo from "@/assets/img/XTools.svg"; import JsonSvg from "@/assets/img/json.svg"; +import ImageCompressSvg from "@/assets/img/imageCompress.svg"; const { Content } = Layout; @@ -18,6 +19,7 @@ const NavMap = [ {itemKey: "/home", text: "首页", icon: , className: styles.navItem}, {itemKey: "/imageSlicing", text: "图片分割", icon: , className: styles.navItem1}, {itemKey: "/jsonFormatting", text: "JSON格式化", icon: , className: styles.navItem1}, + {itemKey: "/imageCompress", text: "图片压缩", icon: , className: styles.navItem1}, ] const XLayout = () => { diff --git a/src/pages/imageCompress.tsx b/src/pages/imageCompress.tsx new file mode 100644 index 0000000..498dcbe --- /dev/null +++ b/src/pages/imageCompress.tsx @@ -0,0 +1,173 @@ +import { InboxOutlined } from "@ant-design/icons"; +import { Form,Button,Image } from "@douyinfe/semi-ui"; +import { Card, Col, Row, Upload } from "antd"; +import Compressor from "compressorjs"; +import { useRef, useState } from "react"; + +const { Dragger } = Upload; + +export default function ImageCompress() { + const [fileList, setFileList] = useState>([]); + const [currentImg, setCurrentImg] = useState(""); + const [compressingImg, setCompressingImg] = useState(""); + const [file, setFile] = useState(); + const [compressingFile, setCompressingFile] = useState(); + const api = useRef(); + + const filesChange = (info: any) => { + setFile(info.file); + let reader = new FileReader(); + reader.readAsDataURL(info.file.originFileObj); + reader.onload = function (e: any) { + setCurrentImg(e.target.result); + }; + const options = api.current.getValues(); + new Compressor(info.file.originFileObj, { + ...options, + success: (result) => { + console.log(result); + setCompressingFile(result); + let reader = new FileReader(); + reader.readAsDataURL(result); + reader.onload = function (e: any) { + setCompressingImg(e.target.result); + }; + }, + }); + }; + + const formOnValueChange = (value: any) => { + console.log(value); + new Compressor(file.originFileObj, { + ...value, + success: (result) => { + console.log(result); + setCompressingFile(result); + let reader = new FileReader(); + reader.readAsDataURL(result); + reader.onload = function (e: any) { + setCompressingImg(e.target.result); + }; + }, + }); + }; + + const downLoadCompressing = () => { + if (compressingFile) { + const a = document.createElement("a"); + a.href = URL.createObjectURL(compressingFile); // 创建对象超链接 + a.download = `${compressingFile.name}.jpg`; // 下载后文件名 + a.click(); // 点击下载 + } + } + + const resetImg = () => { + setFileList([]); + setCurrentImg(""); + setCompressingImg(""); + setFile(undefined); + } + + return ( + + + + (api.current = formApi)} + labelPosition="left" + onValueChange={formOnValueChange} + > + + + + + + + + + + + 自动 + 覆盖 + 包含 + + + + + + + {currentImg ? ( + + 重新上传图片} + > + + + 下载} + > + + + + ) : ( + {}} + showUploadList={false} + > + + + + 点击或拖拽上传图片 + + )} + + + ); +} diff --git a/src/pages/jsonFormatting.tsx b/src/pages/jsonFormatting.tsx index 824634f..149204c 100644 --- a/src/pages/jsonFormatting.tsx +++ b/src/pages/jsonFormatting.tsx @@ -19,7 +19,7 @@ const JsonFormatting = () => { return - handleJsonData(e)}/> + handleJsonData(e)}/> diff --git a/yarn.lock b/yarn.lock index 9aee983..5e0b806 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2260,6 +2260,11 @@ binary-extensions@^2.0.0: resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.3.0.tgz#f6e14a97858d327252200242d4ccfe522c445522" integrity sha512-Ceh+7ox5qe7LJuLHoY0feh3pHuUDHAcRUeyL2VYghZwfpkNIy/+8Ocg0a3UuSoYzavmylwuLWQOf3hl0jjMMIw== +blueimp-canvas-to-blob@^3.29.0: + version "3.29.0" + resolved "https://registry.npmmirror.com/blueimp-canvas-to-blob/-/blueimp-canvas-to-blob-3.29.0.tgz#d965f06cb1a67fdae207a2be56683f55ef531466" + integrity sha512-0pcSSGxC0QxT+yVkivxIqW0Y4VlO2XSDPofBAqoJ1qJxgH9eiUDLv50Rixij2cDuEfx4M6DpD9UGZpRhT5Q8qg== + bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.11.9: version "4.12.0" resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.12.0.tgz#775b3f278efbb9718eec7361f483fb36fbbfea88" @@ -2624,6 +2629,14 @@ commondir@^1.0.1: resolved "https://registry.npmmirror.com/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b" integrity sha512-W9pAhw0ja1Edb5GVdIF1mjZw/ASI0AlShXM83UUGe2DVr5TdAPEA1OA8m/g8zWp9x6On7gqufY+FatDbC3MDQg== +compressorjs@^1.2.1: + version "1.2.1" + resolved "https://registry.npmmirror.com/compressorjs/-/compressorjs-1.2.1.tgz#4dee18ef5032f8166bd0a3258f045eda2cd07671" + integrity sha512-+geIjeRnPhQ+LLvvA7wxBQE5ddeLU7pJ3FsKFWirDw6veY3s9iLxAQEw7lXGHnhCJvBujEQWuNnGzZcvCvdkLQ== + dependencies: + blueimp-canvas-to-blob "^3.29.0" + is-blob "^2.1.0" + compute-scroll-into-view@^1.0.20: version "1.0.20" resolved "https://registry.npmmirror.com/compute-scroll-into-view/-/compute-scroll-into-view-1.0.20.tgz#1768b5522d1172754f5d0c9b02de3af6be506a43" @@ -4269,6 +4282,11 @@ is-binary-path@~2.1.0: dependencies: binary-extensions "^2.0.0" +is-blob@^2.1.0: + version "2.1.0" + resolved "https://registry.npmmirror.com/is-blob/-/is-blob-2.1.0.tgz#e36cd82c90653f1e1b930f11baf9c64216a05385" + integrity sha512-SZ/fTft5eUhQM6oF/ZaASFDEdbFVe89Imltn9uZr03wdKMcWNVYSMjQPFtg05QuNkt5l5c135ElvXEQG0rk4tw== + is-boolean-object@^1.1.0, is-boolean-object@^1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/is-boolean-object/-/is-boolean-object-1.1.2.tgz#5c6dc200246dd9321ae4b885a114bb1f75f63719"
+ +
点击或拖拽上传图片