Home Web Front-end H5 Tutorial H5 mobile image compression upload development process

H5 mobile image compression upload development process

Dec 07, 2016 pm 01:19 PM
h5

H5 activities have become very common, and one of the forms is to allow users to upload pictures to participate. When uploading pictures on the mobile terminal, users usually upload pictures from the mobile phone album. Nowadays, the shooting quality of mobile phones is getting higher and higher, and the size of a single photo is generally around 3M. If you upload it directly, it consumes a lot of traffic and the experience is not good. Therefore, local compression needs to be performed before uploading.

Next, I will summarize the image compression and upload function in the development of h5 activities, and mark several pitfalls I have encountered, and share it with everyone:

A must-see for beginners

If you have no idea about mobile image uploading, It is necessary to add the three concepts of FileReader, Blob, and FormData.

1.FileReader

Definition

Using the FileReader object, web applications can asynchronously read the contents of files (or raw data buffers) stored on the user's computer. You can use File objects or Blob objects to specify the files to be processed. Or data.

Method

H5 mobile image compression upload development process

Event handler

Use

var fileReader = new FileReader();
fileReader.onload = function() {
 var url = this.result;
}
//or
fileReader.onload = function(e) {
 var url = e.target.result;
}
Copy after login

2.Blob

BLOB (binary large object), a binary large object, is a container that can store binary files.

3.FormData

Using the FormData object, you can use a series of key-value pairs to simulate a complete form, and then use XMLHttpRequest to send this "form".

To the point

Mobile image compression upload process:
1) Input file to upload the image, use FileReader to read the image uploaded by the user;
2) Pass the image data into the img object, draw the img to the canvas, and then use canvas.toDataURL for compression;
3) Get the compressed base64 Format image data, convert it into binary, stuff it into formdata, and finally submit the formdata through xmlHttpRequest;

1. Get image data

fileEle.onchange = function() {
  if (!this.files.length) return;
  //以下考虑的是单图情况
  var _ua = window.navigator.userAgent;
  var _simpleFile = this.files[0];
  //判断是否为图片
  if (!/\/(?:jpeg|png|gif)/i.test(_simpleFile.type)) return;
  //插件exif.js获取ios图片的方向信息
  var _orientation;
  if(_ua.indexOf('iphone') > 0) {
    EXIF.getData(_simpleFile,function(){
      _orientation=EXIF.getTag(this,'Orientation');
    });
  }
  //1.读取文件,通过FileReader,将图片文件转化为DataURL,即data:img/png;base64,开头的url,可以直接放在image.src中;
  var _reader = new FileReader(),
    _img = new Image(),
    _url;
  _reader.onload = function() {
    _url = this.result;
    _img.url = _url;
    _img.onload = function () {
      var _data = compress(_img);
      uploadPhoto(_data, _orientation);
    };
  };
  _reader.readAsDataURL(_simpleFile);
};
Copy after login

2. Compress the image

/**
 * 计算图片的尺寸,根据尺寸压缩
 * 1. iphone手机html5上传图片方向问题,借助exif.js
 * 2. 安卓UC浏览器不支持 new Blob(),使用BlobBuilder
 * @param {Object} _img     图片
 * @param {Number} _orientation 照片信息
 * @return {String}       压缩后base64格式的图片
 */
