JS Realizes Scale down Picture Width and Height

  • 2021-08-06 20:56:12
  • OfStack

In this paper, we share the specific code of JS to reduce the width and height of pictures proportionally for your reference. The specific contents are as follows


<!DOCTYPE html>
<html>
<head>
 <title>JS  Scale out the width and height of the picture </title>
</head>
 
<body>
 <div>
 <input type="file" name="" id="upload">
 <img src="" alt="" id="preview">
 </div>
</body>
<script>
 var upd =document.getElementById('upload');
 upd.addEventListener('change',function(e){
 var file=e.target.files[0]; 
 var reader=new FileReader();
 var img = document.createElement('img');
 var canvas=document.createElement('canvas');
 var context=canvas.getContext('2d'); 
 
 reader.onload=function(e){ 
 img.src = e.target.result;
 img.onload = function () {
 var imgWidth=this.width;// Width of uploaded picture 
 var imgHeight = this.height;// The height of uploading pictures  
 // Scale the width and height of the picture  
 var targetWidth = imgWidth;
 var targetHeight = imgHeight; 
 var maxWidth=1920;// Maximum width of picture 
 var maxHeight = 1080;// Maximum height of picture  
 var scale = imgWidth / imgHeight;// Width-height ratio of original image 
 
 // If the original width is greater than the maximum width 
 if(imgWidth>maxWidth){
 targetWidth = maxWidth;
 targetHeight = targetWidth/scale;
 }
 // After scaling, the height is still greater than the maximum height and continues to scale down 
 if(targetHeight>maxHeight){
 targetHeight = maxHeight
 targetWidth = targetHeight*scale;
 }
 canvas.width=targetWidth;//canvas Width of = Width of picture 
 canvas.height=targetHeight;//canvas The height of = Height of picture 
 
 context.clearRect(0,0,targetWidth,targetHeight)// Clean up canvas
 context.drawImage(img,0,0,targetWidth,targetHeight)//canvas Drawing 
 var newUrl=canvas.toDataURL('image',0.92);//canvas Export to base64
 preview.src=newUrl
 }
 }
 reader.readAsDataURL(file);
 })
</script>
 
</html>

This site will share another paragraph for you: the picture is automatically scaled according to the width-height ratio


/**
 *  Pictures are automatically scaled to width-height ratio 
 * @param ImgObj
 *  Zoom the picture source object 
 * @param maxWidth
 *  Maximum width allowed for scaling 
 * @param maxHeight
 *  Maximum Allowable Height for Zoom 
 * @usage 
 *  Call: <img src=" Picture " onload="javascript:DrawImage(this,100,100)">
 */
function DrawImage(ImgObj, maxWidth, maxHeight){
 var image = new Image();
 // The original address of the original picture (used to obtain the true width and height of the original picture, when <img> The label is not affected when it specifies width and height) 
 image.src = ImgObj.src;
 //  Used to set the width and height of the picture 
 var tempWidth;
 var tempHeight;
 
 if(image.width > 0 && image.height > 0){
 // Width-height ratio of original picture   Greater than   Specify the width-height ratio, which shows that the width of the original picture must be  >  Height 
 if (image.width/image.height >= maxWidth/maxHeight) {
  if (image.width > maxWidth) {
  tempWidth = maxWidth;
  //  Zoom to the scale of the original picture 
  tempHeight = (image.height * maxWidth) / image.width;
  } else {
  //  Zoom to the size of the original picture 
  tempWidth = image.width;
  tempHeight = image.height;
  }
 } else {//  The height of the original picture is inevitable  >  Width 
  if (image.height > maxHeight) { 
  tempHeight = maxHeight;
  //  Zoom to the scale of the original picture 
  tempWidth = (image.width * maxHeight) / image.height; 
  } else {
  //  Zoom to the size of the original picture 
  tempWidth = image.width;
  tempHeight = image.height;
  }
 }
 //  Set the width and height of the page picture 
 ImgObj.height = tempHeight;
 ImgObj.width = tempWidth;
 //  Prompt the original size of the picture 
 ImgObj.alt = image.width + " × " + image.height;
 }
}

Related articles: