compress-image.uvue 4.7 KB
Newer Older
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex:1">
  <!-- #endif -->
    <page-head :title="title"></page-head>
    <view class="uni-padding-wrap">
      <view class="image-container">
        <image class="image" :src="beforeCompressPath" mode="aspectFit"></image>
        <image class="image" :src="afterCompressPath" mode="aspectFit"></image>
      </view>
      <view class="uni-title">
        <text class="uni-subtitle-text">压缩前图片信息</text>
      </view>
      <text>{{beforeCompressImageInfo}}</text>
      <view class="uni-title">
        <text class="uni-subtitle-text">压缩后图片信息</text>
      </view>
      <text>{{afterCompressImageInfo}}</text>
      <view class="uni-btn-v">
        <button type="primary" @click="chooseImage">从相册中选取待压缩的图片</button>
      </view>
      <view class="uni-btn-v">
        <button type="primary" @click="compressImage">压缩图片</button>
      </view>
    </view>
    <input-data defaultValue="80" title="压缩质量,范围0~100,数值越小,质量越低,压缩率越高(仅对jpg有效)" type="number"
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
27 28 29 30 31 32
      @confirm="onQualityConfirm"></input-data>
    <input-data title="压缩后图片的宽度,单位px" type="number" @confirm="onCompressedWidthConfirm"></input-data>
    <input-data title="压缩后图片的高度,单位px" type="number" @confirm="onCompressedHeightConfirm"></input-data>
    <input-data defaultValue="auto" title="压缩后图片的宽度,支持px、%、auto" type="string" @confirm="onWidthConfirm"></input-data>
    <input-data defaultValue="auto" title="压缩后图片的高度,支持px、%、auto" type="string" @confirm="onHeightConfirm"></input-data>
    <input-data defaultValue="0" title="旋转度数,范围0~360" type="number" @confirm="onRotateConfirm"></input-data>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script>
  export default {
    data() {
      return {
        title: "compressImage",
        beforeCompressImageInfo: "",
        afterCompressImageInfo: "",
        beforeCompressPath: "",
        afterCompressPath: "",
        quality: 80,
        compressedWidth: null as number | null,
        compressedHeight: null as number | null,
        width: "auto",
        height: "auto",
        rotate: 0
      }
    },
    methods: {
      compressImage() {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
57 58 59 60 61 62 63 64 65 66
        if (this.beforeCompressPath == "") {
          uni.showToast({
            title: "请先选择图片",
            icon: "error"
          });
          return;
        }
        uni.showLoading({
          title: "图片压缩中"
        });
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
67 68 69 70 71 72 73 74
        uni.compressImage({
          src: this.beforeCompressPath,
          quality: this.quality,
          compressedWidth: this.compressedWidth,
          compressedHeight: this.compressedHeight,
          width: this.width,
          height: this.height,
          rotate: this.rotate,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
75 76 77
          success: (res) => {
            console.log("compressImage success", JSON.stringify(res));
            this.afterCompressPath = res.tempFilePath;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
78 79 80 81 82
            uni.showToast({
              title: "压缩成功",
              icon: null
            });
            uni.getImageInfo({
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
83 84 85
              src: res.tempFilePath,
              success: (_res) => {
                this.afterCompressImageInfo = `图片宽度: ${_res.width}, 图片高度: ${_res.height}`;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
86 87 88 89 90
              }
            });
          },
          fail: (err) => {
            uni.showModal({
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
91 92 93 94 95 96 97
              title: "压缩图片失败",
              content: JSON.stringify(err),
              showCancel: false
            });
          },
          complete: (_) => {
            uni.hideLoading();
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
98 99 100 101 102 103 104 105
          }
        })
      },
      chooseImage() {
        uni.chooseImage({
          count: 1,
          sizeType: ["original"],
          sourceType: ["album"],
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
106 107
          success: (res) => {
            this.beforeCompressPath = res.tempFilePaths[0];
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
108
            uni.getImageInfo({
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
109 110 111
              src: res.tempFilePaths[0],
              success: (_res) => {
                this.beforeCompressImageInfo = `图片宽度: ${_res.width}, 图片高度: ${_res.height}`;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
112 113 114 115 116
              }
            });
          }
        });
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
117
      onQualityConfirm(value : number) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
118 119
        this.quality = value;
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
120
      onCompressedWidthConfirm(value : number) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
121 122
        this.compressedWidth = value;
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
123
      onCompressedHeightConfirm(value : number) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
124 125
        this.compressedHeight = value;
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
126
      onWidthConfirm(value : string) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
127 128
        this.width = value;
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
129
      onHeightConfirm(value : string) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
130 131
        this.height = value;
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
132
      onRotateConfirm(value : number) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
        this.rotate = value;
      }
    }
  }
</script>

<style>
  .image {
    flex: 1;
  }

  .image-container {
    flex-direction: row;
  }
</style>