web剪裁图片

先看看效果(采用react实现,其他框架实现也是一样的)

如上,我们可以剪裁任意的图片得到一个剪裁后的base64编码的图片

实现(react)

1.安装

npm i react-cropper -S

 

2.引入

import Cropper from "react-cropper";//引入组件库
import "cropperjs/dist/cropper.css";//引入相应的样式

3.核心代码

逻辑层

 constructor() {
    super();
    this.state = {
      img: "",
    };
    this.cropper = "";
  }
  cropImage = () => {
    this.setState({
      img: this.cropper.cropper.getCroppedCanvas().toDataURL(),
    });
  };

界面层

  render() {
    const { img } = this.state;
    return (
      <div>
        <div style={{ width: "100%" }}>
          <Cropper
            src={logo}
            ref={(cropper) => {
              this.cropper = cropper;
            }}
            style={{ height: 400, width: "100%" }}
            aspectRatio={1 / 1}
            guides={true}
          />
        </div>
        <div>
          <Button
            type="primary"
            onClick={this.cropImage}
            style={{ marginTop: "10px" }}
          >
            确认裁剪
          </Button>
        </div>
        <div style={{ textAlign: "center" }}>
          {img ? (
            <div>
              <p>剪裁后的图片</p>
              <img
                style={{
                  width: "100px",
                  height: "100px",
                  marginTop: "32px",
                  border: "1px solid #a1a1a1",
                  padding: "4px",
                }}
                src={img}
                alt="图片"
              />
            </div>
          ) : null}
        </div>
    </div>
  )}

完整代码

import React from "react";
import logo from "./logo512.png";//注意引用路径
import { Button } from "antd-mobile";
import Cropper from "react-cropper";
import "cropperjs/dist/cropper.css";
export default class ReduxDemo extends React.Component {
  constructor() {
    super();
    this.state = {
      img: "",
    };
    this.cropper = "";
  }
  cropImage = () => {
    this.setState({
      img: this.cropper.cropper.getCroppedCanvas().toDataURL(),
    });
  };
  render() {
    const { img } = this.state;
    return (
      <div>
        <div style={{ width: "100%" }}>
          <Cropper
            src={logo}
            ref={(cropper) => {
              this.cropper = cropper;
            }}
            style={{ height: 400, width: "100%" }}
            aspectRatio={1 / 1}
            guides={true}
          />
        </div>
        <div>
          <Button
            type="primary"
            onClick={this.cropImage}
            style={{ marginTop: "10px" }}
          >
            确认裁剪
          </Button>
        </div>
        <div style={{ textAlign: "center" }}>
          {img ? (
            <div>
              <p>剪裁后的图片</p>
              <img
                style={{
                  width: "100px",
                  height: "100px",
                  marginTop: "32px",
                  border: "1px solid #a1a1a1",
                  padding: "4px",
                }}
                src={img}
                alt="图片"
              />
            </div>
          ) : null}
        </div>
    </div>
  )}
}

 

相关链接

npm:点击这里

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值