网页端 裁剪图片,不需要经过服务器。
这个是用 https://github.com/mailru/FileAPI 框架实现的。配合jcrop.
高级浏览器 使用 canvas 裁剪,ie6 7 8使用 flash过度。
核心代码:
var el = $('input').get(0); seajs.use(['gallery/jcrop/0.9.12/jcrop.css','gallery/jcrop/0.9.12/jcrop.js'] ,function(){ FileAPI.event.on(el, 'change', function (evt){ var files = FileAPI.getFiles(evt); // Retrieve file list FileAPI.filterFiles(files, function (file, info){ if( !/^image/.test(file.type) ){ alert('图片格式不正确'); return false; } else if(file.size > 20 * FileAPI.MB){ alert('图片必须小于20M'); return false; } else{ return true; } }, function (files, rejected){ console.log(files); if( files.length ){ var file = files[0]; var img0 = FileAPI.Image(file); var img1 = FileAPI.Image(file); var ratio = 0; FileAPI.getInfo(file, function (err, info) { //get image ratio if (!err) { if (info.width > info.height) { ratio = info.width / 500; } else { ratio = info.height / 500; } } }); img0.resize(500, 500, 'max') //place image and register jcrop .get(function(err, img) { $('#img2').empty(); $('#img2').append($(img)); $('#img2').children().Jcrop({ aspectRatio: 1, bgColor: 'rgba(0,0,0,0.4)', onSelect: function(c) { img1.matrix.sx = c.x * ratio; img1.matrix.sy = c.y * ratio; img1.matrix.sw = c.w * ratio; img1.matrix.sh = c.h * ratio; img1.matrix.dw = 500; img1.matrix.dh = 500; img1.get(function(err, img) { // $('#img3').empty(); // $('#img3').append($(img)); $('#img3').html($(img)); }); } }); }); $('#btn').on('click',function(){ FileAPI.upload({ // url: '/testUpFile/upFile', // headers: { 'Content-Type': 'multipart/form-data' }, files: { images: img1 }, progress: function (evt){ /* ... */ }, complete: function (err, xhr){ /* ... */ //alert(xhr.responseText); console.log(xhr); } }); }); } }); }); });
完整代码:
<!DOCTYPE html> <html> <head> <title>TODO supply a title</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width"> <script src="/UploadFiles/2021-04-02/jquery.min.js">以上所述就是本文的全部内容了,希望大家能够喜欢。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月25日
2024年09月25日
- 李明依.1989-小女生【滚石】【WAV+CUE】
- 《李荣浩 新时代创作新人王》[WAV分轨][310MB]
- 《黑鸭子与马头琴 史上最动听草原音乐》[WAV+CUE][320MB]
- 《汽车发烧音乐精选CD18》[FLAC+CUE][7.1G]
- 郑希怡.2004-Perfect.Date【英皇娱乐】【FLAC分轨】
- 群星.1994-宝丽金超级孖宝2CD【宝丽金】【WAV+CUE】
- 林忆莲.2019-0(2023精装版)【环球】【WAV+CUE】
- Supper.Moment.2016-The.Moment【寰亚】【FLAC分轨】
- 范莉丝.1979-送郎一朵牵牛花(LP版)【大联机构】【WAV+CUE】
- 刘宇.2024-宇宙诗人【哔哟哔哟文化】【FLAC分轨】
- 《全景声白噪音》沉浸式大自然助眠 带你听自然的声音[mp3]
- 纯音入心系列纯音乐《古筝精选 十指弹一赏心曲一心奏出世人心》1CD[MP3][582MB]
- 纯音入心系列纯音乐《古筝与现代音乐的交织》1CD[MP3][96MB]
- 王俪婷.2011-ROMANCE【华研国际】【WAV+CUE】
- 狮子合唱团.2016-LION(引进版)【华纳】【WAV+CUE】