Skip to content

Commit c23967a

Browse files
authored
Merge pull request Vanthink-UED#103 from Shinerising/master
Fix iOS image rotate issue & Add image rotate function
2 parents 0d67820 + d691103 commit c23967a

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

src/lib/canvas-helper.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,48 @@ export default {
5454
callback(data);
5555
}
5656
},
57+
58+
/**
59+
* init image for reset size and rotation
60+
**/
61+
init(src, callback) {
62+
console.log(src);
63+
let image = new Image();
64+
image.src = src;
65+
var self = this;
66+
image.onload = function() {
67+
var mimeType = self._getImageType(image.src);
68+
var cvs = self._getCanvas(image.naturalWidth, image.naturalHeight);
69+
var ctx = cvs.getContext("2d");
70+
ctx.drawImage(image, 0, 0);
71+
var newImageData = cvs.toDataURL(mimeType, 100);
72+
callback(newImageData);
73+
}
74+
},
75+
76+
/**
77+
* rotate image via canvas
78+
**/
79+
rotate(imageData, direction, callback) {
80+
let image = new Image();
81+
image.src = imageData.src;
82+
var self = this;
83+
image.onload = function() {
84+
var mimeType = self._getImageType(image.src);
85+
var cvs = self._getCanvas(image.naturalHeight, image.naturalWidth);
86+
var ctx = cvs.getContext("2d");
87+
if (direction == 1) {
88+
ctx.rotate(90 * Math.PI / 180);
89+
ctx.translate(0, -cvs.width);
90+
} else {
91+
ctx.rotate(-90 * Math.PI / 180);
92+
ctx.translate(-cvs.height, 0);
93+
}
94+
ctx.drawImage(image, 0, 0);
95+
var newImageData = cvs.toDataURL(mimeType, 100);
96+
callback(newImageData);
97+
}
98+
},
5799

58100
resize(image, options, callback) {
59101
const checkNumber = function(num) {

src/vue-core-image-upload.vue

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
<div class="g-core-image-corp-container" v-bind:id="'vciu-modal-' + formID" v-show="hasImage">
88
<crop ref="cropBox" :is-resize="resize && !crop" :ratio="cropRatio"></crop>
99
<div class="info-aside">
10+
<p class="btn-groups rotate-groups" v-if="crop">
11+
<button type="button" v-on:click="doRotate" class="btn btn-rotate">↺</button>
12+
<button type="button" v-on:click="doReverseRotate" class="btn btn-reverserotate">↻</button>
13+
</p>
1014
<p class="btn-groups" v-if="crop">
1115
<button type="button" v-on:click="doCrop" class="btn btn-upload">{{cropBtn.ok}}</button>
1216
<button type="button" v-on:click="cancel" class="btn btn-cancel">{{cropBtn.cancel}}</button>
@@ -133,7 +137,9 @@
133137
const cropBox = this.$refs.cropBox;
134138
pic.onload= function() {
135139
self.image.minProgress = self.minWidth / pic.naturalWidth;
136-
self.imgChangeRatio = cropBox.setImage(src, pic.naturalWidth, pic.naturalHeight);
140+
canvasHelper.init(src, (src) => {
141+
self.imgChangeRatio = cropBox.setImage(src, pic.naturalWidth, pic.naturalHeight);
142+
});
137143
}
138144
},
139145
@@ -142,6 +148,26 @@
142148
cropBox.resizeImage(progress);
143149
},
144150
151+
doRotate(e) {
152+
let self = this;
153+
const cropBox = this.$refs.cropBox;
154+
const targetImage = cropBox.getCropImage();
155+
this.data.comprose = 100 - this.compress;
156+
return canvasHelper.rotate(targetImage, 1, (src) => {
157+
self.__initImage(src)
158+
})
159+
},
160+
161+
doReverseRotate(e) {
162+
let self = this;
163+
const cropBox = this.$refs.cropBox;
164+
const targetImage = cropBox.getCropImage();
165+
this.data.comprose = 100 - this.compress;
166+
return canvasHelper.rotate(targetImage, -1, (src) => {
167+
self.__initImage(src)
168+
})
169+
},
170+
145171
doCrop(e) {
146172
this.__setData('crop');
147173
const cropBox = this.$refs.cropBox;
@@ -159,7 +185,7 @@
159185
},
160186
161187
doResize(e) {
162-
this.__setData('reszie');
188+
this.__setData('resize');
163189
const cropBox = this.$refs.cropBox;
164190
const upload = this.__setUpload(e.target);
165191
if (this.resize === 'local') {

0 commit comments

Comments
 (0)