How to use canvas of HTML5 to realize picture compression
This article focuses on "how to use HTML5 canvas to achieve picture compression", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to use HTML5 canvas to achieve picture compression"!
The code is as follows:
Lianxi
Name:
Upload:
Button
Varfile=document.querySelector ('# fileimage')
Varusername=document.querySelector ('# username')
Varcanvas=document.createElement ('canvas')
Varspan=document.createElement ('span')
VarformData=newFormData ()
File.addEventListener ('change',function () {
/ / newformData () of the implementation principle, and then transfer the formData to ajax.
SPAN [XSS _ clean] = 'preview loading.'
Document.body.appendChild (span)
Console.log (file.files [0])
Username.value?formData.append ('username',username.value): null
Resize (file.files [0])
})
/ / Image compression before upload
Functionresize (file) {
Varreader=newFileReader ()
Reader.readAsDataURL (file)
Varimg=newImage ()
Reader.onload=function (e) {
/ / this.result is the base64 address information of the image.
Img.src=this.result
}
Varcontext=canvas.getContext ('2d')
Vargif=document.createElement ('img')
/ / after the base64 address picture is loaded
Img.onload=function () {
/ / original size of the picture
/ / define the size of the canvas
If (img.width > 300 | | img.height > 300) {
If (img.width > img.height) {
Canvas.width=300
Canvas.height=img.height/img.width*300
}
Else {
Canvas.height=300
Canvas.width=img.width/img.height*300
}
}
/ / start intercepting pictures from there
Context.drawImage (img,0,0,canvas.width,canvas.height)
/ * the first parameter is the created img object; the second parameter is the upper-left corner coordinates, and the last two are the width and height of the canvas area * /
/ / compressed image base64url
/ * canvas.toDataURL (mimeType,qualityArgument), the default value of mimeType is' image/jpeg'
* qualityArgument indicates the quality of the exported images. This parameter will only have an effect when exported to jpg and webp formats. The default value is 0.92 images /.
/ / varnewUrl=canvas.toDataURL ('image/jpeg',0.92); / / base64 format
Document.body.removeChild (span)
Document.querySelector ('body') .appendChild (canvas)
}
}
/ / this is uploaded
Document.querySelector ('button'). AddEventListener (' click',function () {
Canvas.toBlob (function (blob) {
Varxhr=newXMLHttpRequest ()
FormData.append ('fileimage',blob,'002.webp')
Xhr.open ('POST','test.php',true) / / default true is asynchronous
/ / upload progress
Xhr.upload.addEventListener ('progress',function (e) {
Console.log ('upload progress is:' + (e.loaded/e.total*100) .tofixed (2) +'%') / / occurs multiple times
/ / event.total is the total number of bytes that need to be transferred, and event.loaded is the bytes that have already been transferred
})
Xhr.upload.addEventListener ('loadstart',function () {
Console.log ('upload start') / / appears only once
})
Xhr.onreadystatechange=function () {
If (xhr.readyState===4) {
If (xhr.status > = 200&&xhr.status