当前位置:网站首页>Practice | solution for image upload, rotation and compression on mobile terminal
Practice | solution for image upload, rotation and compression on mobile terminal
2022-06-29 16:36:00 【User 1097444】
author | Lin Xin
original text |http://imweb.io/topic/59559c01ad7fa941029740aa
Preface
On the mobile phone through the web input Take photos and upload pictures , There are some mobile phones that will rotate the picture 90 The problem of degree , Include iPhone And some Samsung mobile phones . This problem only occurs when these mobile phones shoot vertically , The photos taken horizontally will display normally . therefore , You can rotate the photos by obtaining the camera angle of the mobile phone , To solve this problem .
Orientation
Not all images have this parameter , But the picture taken by the mobile phone has this parameter .
Rotation Angle | Parameter values |
|---|---|
0° | 1 |
Clockwise 90° | 6 |
Anti-clockwise 90° | 8 |
180° | 3 |
Parameter is 1 When it shows normal , So in these horizontal strokes, the display is normal , namely Orientation = 1 On my cell phone , The parameters of the vertical beat are 6.
Want to get Orientation Parameters , Can pass exif.js Library to operate .exif.js A lot of function , Very big , There is enough before compression 30k, This has a great impact on mobile page loading . And I just need to get Orientation Information only , So I have deleted exif.js Some code of the library , Reduce the code to a few KB.
exif.js obtain Orientation :
file It is input File form uploaded file . The uploaded file goes through fileReader.readAsDataURL(file) You can preview the picture , If you are not clear about this aspect, you can check :HTML5 Advanced Series : File upload download (https://github.com/lin-xin/blog/issues/19)
rotate
Rotation requires canvas Of rotate() Method .
rotate The parameter of the method is the rotation arc . You need to convert the angle to radians :degrees * Math.PI / 180
By default, the rotation center points are all in canvas The starting point of the , namely ( 0, 0 ). The principle of rotation is shown in the figure below :
After spinning , If from ( 0, 0 ) Point proceed drawImage(), Then the position drawn is the rotation in the left figure 90 Position after degrees , Not in the visible area . After spinning , The axis also rotates , Want to display in the visible area , Need to put ( 0, 0 ) Point to y The shaft moves in the opposite direction y A unit of , The starting point is ( 0, -y ).
Empathy , You can obtain rotation -90 The starting point after degrees is ( -x, 0 ), rotate 180 The starting point after degrees is ( -x, -y ).
Compress
The photos taken by the mobile phone are too big , And use base64 The encoded photo will be larger than the original photo , So it is very necessary to compress when uploading . Today's mobile phones are so high in pixels , The width and height of the photos are thousands of pixels , use canvas Rendering this picture will be relatively slow .
Therefore, the first step is to limit the width and height of the uploaded photos , Determine whether the width or height is out of which range , Then compress its width and height in equal proportion .
The second step is to pass canvas.toDataURL() Method to compress photo quality .
toDataURL() Method returns a data URI . Use two parameters , The first parameter is the image format , The default is image/png. The second parameter is the compression quality , In the specified picture format image/jpeg or image/webp Under the circumstances , It can be downloaded from 0 To 1 The quality of selected pictures in the range of .
summary
Above all , The code of the example includes a simplified exif.js The base address :file-demo(https://github.com/lin-xin/blog/tree/master/file-demo)
The main core codes are as follows :
Scan the QR code below the code ,
Follow more front-end dry goods articles at any time !
▼
WeChat :IMWebTech
边栏推荐
- C language -- printf print base prefix
- 华夏基金:基金行业数字化转型实践成果分享
- Tianlong Babu TLBB series - how to make pets learn twelve full skills
- 又拍云 Redis 的改进之路
- 实践 | 移动端图片上传旋转、压缩的解决方案
- 『计组』CPU 如何区分指令和数据
- A simple but scalable feature normalization method
- Comprehensive analysis of Seata distributed transaction at and XA
- 【无标题】
- 暑期数据可视化分析展示效果
猜你喜欢

Sophon kg upgrade 3.1: break down barriers between data and liberate enterprise productivity

After eight years of testing and opening experience and interview with 28K company, hematemesis sorted out high-frequency interview questions and answers

Science: the interrelated causes and consequences of sleep in the brain

Metadata management Apache Atlas Compilation integration deployment and testing

Picture and text show you how to thoroughly understand the atomicity of MySQL transaction undolog

Stable currency risk profile: are usdt and usdc safe?

The third sprint of Wei long La Tiao: the growth rate of performance declined, and Liu Weiping and Liu Fuping cashed out in advance

京东方:随着下半年旺季到来、促销拉动、新产品发布等影响,需求有望出现好转

Greedy Apple plans to raise the price of iphone14, which will provide opportunities for Chinese mobile phones

apache atlas断点查看
随机推荐
资讯 | 扎克伯格被评为全球IT业最不谨慎的CEO;中国移动研发系留式无人机应急通信高空基站
Perhaps in two years, ASML will be free to supply EUV lithography machines to China
Implement a ThreadLocal by yourself
What memory consuming data is stored in MySQL CDC jobmanager?
MySQL基础——事务
Stable currency risk profile: are usdt and usdc safe?
Profil de risque de monnaie stable: l'usdt et l'USDC sont - ils sûrs?
UWB精准定位方案,厘米级高精度技术应用,智能配对感应技术
MySQL基础——多表查询
C语言——printf打印进制前缀
Telnet+ftp to control and upgrade the equipment
华夏基金:基金行业数字化转型实践成果分享
美国芯片再遭重击,Intel或将被台积电击败而沦落至全球第三
实战 | 神奇的 conic-gradient 圆锥渐变
Sophon Base 3.1 推出MLOps功能,为企业AI能力运营插上翅膀
Sophon KG升级3.1:打破数据间壁垒,解放企业生产力
分片信息调哪个参数呢?用的是MySQLsource stream api,不是table api
暑期数据可视化分析展示效果
Mathematical knowledge: finding combinatorial number II - finding combinatorial number
Us chips are hit hard again, and Intel may be defeated by TSMC and reduced to the third place in the world