图片处理

2019-06-06  本文已影响0人  松松土_0b13

为了适应各种分辨率的显示和速度快,需要将图片进行压缩或者裁剪

前端图片处理

页面示例
import Cropper from 'cropperjs'
import 'cropperjs/dist/cropper.min.css'
initCropper () {
   this.showUpload=true;
     this.$nextTick( () => {
       this.cropper = new Cropper(document.getElementById(this.imgId), {
         viewMode: 1,
         aspectRatio: this.width/this.height,
         minCropBoxWidth: this.width,
         minCropBoxHeight: this.height,
         preview: `#${this.previewId}`,
         // checkCrossOrigin: false,
         // checkOrientation: false,
       })
     })
}

1、引入cropperjs和样式
2、指定dom元素和配置选项
3、调用this.cropper.replace(src)方法,指定src的时候,要保持同域(浏览器限制的)
4、压缩要指定格式“image/jpeg”

this.cropper.getCroppedCanvas({
     width: this.width, height: this.height
 }).toBlob(blob => {
  const isLt300KB = blob.size / 1024 < 300;
  if (!isLt300KB) {
     this.$message.error('图片大小不能超过 300KB!');
     return;
  }
  const formData = new FormData()
  formData.append('file', blob, 'aaa.png')
  uploadImg(formData).then((res) => {
     this.$emit('input', res.data.data);
     this.showUpload = false;
     this.clear()
  })
},'image/jpeg')

阿里云oss图片处理

原始图片上传到OSS后,您可以通过简单的RESTful接口,在任何时间、任何地点、任何互联网设备上对图片进行处理

OSS图片处理提供以下功能:

css图片自适应

background-size: cover;  
background-position:center;
上一篇 下一篇

猜你喜欢

热点阅读