Html5移动端上传图片并裁剪 - Clipic.js

JavaScript011

Html5移动端上传图片并裁剪 - Clipic.js,第1张

Clipic.js插件可以为移动端 (仅支持移动端) 提供头像上传并裁剪成指定尺寸,用原生js开发的,轻量级,包含html跟css,不到8kb。点此链接体验: https://teojs.github.io/clipic/

https://github.com/teojs/clipic

参数说明

width:Number (默认:500) – 裁剪宽度

height:Number (默认:500) – 裁剪高度

ratio:Number (可选) – 裁剪的比例,当传入ratio时width/height将无效

src:String (必传) – 需要裁剪的图片,可以是图片链接,或者 base64

type:String (默认:jpeg) – 裁剪后图片的类型,仅支持 jpeg/png 两种

quality:Number (默认:0.9) – 压缩质量

buttonText:Array (默认:[‘取消’, ‘重置’, ‘完成’]) – 底部三个按钮文本

http://bbs.itying.com/topic/5cb17892c6a71b10bcef96b0

http://love21cn.msn.com.cn的图片上传功能后可以实现区域截图,也可以实现放大缩小...估计是用了JS来实现的:

var div_move = 0

var IE = document.all?true:false

var tempX,tempY,oldX,oldY

var have_move = 0

function grasp()

{

div_move = 1

if(IE)

{

document.getElementById("source_div").setCapture()

}

}

function free()

{

div_move = 0

have_move = 0

document.getElementById("source_div").releaseCapture()

}

function getMouseXY(e)

{

if (IE)

{ // grab the x-y pos.s if browser is IE

tempX = event.clientX + document.body.scrollLeft

tempY = event.clientY + document.body.scrollTop

}

else

{

// grab the x-y pos.s if browser is NS

tempX = e.pageX

tempY = e.pageY

}

// catch possible negative values in NS4

if (tempX <0){tempX = 0}

if (tempY <0){tempY = 0}

}

function move_it(e)

{

getMouseXY(e)

if(div_move == 1)

{

if(have_move == 0)

{

//alert('a')

oldX = tempX

oldY = tempY

have_move = 1

}

var left = parseInt(document.getElementById("source_div").style.left)

var top = parseInt(document.getElementById("source_div").style.top)

//alert(top)

//alert(left)

//alert(tempX)

//alert(oldX)

document.getElementById("source_div").style.left = left + tempX - oldX

document.getElementById("source_div").style.top = top + tempY - oldY

oldX = tempX

oldY = tempY

}

}

function change_size(method)

{

if(method == 1)

{

var per = 1.25

}

else

{

var per = 0.8

}

document.getElementById("show_img").width = document.getElementById("show_img").width*per

//document.getElementById("show_img").height = document.getElementById("show_img").height*per

}

function micro_move(method)

{

switch (method)

{

case "up":

var top = parseInt(document.getElementById("source_div").style.top)

document.getElementById("source_div").style.top = top - 5

break

case "down":

var top = parseInt(document.getElementById("source_div").style.top)

document.getElementById("source_div").style.top = top + 5

break

case "left":

var left = parseInt(document.getElementById("source_div").style.left)

document.getElementById("source_div").style.left = left - 5

break

case "right":

var left = parseInt(document.getElementById("source_div").style.left)

document.getElementById("source_div").style.left = left + 5

break

}

}

function turn(method)

{

var i=document.getElementById('show_img').style.filter.match(/\d/)[0]

//alert(i)

i=parseInt(i)+parseInt(method)

//alert(i)

if(i<0)

{

i += 4

}

if(i>=4)

{

i -= 4

}

//alert(i)

document.getElementById('show_img').style.filter='progid:DXImageTransform.Microsoft.BasicImage(Rotation='+i+')'

}

function mysub()

{

var Oform = document.myform

Oform.go.value = 1

Oform.width.value = document.getElementById("show_img").width

Oform.left.value = document.getElementById("source_div").style.left

Oform.top.value = document.getElementById("source_div").style.top

if(IE)

{

Oform.turn.value = document.getElementById('show_img').style.filter.match(/\d/)[0]

}

Oform.submit()

}

苹果树下也有类似功能不过,功能要比你所说的强大的多...

资料搜集于百度知道!

推荐采用webuploader控件来解决。

关于WebUploader的功能说明:

大文件上传续传

支持超大文件上传(100G+)和续传,可以关闭浏览器,重启系统后仍然继续上传。

开源

提供ASP.NET,JSP,PHP示例和源代码,其中JSP提供MySQL,Oracle,SQL Server数据库的配置和示例代码。

分片、并发

分片与并发结合,将一个大文件分割成多块,并发上传,极大地提高大文件的上传速度。

当网络问题导致传输错误时,只需要重传出错分片,而不是整个文件。另外分片传输能够更加实时的跟踪上传进度。

预览、压缩

支持常用图片格式jpg,jpeg,gif,bmp,png预览与压缩,节省网络数据传输。

解析jpeg中的meta信息,对于各种orientation做了正确的处理,同时压缩后上传保留图片的所有原始meta数据。

多途径添加文件

支持文件多选,类型过滤,拖拽(文件&文件夹),图片粘贴功能。上传本地指定路径的文件,不需要通过点击按钮选择文件。

粘贴功能主要体现在当有图片数据在剪切板中时(截屏工具如QQ(Ctrl + ALT + A), 网页中右击图片点击复制),Ctrl + V便可添加此图片文件。

HTML5 &FLASH

兼容主流浏览器和低版本浏览器,接口一致,实现了两套运行时支持,用户无需关心内部用了什么内核。而且支持IE6,IE8浏览器。

同时Flash部分没有做任何UI相关的工作,方便不关心flash的用户扩展和自定义业务需求。

基于内存映射模式进行IO操作,充分发挥操作系统性能。

MD5秒传

当文件体积大、量比较多时,支持上传前做文件md5值验证,一致则可直接跳过。

如果服务端与前端统一修改算法,取段md5,可大大提升验证性能,耗时在20ms左右。

易扩展、可拆分

采用可拆分机制, 将各个功能独立成了小组件,可自由搭配。

采用AMD规范组织代码,清晰明了,方便高级玩家扩展。