图片切成html5代码,jQuery+html5图片上传并裁切预览

function bytesToSize(bytes) {

var sizes = ['Bytes', 'KB', 'MB'];

if (bytes == 0) return 'n/a';

var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));

return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];

};

function checkForm() {

if (parseInt($('#w').val())) return true;

$('.error').html('Please select a crop region and then press Upload').show();

return false;

};

// 更新裁切的图片

function updateInfo(e) {

$('#x1').val(e.x);

$('#y1').val(e.y);

$('#x2').val(e.x2);

$('#y2').val(e.y2);

$('#w').val(e.w);

$('#h').val(e.h);

};

// clear info by cropping (onRelease event handler)

function clearInfo() {

$('.info #w').val('');

$('.info #h').val('');

};

function fileSelectHandler() {

// get selected file

var oFile = $('#image_file')[0].files[0];

// hide all errors

$('.error').hide();

// check for image type (jpg and png are allowed)

var rFilter = /^(image\/jpeg|image\/png)$/i;

if (! rFilter.test(oFile.type)) {

$('.error').html('请选择图像文件').show();

return;

}

// check for file size

if (oFile.size > 250 * 1024) {

$('.error').html('文件太大,请选择一张稍小点的图片').show();

return;

}

// preview element

var oImage = document.getElementById('preview');

// prepare HTML5 FileReader

var oReader = new FileReader();

oReader.onload = function(e) {

// e.target.result contains the DataURL which we can use as a source of the image

oImage.src = e.target.result;

oImage.onload = function () { // onload event handler

// display step 2

$('.step2').fadeIn(500);

// display some basic image info

var sResultFileSize = bytesToSize(oFile.size);

$('#filesize').val(sResultFileSize);

$('#filetype').val(oFile.type);

$('#filedim').val(oImage.naturalWidth + ' x ' + oImage.naturalHeight);

// Create variables (in this scope) to hold the Jcrop API and image size

var jcrop_api, boundx, boundy;

// destroy Jcrop if it is existed

if (typeof jcrop_api != 'undefined')

jcrop_api.destroy();

// initialize Jcrop

$('#preview').Jcrop({

minSize: [32, 32], // min crop size

aspectRatio : 1, // keep aspect ratio 1:1

bgFade: true, // use fade effect

bgOpacity: .3, // fade opacity

onChange: updateInfo,

onSelect: updateInfo,

onRelease: clearInfo

}, function(){

// use the Jcrop API to get the real image size

var bounds = this.getBounds();

boundx = bounds[0];

boundy = bounds[1];

// Store the Jcrop API in the jcrop_api variable

jcrop_api = this;

});

};

};

// Download by 链接已屏蔽

// read selected file as DataURL

oReader.readAsDataURL(oFile);

}



更多源码 | 好库简介 | 网站地图 | 帮助中心 | 版权说明

Copyright© 2009-2012 OKBASE.NET All Rights Reserved 好库网 版权所有

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值