clipPhoto.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // pages/clip/clip.js
  2. const HOST = require('../../utils/const.js').apiUrl;
  3. import httpRequestApi from '../../utils/APIRequest';
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. imageUrl: '',
  10. cropperW: '',
  11. cropperH: '',
  12. img_ratio: '',
  13. IMG_W: '',
  14. IMG_H: '',
  15. clipImage: '',
  16. top: '',
  17. screenW: '',
  18. screenH: ''
  19. },
  20. //拖拽事件
  21. move: function ({ detail }) {
  22. this.setData({
  23. top: detail.y * 2
  24. })
  25. },
  26. //生成图片
  27. getImageInfo: function () {
  28. wx.showLoading({
  29. title: '图片生成中...',
  30. })
  31. const img_ratio = this.data.img_ratio;
  32. const canvasW = this.data.IMG_W;
  33. const canvasH = (this.data.screenH / this.data.cropperH) * this.data.IMG_H
  34. const canvasL = 0;
  35. const canvasT = (this.data.top / this.data.cropperH) * this.data.IMG_H
  36. // 将图片写入画布
  37. const ctx = wx.createCanvasContext('myCanvas');
  38. //绘制图像到画布
  39. ctx.beginPath(); //开始绘制
  40. //判断是竖着显示还是横着显示
  41. if(this.data.img_ratio >= 1) {
  42. //保存图片分别是图片地址,canvas绘制距离左边距离,距离上边距离,截取原图片宽度,原图片高度,图片在canvas上的左位置,上位置,生成图片的宽,生成图片的高;
  43. ctx.drawImage(this.data.imageUrl, canvasL, canvasT, canvasW, canvasH, 0, 0, this.data.screenW / 2, this.data.screenH / 2);
  44. } else {
  45. //保存图片分别是图片地址,canvas绘制距离左边距离,距离上边距离,截取原图片宽度,原图片高度,图片在canvas上的左位置,上位置,生成图片的宽,生成图片的高;
  46. ctx.drawImage(this.data.imageUrl, canvasL, canvasT, canvasW, canvasH, 0, 0, this.data.screenW / 2, this.data.screenH / 1.2);
  47. }
  48. ctx.draw(true, () => {
  49. // 获取画布要裁剪的位置和宽度
  50. wx.canvasToTempFilePath({
  51. x: 0,
  52. y: 0,
  53. width: this.data.screenW / 2,
  54. height: this.data.screenH / 1.2,
  55. destWidth: this.data.screenW / 2,
  56. destHeight: this.data.screenH / 1.2,
  57. quality: 0.5,
  58. canvasId: 'myCanvas',
  59. success: (res) => {
  60. wx.hideLoading()
  61. console.log(res);
  62. this.setData({
  63. clipImage: res.tempFilePath
  64. })
  65. //上传文件
  66. wx.uploadFile({
  67. url: HOST + 'wx/file/upload',
  68. filePath: res.tempFilePath,
  69. name: 'uploadfile_ant',
  70. header: {
  71. "Content-Type": "multipart/form-data"
  72. },
  73. success: (res) => {
  74. const data = JSON.parse(res.data);
  75. if(data.success) {
  76. //上传文件成功后放到相册里
  77. httpRequestApi.addPhotoList({
  78. path: data.data
  79. }).success((res) => {
  80. wx.navigateTo({
  81. url: '/pages/album/album'
  82. })
  83. }).fail(() => {
  84. wx.showModal({
  85. title: '错误提示',
  86. content: '图片上传到相册失败'
  87. })
  88. });
  89. }
  90. },
  91. fail: function (res) {
  92. wx.hideToast();
  93. wx.showModal({
  94. title: '错误提示',
  95. content: '上传图片失败'
  96. })
  97. }
  98. });
  99. //成功获得地址的地方
  100. // wx.previewImage({
  101. // current: '', // 当前显示图片的http链接
  102. // urls: [res.tempFilePath] // 需要预览的图片http链接列表
  103. // })
  104. }
  105. })
  106. })
  107. },
  108. /**
  109. * 生命周期函数--监听页面加载
  110. */
  111. onLoad: function (options) {
  112. console.log(options.imageUrl);
  113. //获取初始屏幕宽度
  114. const screenW = wx.getSystemInfoSync().windowWidth * 2;
  115. //h获取剪裁框的高度,按照16:9来计算高度
  116. const screenH = screenW / 16 * 9;
  117. this.setData({
  118. screenW
  119. })
  120. const imageUrl = options.imageUrl;
  121. this.setData({
  122. imageUrl
  123. })
  124. wx.getImageInfo({
  125. src: imageUrl,
  126. success: (res) => {
  127. console.log(res);
  128. //图片实际款高
  129. const width = res.width;
  130. const height = res.height;
  131. //图片宽高比例
  132. const img_ratio = width / height
  133. this.setData({
  134. img_ratio,
  135. IMG_W: width,
  136. IMG_H: height,
  137. })
  138. if (img_ratio >= 1) {
  139. //宽比较大,横着显示
  140. this.setData({
  141. cropperW: 750,
  142. cropperH: 750 / img_ratio,
  143. screenH: 750 / 16 * 9
  144. })
  145. } else {
  146. //竖着显示
  147. this.setData({
  148. cropperW: 750 * img_ratio,
  149. cropperH: 750,
  150. screenH: (750 * img_ratio) / 16 * 9
  151. })
  152. }
  153. }
  154. })
  155. },
  156. /**
  157. * 生命周期函数--监听页面初次渲染完成
  158. */
  159. onReady: function () {
  160. },
  161. /**
  162. * 生命周期函数--监听页面显示
  163. */
  164. onShow: function () {
  165. },
  166. /**
  167. * 生命周期函数--监听页面隐藏
  168. */
  169. onHide: function () {
  170. },
  171. /**
  172. * 生命周期函数--监听页面卸载
  173. */
  174. onUnload: function () {
  175. },
  176. /**
  177. * 页面相关事件处理函数--监听用户下拉动作
  178. */
  179. onPullDownRefresh: function () {
  180. },
  181. /**
  182. * 页面上拉触底事件的处理函数
  183. */
  184. onReachBottom: function () {
  185. },
  186. /**
  187. * 用户点击右上角分享
  188. */
  189. onShareAppMessage: function () {
  190. }
  191. })