ajax头像上传
html代码:
<input id="fileinput" type="file" /><br />
<br />
<div id="canvasContainer">
<canvas id="container"></canvas>
<div id="picker">
<div id="resize"></div>
</div>
</div>
<br />
<button id="bigger">原图放大</button>
<button id="smaller">原图缩小</button>
<p>结果:</p>
<div>
<canvas id="res1"></canvas>
<canvas id="res2"></canvas>
<canvas id="res3"></canvas>
<button id="download">下载 </button>
<button id="upload">上传 </button>
(demo只上传/下载第一张图片)
<div id="uploadres"></div>
</div>
<p>滤镜:</p>
<div id="filters"></div>
jQuery操作代码:
$(function () {
var canvas = document.getElementById("container"),
context = canvas.getContext("2d"),
//文件服务器地址
fileServer = "Handler.ashx",
//适配环境,随时修改事件名称
eventName = { down: "mousedown", move: "mousemove", up: "mouseup", click: "click" };
//////////canvas尺寸配置
var canvasConfig = {
//容器canvas尺寸
width: 500,
height: 300,
//原图放大/缩小
zoom: 1,
//图片对象
img: null,
//图片完整显示在canvas容器内的尺寸
size: null,
//图片绘制偏移,为了原图不移出框外,这个只能是负值or 0
offset: { x: 0, y: 0 },
//当前应用的滤镜
filter: null
}
canvas.width = canvasConfig.width;
canvas.height = canvasConfig.height;
///////////设置选择工具配置
var config = {
//图片选择框当前大小、最大大小、最小大小
pickerSize: 100,
minSize: 50,
maxSize: 200,
x: canvas.width / 2 - 100 / 2,
y: canvas.height / 2 - 100 / 2
}
/////////////结果canvas配置
var resCanvas = [$("#res1")[0].getContext("2d"), $("#res2")[0].getContext("2d"), $("#res3")[0].getContext("2d")];
//结果canvas尺寸配置
var resSize = [100, 50, 32]
resSize.forEach(function (size, i) {
$("#res" + (i + 1))[0].width = size;
$("#res" + (i + 1))[0].height = size;
});
//////// 滤镜配置
var filters = [];
filters.push({
name: "灰度", func: function (pixelData) {
//r、g、b、a
//灰度滤镜公式: gray=r*0.3+g*0.59+b*0.11
var gray;
for (var i = 0; i < canvasConfig.width * canvasConfig.height; i++) {
gray = pixelData[4 * i + 0] * 0.3 + pixelData[4 * i + 1] * 0.59 + pixelData[4 * i + 2] * 0.11;
pixelData[4 * i + 0] = gray;
pixelData[4 * i + 1] = gray;
pixelData[4 * i + 2] = gray;
}
}
});
filters.push({
name: "黑白", func: function (pixelData) {
//r、g、b、a
//黑白滤镜公式: 0 or 255
var gray;
for (var i = 0; i < canvasConfig.width * canvasConfig.height; i++) {
gray = pixelData[4 * i + 0] * 0.3 + pixelData[4 * i + 1] * 0.59 + pixelData[4 * i + 2] * 0.11;
if (gray > 255 / 2) {
gray = 255;
}
else {
gray = 0;
}
pixelData[4 * i + 0] = gray;
pixelData[4 * i + 1] = gray;
pixelData[4 * i + 2] = gray;
}
}
});
filters.push({
name: "反色", func: function (pixelData) {
for (var i = 0; i < canvasConfig.width * canvasConfig.height; i++) {
pixelData[i * 4 + 0] = 255 - pixelData[i * 4 + 0];
pixelData[i * 4 + 1] = 255 - pixelData[i * 4 + 1];
pixelData[i * 4 + 2] = 255 - pixelData[i * 4 + 2];
}
}
});
filters.push({ name: "无", func: null });
// 添加滤镜按钮
filters.forEach(function (filter) {
var button = $("<button>" + filter.name + "</button>");
button.on(eventName.click, function () {
canvasConfig.filter = filter.func;
//重绘
draw(context, canvasConfig.img, canvasConfig.size);
})
$("#filters").append(button);
});
//下载生成的图片(只下载第一张)
$("#download").on(eventName.click, function () { //将mime-type改为image/octet-stream,强制让浏览器直接download
var _fixType = function (type) {
type = type.toLowerCase().replace(/jpg/i, 'jpeg');
var r = type.match(/png|jpeg|bmp|gif/)[0];
return 'image/' + r;
};
var saveFile = function (data, filename) {
var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
save_link.href = data;
save_link.download = filename;
var event = document.createEvent('MouseEvents');
event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
save_link.dispatchEvent(event);
};
var imgData = $("#res1")[0].toDataURL("png");
imgData = imgData.replace(_fixType("png"), 'image/octet-stream');//base64
saveFile(imgData, "头像created on" + new Date().getTime() + "." + "png");
});
//上传图片
$("#upload").on(eventName.click, function () {
var imgData = $("#res1")[0].toDataURL("png");
imgData = imgData.replace(/^data:image\/(png|jpg);base64,/, "");
alert(fileServer);
if (!fileServer) {
alert("请配置文件服务器地址");
return;
} var blobBin = atob(imgData);
var array = [];
for (var i = 0; i < blobBin.length; i++) {
array.push(blobBin.charCodeAt(i));
}
var blob = new Blob([new Uint8Array(array)], { type: 'image/png' });
var file = new File([blob], "userlogo.png", { type: 'image/png' });
var formdata = new FormData();
formdata.append("userlogo", file);
$.ajax({
type: 'POST',
url: fileServer,
data: formdata,
processData: false,
contentType: false,
success: function (msg) {
$("#uploadres").text(JSON.stringify(msg));
}
});
});
//绑定选择图片事件
$("#fileinput").change(function () {
var file = this.files[0],
URL = (window.webkitURL || window.URL),
url = URL.createObjectURL(file),
img = new Image();
img.src = url;
img.onload = function () {
canvasConfig.img = img;
canvasConfig.size = getFixedSize(img, canvas);
draw(context, img, canvasConfig.size);
setPicker();
} });
//移动选择框
//绑定鼠标在选择工具上按下的事件
$("#picker").on(eventName.down, function (e) {
e.stopPropagation();
var start = { x: e.clientX, y: e.clientY, initX: config.x, initY: config.y };
$("#canvasContainer").on(eventName.move, function (e) {
// 将x、y限制在框内
config.x = Math.min(Math.max(start.initX + e.clientX - start.x, 0), canvasConfig.width - config.pickerSize);
config.y = Math.min(Math.max(start.initY + e.clientY - start.y, 0), canvasConfig.height - config.pickerSize);
setPicker();
})
});
//原图移动事件
$("#container").on(eventName.down, function (e) {
e.stopPropagation();
var start = { x: e.clientX, y: e.clientY, initX: canvasConfig.offset.x, initY: canvasConfig.offset.y };
var size = canvasConfig.size;
$("#canvasContainer").on(eventName.move, function (e) {
// 将x、y限制在框内
// 坐标<0 当图片大于容器 坐标>容器-图片 否则不能移动
canvasConfig.offset.x = Math.max(Math.min(start.initX + e.clientX - start.x, 0), Math.min(canvasConfig.width - size.width * canvasConfig.zoom, 0));
canvasConfig.offset.y = Math.max(Math.min(start.initY + e.clientY - start.y, 0), Math.min(canvasConfig.height - size.height * canvasConfig.zoom, 0));
//重绘蒙版
draw(context, canvasConfig.img, canvasConfig.size);
})
});
//改变选择框大小事件
$("#resize").on(eventName.down, function (e) {
e.stopPropagation();
var start = { x: e.clientX, init: config.pickerSize };
$("#canvasContainer").on(eventName.move, function (e) {
config.pickerSize = Math.min(Math.max(start.init + e.clientX - start.x, config.minSize), config.maxSize);
$("#picker").css({ width: config.pickerSize, height: config.pickerSize });
draw(context, canvasConfig.img, canvasConfig.size);
})
});
$(document).on(eventName.up, function (e) {
$("#canvasContainer").unbind(eventName.move);
})
//原图放大、缩小
$("#bigger").on(eventName.click, function () {
canvasConfig.zoom = Math.min(3, canvasConfig.zoom + 0.1);
//重绘蒙版
draw(context, canvasConfig.img, canvasConfig.size);
})
$("#smaller").on(eventName.click, function () {
canvasConfig.zoom = Math.max(0.4, canvasConfig.zoom - 0.1);
//重绘蒙版
draw(context, canvasConfig.img, canvasConfig.size);
}) // 定位选择工具
function setPicker() {
$("#picker").css({
width: config.pickerSize + "px", height: config.pickerSize + "px",
top: config.y, left: config.x
});
//重绘蒙版
draw(context, canvasConfig.img, canvasConfig.size);
}
//绘制canvas中的图片和蒙版
function draw(context, img, size) {
var pickerSize = config.pickerSize,
zoom = canvasConfig.zoom,
offset = canvasConfig.offset;
context.clearRect(0, 0, canvas.width, canvas.height);
context.drawImage(img, 0, 0, img.width, img.height, offset.x, offset.y, size.width * zoom, size.height * zoom);
//绘制挖洞后的蒙版
context.save();
context.beginPath();
pathRect(context, config.x, config.y, pickerSize, pickerSize);
context.rect(0, 0, canvas.width, canvas.height);
context.closePath();
context.fillStyle = "rgba(255,255,255,0.9)";
context.fill();
context.restore();
//绘制结果
var imageData = context.getImageData(config.x, config.y, pickerSize, pickerSize)
resCanvas.forEach(function (resContext, i) {
resContext.clearRect(0, 0, resSize[i], resSize[i]);
resContext.drawImage(canvas, config.x, config.y, pickerSize, pickerSize, 0, 0, resSize[i], resSize[i]);
//添加滤镜效果
if (canvasConfig.filter) {
var imageData = resContext.getImageData(0, 0, resSize[i], resSize[i]);
var temp = resContext.getImageData(0, 0, resSize[i], resSize[i]);// 有的滤镜实现需要temp数据
canvasConfig.filter(imageData.data, temp);
resContext.putImageData(imageData, 0, 0, 0, 0, resSize[i], resSize[i]);
}
});
}
//逆时针用路径自己来绘制矩形,这样可以控制方向,以便挖洞
// 起点x,起点y,宽度,高度
function pathRect(context, x, y, width, height) {
context.moveTo(x, y);
context.lineTo(x, y + height);
context.lineTo(x + width, y + height);
context.lineTo(x + width, y);
context.lineTo(x, y);
}
// 根据图片和canvas的尺寸,确定图片显示在canvas中的尺寸
function getFixedSize(img, canvas) {
var cancasRate = canvas.width / canvas.height,
imgRate = img.width / img.height, width = img.width, height = img.height;
if (cancasRate >= imgRate && img.height > canvas.height) {
height = canvas.height;
width = imgRate * height;
}
else if (cancasRate < imgRate && img.width > canvas.width) {
width = canvas.width;
height = width / imgRate;
}
return { width: width, height: height };
}
});
一般处理程序代码:
<%@ WebHandler Language="C#" Class="Handler" %> using System;
using System.Web;
using System.IO; public class Handler : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
//接收图片文件
HttpPostedFile file = context.Request.Files["userlogo"];
//设置图片保存路径
string savePath = context.Server.MapPath("~/image/");
//拼写图片名字
string _imageName = DateTime.Now.ToString("yyyyMMddhhmmss") + Path.GetFileName(file.FileName);
//保存
string imageUrl = savePath +_imageName;
string _imageExt = Path.GetExtension(file.FileName).ToLower();
file.SaveAs(imageUrl);
string result = "success";
context.Response.Write(result);
} public bool IsReusable
{
get
{
return false;
}
} }
ajax头像上传的更多相关文章
- 强大的flash头像上传插件(支持旋转、拖拽、剪裁、生成缩略图等)
今天介绍的这款flash上传头像功能非常强大,支持php,asp,jsp,asp.net 调用 头像剪裁,预览组件插件. 本组件需要安装Flash Player后才可使用,请从http://dl.pc ...
- 【Bootstrap-插件使用】Jcrop+fileinput组合实现头像上传功能
作者:Dreawer链接:https://zhuanlan.zhihu.com/p/24465742来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 作者:梦游的龙猫(转 ...
- [Bootstrap-插件使用]Jcrop+fileinput组合实现头像上传功能
很久没有更新博客了,再不写点东西都烂了. 这次更新一个小内容,是两个插件的组合使用,实现头像上传功能. 业务需求: 头像上传功能,要对上传的文件进行剪切,且保证头像到服务器时必须是正方形的. 优化&l ...
- Html5实现头像上传和编辑,保存为Base64的图片过程
一.Html5实现头像上传和编辑 插件地址: html5手机端裁剪图片上传头像代码 本地项目引入注意事项: 1.将html的js搬到外面的js文件中,便于管理 2.图片样式在html都是在页面写死,需 ...
- php头像上传预览
php头像上传带预览: 说道上传图片,大家并不陌生,不过,在以后开发的项目中,可能并不会让你使用提交刷新页面式的上传图片,比如上传头像,按照常理,肯定是在相册选择照片之后,确认上传,而肯定不会通过fo ...
- Django实现注册页面_头像上传
Django实现注册页面_头像上传 Django实现注册页面_头像上传 1.urls.py 配置路由 from django.conf.urls import url from django.cont ...
- springmvc+ajax文件上传
环境:JDK6以上,这里我是用JDK8,mysql57,maven项目 框架环境:spring+springmvc+mybaits或spring+springmvc+mybatis plus 前端代码 ...
- spring--mvc添加用户及用户头像上传
spring--mvc添加用户及用户头像上传 添加用户步骤: 1.用ajax获取省份信息 2.添加用户 代码:register.jsp <meta http-equiv="Conten ...
- jquery头像上传剪裁插件cropper的前后台demo
因为一个项目要做一个头像上传的功能,因此选择了使用jquery的头像插件cropper,cropper是一款使用简单且功能强大的图片剪裁jQuery插件,但是在使用的时候,有一个很大的坑需要注意,那就 ...
随机推荐
- Python之路,day2-Python基础1
python2 range(20) for i in range(10): print(i) range(1,10) ----->从1开始到9 else: #如果for循环正常结束, 就执行 ...
- 知识积累:关于Memory
仅清除页面缓存(PageCache)# sync; echo 1 > /proc/sys/vm/drop_caches清除目录项和inode# sync; echo 2 > /proc/s ...
- Redis客户端之Spring整合Jedis,ShardedJedisPool集群配置
Jedis设计 Jedis作为推荐的java语言redis客户端,其抽象封装为三部分: 对象池设计:Pool,JedisPool,GenericObjectPool,BasePoolableObjec ...
- Centos6.5下安装protobuf及简单使用
1.protobuf是google公司提出的数据存储格式,详细介绍可以参考:https://code.google.com/p/protobuf/ 2.下载最新的protobuf,下载地址:https ...
- 基于SVG的web页面图形绘制API介绍
转自:http://blog.csdn.net/jia20003/article/details/9185449 一:什么是SVG SVG是1999由W3C发布的2D图形描述语言,纯基于XML格式的标 ...
- IntelliJ IDEA 环境配置
0. 下载 jdk 用于 java developer kit 下载地址 见 rj.baidu.com 1. 从百度 网盘下载 ideaIU-2016.2.5.exe 并安装在window上 2. ...
- 在linux下python爬虫进程发生异常时自动重启直至正常结束的方法
之前在做爬虫的时候遇到一种情况,当网络情况不太好的时候,爬虫爬到的链接在urlopen时会因为无法正常连接而报URLError或者timeout的错误导致陈序报错而终止:但是这些错误在重新运行陈序后能 ...
- Code Igniter + PHP5.3 + SqlServer2008配置
1.配置apache+php5.3 2.配置sql server服务器,并允许远程连接. 3.去http://www.microsoft.com/en-us/download/details.aspx ...
- startssl
Validation Success You have successfully authenticated domain "xxx.com.cn".You will be abl ...
- XE6移动开发环境搭建之IOS篇(5):解决Windows和虚拟机下Mac OSX的共享问题(有图有真相)
网上能找到的关于Delphi XE系列的移动开发环境的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 原创作品,请尊重作者劳动成果,转载请注明出处!!! 在安装XE6 PAS ...