|
@@ -0,0 +1,110 @@
|
|
|
|
+<template>
|
|
|
|
+ <div class="magick-test">
|
|
|
|
+ <h2>magick-test</h2>
|
|
|
|
+ <el-form label-width="60px">
|
|
|
|
+ <el-form-item label="图片" prop="sourceFilePath">
|
|
|
|
+ <el-input
|
|
|
|
+ style="width: 80%"
|
|
|
|
+ v-model="modalForm.sourceFilePath"
|
|
|
|
+ placeholder="请选择资源文件"
|
|
|
|
+ readonly
|
|
|
|
+ ></el-input>
|
|
|
|
+ <el-button
|
|
|
|
+ type="primary"
|
|
|
|
+ style="margin-left: 10px"
|
|
|
|
+ @click="toSelectSourceFilePath"
|
|
|
|
+ >
|
|
|
|
+ 选择
|
|
|
|
+ </el-button>
|
|
|
|
+ <div v-if="sourceFilePath">
|
|
|
|
+ <img :src="sourceFilePath" style="width: 260px" />
|
|
|
|
+ </div>
|
|
|
|
+ </el-form-item>
|
|
|
|
+ <el-form-item label="结果">
|
|
|
|
+ <div v-if="resultFilePath">
|
|
|
|
+ <img :src="resultFilePath" style="width: 260px" />
|
|
|
|
+ </div>
|
|
|
|
+ </el-form-item>
|
|
|
|
+ <el-form-item>
|
|
|
|
+ <el-button type="primary" @click="toCrop">裁剪图片</el-button>
|
|
|
|
+ </el-form-item>
|
|
|
|
+ </el-form>
|
|
|
|
+ </div>
|
|
|
|
+</template>
|
|
|
|
+
|
|
|
|
+<script>
|
|
|
|
+import { getTmpDir } from "@/plugins/env";
|
|
|
|
+import gm from "@/plugins/gm";
|
|
|
|
+import { randomCode, formatDate } from "@/plugins/utils";
|
|
|
|
+
|
|
|
|
+const { dialog } = window.nodeRequire("electron").remote;
|
|
|
|
+const path = window.nodeRequire("path");
|
|
|
|
+
|
|
|
|
+export default {
|
|
|
|
+ name: "magick-test",
|
|
|
|
+ data() {
|
|
|
|
+ return {
|
|
|
|
+ modalForm: {
|
|
|
|
+ sourceFilePath:
|
|
|
|
+ "/Users/chulin/develop/workspace/tcs/paper-library-client/stores/tmp/0-封面@1x.png",
|
|
|
|
+ resultFilePath: "",
|
|
|
|
+ },
|
|
|
|
+ };
|
|
|
|
+ },
|
|
|
|
+ computed: {
|
|
|
|
+ sourceFilePath() {
|
|
|
|
+ return this.modalForm.sourceFilePath
|
|
|
|
+ ? `file:///${this.modalForm.sourceFilePath}`
|
|
|
|
+ : "";
|
|
|
|
+ },
|
|
|
|
+ resultFilePath() {
|
|
|
|
+ return this.modalForm.resultFilePath
|
|
|
|
+ ? `file:///${this.modalForm.resultFilePath}`
|
|
|
|
+ : "";
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ methods: {
|
|
|
|
+ toSelectSourceFilePath() {
|
|
|
|
+ dialog
|
|
|
|
+ .showOpenDialog({
|
|
|
|
+ title: "请选择图片",
|
|
|
|
+ properties: ["openFile"],
|
|
|
|
+ filters: [
|
|
|
|
+ {
|
|
|
|
+ name: "img",
|
|
|
|
+ extensions: ["jpg", "png"],
|
|
|
|
+ },
|
|
|
|
+ ],
|
|
|
|
+ })
|
|
|
|
+ .then(({ canceled, filePaths }) => {
|
|
|
|
+ if (canceled) return;
|
|
|
|
+ this.modalForm.sourceFilePath = filePaths[0];
|
|
|
|
+ });
|
|
|
|
+ },
|
|
|
|
+ toCrop() {
|
|
|
|
+ const tmpFile = path.join(
|
|
|
|
+ getTmpDir(),
|
|
|
|
+ `${formatDate("YYYYMMDDHHmmss")}_${randomCode(8)}.png`
|
|
|
|
+ );
|
|
|
|
+ const imgObj = gm(this.modalForm.sourceFilePath);
|
|
|
|
+ // 裁剪条形码区域
|
|
|
|
+ imgObj.crop(300, 300, 0, 0).write(tmpFile, (err) => {
|
|
|
|
+ if (err) {
|
|
|
|
+ console.dir(err);
|
|
|
|
+ this.$message.error("裁剪失败");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.modalForm.resultFilePath = tmpFile;
|
|
|
|
+ });
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+};
|
|
|
|
+</script>
|
|
|
|
+
|
|
|
|
+<style scoped>
|
|
|
|
+.magick-test h2 {
|
|
|
|
+ font-size: 18px;
|
|
|
|
+ margin-bottom: 20px;
|
|
|
|
+}
|
|
|
|
+</style>
|