1. 引言

2. 环境准备

在开始之前,请确保你已经安装了 Node.js 和 npm。然后,使用 Vue CLI 创建一个新的 Vue 项目:

npm install -g @vue/cli
vue create my-image-frame-app
cd my-image-frame-app

3. 项目结构

创建项目后,你的项目结构大致如下:

my-image-frame-app/
├── src/
│   ├── assets/       # 存放图片资源
│   ├── components/   # 存放自定义组件
│   ├── views/        # 存放页面组件
│   ├── App.vue       # 根组件
│   └── main.js       # 入口文件
├── public/
│   └── index.html    # 静态 HTML 文件
└── package.json     # 项目配置文件

4. 使用 Element UI 实现基本功能

Element UI 是一个基于 Vue 2.0 的桌面端组件库,提供了丰富的 UI 组件。首先,我们需要安装 Element UI 并引入到项目中:

npm install element-ui --save

然后在 main.js 中引入:

import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App.vue';

Vue.use(ElementUI);

new Vue({
  el: '#app',
  render: h => h(App)
});

5. 创建相框组件

<template>
  <div class="image-frame" :style="{ width: size + 'px', height: size + 'px' }">
    <img :src="imageSrc" :style="{ width: size + 'px', height: size + 'px' }">
  </div>
</template>

<script>
export default {
  props: {
    imageSrc: {
      type: String,
      required: true
    },
    size: {
      type: Number,
      default: 200
    }
  }
};
</script>

<style scoped>
.image-frame {
  border: 2px solid #ccc;
  position: relative;
  overflow: hidden;
}
.image-frame::before {
  content: '';
  display: block;
  padding-top: 100%;
}
.image-frame img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
</style>

6. 使用 el-image-viewer 展示大图

<template>
  <el-image-viewer :url-list="imageList" :z-index="zIndex"></el-image-viewer>
</template>

<script>
export default {
  data() {
    return {
      imageList: [
        'https://example.com/image1.jpg',
        'https://example.com/image2.jpg',
        // 更多图片...
      ],
      zIndex: 2000
    };
  }
};
</script>

7. 实现图片编辑功能

<template>
  <fabric-editor :image-src="imageSrc" @save="handleSave"></fabric-editor>
</template>

<script>
import FabricEditor from 'vue-fabric-editor';

export default {
  components: {
    FabricEditor
  },
  data() {
    return {
      imageSrc: 'https://example.com/image.jpg'
    };
  },
  methods: {
    handleSave(canvasData) {
      // 处理保存图片逻辑...
    }
  }
};
</script>

8. 实现图片上传和预览

<template>
  <el-upload
    action="https://jsonplaceholder.typicode.com/posts/"
    list-type="picture-card"
    :on-preview="handlePreview"
    :on-remove="handleRemove"
    :before-upload="beforeUpload"
  >
    <i class="el-icon-plus"></i>
  </el-upload>
  <el-dialog :visible.sync="dialogVisible">
    <img width="100%" :src="dialogImageUrl" alt="preview">
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false
    };
  },
  methods: {
    handlePreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleRemove(file, fileList) {
      // 处理删除图片逻辑...
    },
    beforeUpload(file) {
      // 处理图片上传前的逻辑...
      return true;
    }
  }
};
</script>

9. 总结