I was recently working on a project about face collection, and when I was writing the front-end module, I encountered a problem. When customers upload photos or take photos and upload them directly, some pictures may be flipped 90 degrees. So, we need to give customers a button so that they can rotate the pictures themselves. The effect is roughly as follows Figure 1. Normal image upload Figure 2. The image is rotated left Figure 3. Image rotated right The above is a picture rotation function Now let’s start with the code part. Here I have adopted a method, and my current image format is base64. It doesn’t matter if you encounter other formats, because our final effect still needs to be converted to an image object to achieve it. /** * Image rotation* @param direction The direction of rotation*/ rotate (direction) { const img = new Image() // The idea here is to map the image to a drawing board and then redraw the image, so we need to create a canvas object to act as our drawing board const canvas = document.createElement('canvas') // base64 convert image object img.src = this.uploadImage // Remember to convert base64 into an image object before performing other operations. The author has made a mistake here. The onload method loads the image before performing other operations. If the image is a file path, the cross-domain call effect is more obvious.img.onload = () => { // The height and width of img cannot be obtained after the img element is hidden, otherwise an error will occur const height = img.height const width = img.width // The rotation angle is in radians as a parameter const ctx = canvas.getContext('2d') if (direction === 'right') { canvas.width = height canvas.height = width ctx.rotate(90 * Math.PI / 180) ctx.drawImage(img, 0, 0, width, -height) } else { canvas.width = height canvas.height = width ctx.rotate(-90 * Math.PI / 180) ctx.drawImage(img, 0, 0, -width, height) } // Convert the rotated image back to base64 this.uploadImage = this.getBase64Image(img, canvas) } }, /** * Convert the image file to base64 */ getBase64Image (img, canvas) { const ctx = canvas.getContext('2d') ctx.drawImage(img, 0, 0, img.width, img.height) const ext = img.src.substring(img.src.lastIndexOf('.') + 1).toLowerCase() return canvas.toDataURL('image/' + ext) } Note: If the image is obtained from a cross-domain, there may be problems converting canvas back to base64. In this case, you may need to enable a proxy to obtain the image. The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
>>: Start nginxssl configuration based on docker
Table of contents Preface Basic Concepts of Argum...
Table of contents Overview 1. Test for null value...
This article shares the specific code for JavaScr...
Test: Chrome v80.0.3987.122 is normal There are t...
This article shares the specific code for WeChat ...
We can create jsx/tsx files directly The project ...
Tetris is a very classic little game, and I also ...
Some web pages may not look large but may be very ...
Preface I don't know how long this friend has...
1. Find mysqldump.exe in the MySQL installation p...
Using provide+inject combination in Vue First you...
Table of contents 1. Create HTML structure 2. Cre...
I have encountered the problem that MySQL can con...
Add rules to the el-form form: Define rules in da...
This article example shares the specific code for...