function compress(_img, _orientation) {
  //2.计算符合目标尺寸宽高值,若上传图片的宽高都大于目标图,对目标图等比压缩;如果有一边小于,对上传图片等比放大。
  var _goalWidth = 750,         //目标宽度
    _goalHeight = 750,         //目标高度
    _imgWidth = _img.naturalWidth,   //图片宽度
    _imgHeight = _img.naturalHeight,  //图片高度
    _tempWidth = _imgWidth,      //放大或缩小后的临时宽度
    _tempHeight = _imgHeight,     //放大或缩小后的临时宽度
    _r = 0;              //压缩比
  if(_imgWidth === _goalWidth && _imgHeight === _goalHeight) {
  } else if(_imgWidth > _goalWidth && _imgHeight > _goalHeight) {//宽高都大于目标图,需等比压缩
    _r = _imgWidth / _goalWidth;
    if(_imgHeight / _goalHeight < _r) {
      _r = _imgHeight / _goalHeight;
    }
    _tempWidth = Math.ceil(_imgWidth / _r);
    _tempHeight = Math.ceil(_imgHeight / _r);
  } else {
    if(_imgWidth < _goalWidth && _imgHeight < _goalHeight) {//宽高都小于
      _r = _goalWidth / _imgWidth;
      if(_goalHeight / _imgHeight < _r) {
        _r = _goalHeight / _imgHeight;
      }
    } else {
      if(_imgWidth < _goalWidth) {     //宽小于
        _r = _goalWidth / _imgWidth;
      } else{               //高小于
        _r = _goalHeight / _imgHeight;
      }
    }
    _tempWidth = Math.ceil(_imgWidth * _r);
    _tempHeight = Math.ceil(_imgHeight * _r);
  }
  //3.利用canvas对图片进行裁剪,等比放大或缩小后进行居中裁剪
  var _canvas = e._$get(&#39;canvas-clip&#39;);
  if(!_canvas.getContext) return;
  var _context = _canvas.getContext(&#39;2d&#39;);
  _canvas.width = _tempWidth;
  _canvas.height = _tempHeight;
  var _degree;
  //ios bug,iphone手机上可能会遇到图片方向错误问题
  switch(_orientation){
    //iphone横屏拍摄,此时home键在左侧
    case 3:
      _degree=180;
      _tempWidth=-_imgWidth;
      _tempHeight=-_imgHeight;
      break;
    //iphone竖屏拍摄,此时home键在下方(正常拿手机的方向)
    case 6:
      _canvas.width=_imgHeight;
      _canvas.height=_imgWidth; 
      _degree=90;
      _tempWidth=_imgWidth;
      _tempHeight=-_imgHeight;
      break;
    //iphone竖屏拍摄,此时home键在上方
    case 8:
      _canvas.width=_imgHeight;
      _canvas.height=_imgWidth; 
      _degree=270;
      _tempWidth=-_imgWidth;
      _tempHeight=_imgHeight;
      break;
  }
  if(window.navigator.userAgent.indexOf(&#39;iphone&#39;) > 0 && !!_degree) {
    _context.rotate(_degree*Math.PI/180);
    _context.drawImage(_img, 0, 0, _tempWidth, _tempHeight); 
  } else {
    _context.drawImage(_img, 0, 0, _tempWidth, _tempHeight);
  }
  //toDataURL方法,可以获取格式为"data:image/png;base64,***"的base64图片信息;
  var _data = _canvas.toDataURL(&#39;image/jpeg&#39;);
  return _data;
}
Copy after login

3. Upload the image

/**
 * 上传图片到NOS
 * @param {Object} _blog Blob格式的图片
 * @return {Void}
 */
function uploadPhoto(_data) {
  //4.获取canvas中的图片信息
  //window.atob方法将其中的base64格式的图片转换成二进制字符串;若将转换后的值直接赋值给Blob会报错,需Uint8Array转换:最后创建Blob对象;
  _data = _data.split(&#39;,&#39;)[1];
  _data = window.atob(_data);
  //如果不用ArrayBuffer,发送给服务器的图片格式是[object Uint8Array],上传失败...
  var _buffer = new ArrayBuffer(_data.length);
  var _ubuffer = new Uint8Array(_buffer);
  for (var i = 0; i < _data.length; i++) {
    _ubuffer[i] = _data.charCodeAt(i);
  }
  // 安卓 UC浏览器不支持 new Blob(),使用BlobBuilder
  var _blob;
  try {
    _blob = new Blob([_buffer], {type:&#39;image/jpeg&#39;});
  } catch(ee) {
    window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder;
    if (ee.name == &#39;TypeError&#39; && window.BlobBuilder) {
      var _bb = new BlobBuilder();
      _bb.append(_buffer);
      _blob = _bb.getBlob(&#39;image/jpeg&#39;);
    }
  }
  var _suffix = &#39;jpg&#39;;
  if(_blob.type === &#39;image/jpeg&#39;) {
    _suffix = &#39;jpg&#39;;
  }
  //获取NOStoken
  this.__cache._$requestDWRByGet({url: &#39;ImageBean.genTokens&#39;,param: [_suffix,&#39;&#39;,&#39;&#39;,&#39;&#39;,&#39;1&#39;],onload: function(_tokens) {
    _tokens = _tokens || [];
    var _token = _tokens[0];
    if(!_token || !_token.objectName || !_token.uploadToken){
      alert(&#39;token获取失败!&#39;);
      return false;
    }
    //上传图片到NOS
    var _objectName = _token.objectName,
      _uploadToken = _token.uploadToken,
      _bucketName = _token.bucketName;
    var _formData = new FormData();
    _formData.append(&#39;Object&#39;, _objectName);
    _formData.append(&#39;x-nos-token&#39;, _uploadToken);
    _formData.append(&#39;file&#39;,_blob);
    var _xhr;
    if (window.XMLHttpRequest) {
      _xhr = new window.XMLHttpRequest();
    } else if (window.ActiveXObject) {
      _xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    _xhr.onreadystatechange = function() {
      if(_xhr.readyState === 4) {
        if((_xhr.status >= 200 && _xhr.status < 300) || _xhr.status === 304) {
          var _imgurl = "http://nos.netease.com/" + _bucketName + "/" + _objectName + "?imageView";
          var _newUrl = mb.x._$imgResize(_imgurl, 750, 750, 1, true);
          window.location.href = &#39;http://www.lofter.com/act/taxiu?op=effect&originImgUrl=&#39; + _newUrl;
        }
      }
    };
    _xhr.open(&#39;POST&#39;, &#39;http://nos.netease.com/&#39; + _bucketName, true);
    _xhr.send(_formData);
  }});
}
Copy after login

A plug-in to determine the direction of the picture taken by the iPhone: exif

This completes the H5 image compression and upload process.


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does h5 mean? What does h5 mean? Aug 02, 2023 pm 01:52 PM

H5 refers to HTML5, the latest version of HTML. H5 is a powerful markup language that provides developers with more choices and creative space. Its emergence promotes the development of Web technology, making the interaction and effect of web pages more Excellent, as H5 technology gradually matures and becomes popular, I believe it will play an increasingly important role in the Internet world.

How to distinguish between H5, WEB front-end, big front-end, and WEB full stack? How to distinguish between H5, WEB front-end, big front-end, and WEB full stack? Aug 03, 2022 pm 04:00 PM

This article will help you quickly distinguish between H5, WEB front-end, large front-end, and WEB full stack. I hope it will be helpful to friends in need!

How to implement h5 to slide up on the web side to load the next page How to implement h5 to slide up on the web side to load the next page Mar 11, 2024 am 10:26 AM

Implementation steps: 1. Monitor the scroll event of the page; 2. Determine whether the page has scrolled to the bottom; 3. Load the next page of data; 4. Update the page scroll position.

How to use position in h5 How to use position in h5 Dec 26, 2023 pm 01:39 PM

In H5, you can use the position attribute to control the positioning of elements through CSS: 1. Relative positioning, the syntax is "style="position: relative;"; 2. Absolute positioning, the syntax is "style="position: absolute;" "; 3. Fixed positioning, the syntax is "style="position: fixed;" and so on.

How to implement H5 form validation component in vue3 How to implement H5 form validation component in vue3 Jun 03, 2023 pm 02:09 PM

The rendering description is based on vue.js and does not rely on other plug-ins or libraries; the basic functions remain consistent with element-ui, and some adjustments have been made to the internal implementation for mobile terminal differences. The current construction platform is built using the uni-app official scaffolding. Because most mobile terminals currently have two types: h6 and WeChat mini-programs, it is very suitable for technology selection to run one set of code on multiple terminals. Implementation idea core api: use provide and inject, corresponding to and. In the component, a variable (array) is used internally to store all instances, and the data to be transferred is exposed through provide; the component uses inject internally to receive the data provided by the parent component, and finally combines its own attributes with method submission

Is h5 same as HTML5? Is h5 same as HTML5? Apr 08, 2025 am 12:16 AM

"h5" and "HTML5" are the same in most cases, but they may have different meanings in certain specific scenarios. 1. "HTML5" is a W3C-defined standard that contains new tags and APIs. 2. "h5" is usually the abbreviation of HTML5, but in mobile development, it may refer to a framework based on HTML5. Understanding these differences helps to use these terms accurately in your project.

What Does H5 Refer To? Exploring the Context What Does H5 Refer To? Exploring the Context Apr 12, 2025 am 12:03 AM

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

H5 Code: Accessibility and Semantic HTML H5 Code: Accessibility and Semantic HTML Apr 09, 2025 am 12:05 AM

H5 improves web page accessibility and SEO effects through semantic elements and ARIA attributes. 1. Use, etc. to organize the content structure and improve SEO. 2. ARIA attributes such as aria-label enhance accessibility, and assistive technology users can use web pages smoothly.

See all articles