Skip to content

Commit

Permalink
feat: compressingImg
Browse files Browse the repository at this point in the history
  • Loading branch information
Xutaotaotao committed May 7, 2024
1 parent 38fbdce commit a4f1d7b
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/.umi/core/polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
3 changes: 2 additions & 1 deletion src/.umi/core/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
Expand Down
1 change: 1 addition & 0 deletions src/assets/img/imageCompress.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/layouts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -18,6 +19,7 @@ const NavMap = [
{itemKey: "/home", text: "首页", icon: <IconIntro className={styles.iconIntro}/>, className: styles.navItem},
{itemKey: "/imageSlicing", text: "图片分割", icon: <IconImage className={styles.iconHeart}/>, className: styles.navItem1},
{itemKey: "/jsonFormatting", text: "JSON格式化", icon: <img src={JsonSvg} className={styles.iconHeart}/>, className: styles.navItem1},
{itemKey: "/imageCompress", text: "图片压缩", icon: <img src={ImageCompressSvg} className={styles.iconHeart}/>, className: styles.navItem1},
]

const XLayout = () => {
Expand Down
173 changes: 173 additions & 0 deletions src/pages/imageCompress.tsx
Original file line number Diff line number Diff line change
@@ -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<Array<any>>([]);
const [currentImg, setCurrentImg] = useState<string>("");
const [compressingImg, setCompressingImg] = useState<string>("");
const [file, setFile] = useState<any>();
const [compressingFile, setCompressingFile] = useState<any>();
const api = useRef<any>();

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 (
<Row gutter={16}>
<Col span={6}>
<Card
title="设置"
styles={{ body: { height: "calc(100vh - 150px", overflow: "auto" } }}
>
<Form
getFormApi={(formApi) => (api.current = formApi)}
labelPosition="left"
onValueChange={formOnValueChange}
>
<Form.Checkbox field="strict" label="严格模式"></Form.Checkbox>
<Form.Checkbox
field="checkOrientation"
label="检查方向"
></Form.Checkbox>
<Form.Checkbox
field="retainExif"
label="保留EXIF信息"
></Form.Checkbox>
<Form.InputNumber
field="maxWidth"
label="最大宽度"
></Form.InputNumber>
<Form.InputNumber
field="maxHeight"
label="最大高度"
></Form.InputNumber>
<Form.InputNumber
field="minWidth"
label="最小宽度"
></Form.InputNumber>
<Form.InputNumber
field="minHeight"
label="最小高度"
></Form.InputNumber>
<Form.InputNumber field="width" label="宽度"></Form.InputNumber>
<Form.InputNumber field="height" label="高度"></Form.InputNumber>
<Form.Select
field="resize"
label="缩放模式"
style={{ width: "100%" }}
>
<Form.Select.Option value="none">自动</Form.Select.Option>
<Form.Select.Option value="cover">覆盖</Form.Select.Option>
<Form.Select.Option value="contain">包含</Form.Select.Option>
</Form.Select>
<Form.InputNumber
min={0}
max={1}
field="quality"
label="压缩质量(仅支持jpg)"
></Form.InputNumber>
</Form>
</Card>
</Col>
<Col span={18}>
{currentImg ? (
<div style={{ height: "calc(100vh - 100px)", overflow: "auto" }}>
<Card
title={`原图 【大小】:${file.size / 1024}KB`}
styles={{ body: { textAlign: "center" } }}
extra={<Button theme='solid' type="primary" onClick={resetImg} >重新上传图片</Button>}
>
<Image
height={350}
src={currentImg}
alt="preview"
/>
</Card>
<Card
title={`压缩后图片 【大小】:${compressingFile?.size / 1024}KB`}
style={{ marginTop: "16px" }}
styles={{ body: { textAlign: "center" } }}
extra={<Button theme='solid' type="primary" onClick={downLoadCompressing} >下载</Button>}
>
<Image
height={350}
src={compressingImg}
alt="preview"
/>
</Card>
</div>
) : (
<Dragger
name="file"
multiple
onChange={filesChange}
fileList={fileList}
customRequest={() => {}}
showUploadList={false}
>
<p className="ant-upload-drag-icon">
<InboxOutlined />
</p>
<p className="ant-upload-text">点击或拖拽上传图片</p>
</Dragger>
)}
</Col>
</Row>
);
}
2 changes: 1 addition & 1 deletion src/pages/jsonFormatting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const JsonFormatting = () => {

return <Row gutter={[16, 16]}>
<Col span={10}>
<TextArea className="json-input" onChange={(e) => handleJsonData(e)}/>
<TextArea placeholder="请输入想要格式化的数据" className="json-input" onChange={(e) => handleJsonData(e)}/>
</Col>
<Col span={14}>
<div className="json-input">
Expand Down
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit a4f1d7b

Please sign in to comment.