jquery 上传图片自由截取
为了使用户能自定义个人头像,需要提供一个对上传图片的截图功能,当前很多网站特别是SNS类网站都提供这样的功能,非常实用。本文主要是利用jQuery的imgAreaSelect插件实现。
首先引入三个文件:
<script src="<%:Url.Content("~/UI/Scripts/jquery-1.8..min.js") %>"></script>
<link href='<%:Url.Content("~/UI//CSS/imgareaselect-default.css") %>' rel="stylesheet" />
<script src='<%:Url.Content("~/UI/Scripts/jquery.imgareaselect.pack.js")%>'></script>
前段主要代码:初始化所选择截取的图片
$('#photo').imgAreaSelect({
aspectRatio: '1:1',
handles: true
, fadeSpeed: 200
, onSelectChange: preview
// , onSelectEnd: someFunction
});
设置所选区域大小值,与坐标:
function preview(img, selection) {
if (!selection.width || !selection.height)
return;
var scaleX = 100 / selection.width;
var scaleY = 100 / selection.height;
$('#left').val(selection.x1);
$('#top').val(selection.y1);
$('#w').val(selection.width);
$('#h').val(selection.height);
}
实现代码:前台
//上传图片
$("#File1").change(function () { $("#formSave").ajaxSubmit({
type: "POST",
url: "/Home/UpPic/",
dataType: "json",
success: function (data) {
if (data.msg == "OK") {
$("#photo").attr("src", data.path)
} else {
alert(data.msg);
}
}
});
}); //剪切后保存头像
$("#btnSaveImg").click(function () { if ($('#left').val() == "") {
alert("请先截取图片");
return;
} $("#formSave").ajaxSubmit({
type: "POST",
url: "/Home/SavePic/",
dataType: "json",
success: function (data) {
if (data.msg == "OK") {
$("#photo").attr("src", data.path)
alert("保存成功!");
} else {
alert(data.msg);
}
}
});
});
实现代码后台:
/// <summary>
/// 上传图片
/// </summary>
public void UpPic()
{
try
{
var file = Request.Files["File1"];
if (file.ContentLength == 0)
{
ReWrite("Error","请选择文件");
return;
}
if (file.ContentLength > 307200)
{
ReWrite("Error","文件过大");
return;
} int width = 0; int height = 0; using (Image originalImg = Image.FromFile(file.FileName))
{
double bi = originalImg.Width / originalImg.Height;
if (bi > 1.6)
{
width = 600;
height = (int)(600 / bi);
}
else
{
height = 360;
width = (int)(360 * bi);
}
} //w600 h360;
string extensionName = System.IO.Path.GetExtension(file.FileName).ToLower();
string fileName ="temp" + extensionName; string p = "/Images/" + fileName;
string path = Server.MapPath("~" + p);
// file.SaveAs(path);
Session["path"] = "~" + p;
CommonMethod.AutoMakeThumNail(file.FileName, path, width, height, PicThumNailModel.H);
ReWrite("OK", p);
}
catch (Exception ex)
{
ReWrite("Error",ex.Message);
return;
}
} public void SavePic()
{ string photo ="";
if (Session["path"] != null)
{
photo = Session["path"].ToString();
}
else
{
photo = "~/Images/20140430172226.png";
}
photo = Server.MapPath(photo);
using (Image originalImg = Image.FromFile(photo))
{ int imageWidth = originalImg.Width;
int imageHeight = originalImg.Height;
int cutTop = Int32.Parse(Request.Form["top"]);
int cutLeft = Int32.Parse(Request.Form["left"]);
int dropWidth = Int32.Parse(Request.Form["w"]);
int dropHeight = Int32.Parse(Request.Form["h"]);
ImageHelp imgHelp = new ImageHelp(); // string picPath = CommonMethod.GetConfig("HeadPicPath"); string extensionName = System.IO.Path.GetExtension(photo).ToLower();
string picName =DateTime.Now.ToString("yyyyMMddHHmmssff") + extensionName; string pp = "/Images/" + picName; imgHelp.GetPart(photo, Server.MapPath("/Images/"), 0, 0, dropWidth, dropHeight, cutLeft, cutTop, imageWidth, imageHeight, picName); ReWrite("OK", pp);
}
// DelPic(photo);
}
转载请注明出处:http://www.cnblogs.com/Xingsoft-555/
jquery 上传图片自由截取的更多相关文章
- jquery上传图片插件plupload
官方网站:http://plupload.com/ jquery.plupload.queue插件,是上传图片组件很强大的插件.plupload 前端根据浏览器不同选择使用Html5. Gears, ...
- jquery 上传图片转为base64,ajax提交到后台
支持多张图片上传.图片上传数量修改.可以删除 <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...
- jquery插件-自由拖拽
最近工作不是很忙,学习之余想整理一些代码出来,首先想到的就是是js拖拽. 两年前去某公司面试的时候,曾经被问过这个问题,如何在页面上拖放元素,尽管现在看起来很简单,但当时的我半点思路都没有,面试想当然 ...
- jquery 上传图片即时预览功能
<script type="text/javascript"> jQuery.fn.extend({ uploadPreview: ...
- jquery上传图片
http://www.cnblogs.com/wutao/archive/2010/01/28/1658496.html http://www.cnblogs.com/cloudgamer/archi ...
- jQuery 超过字符截取部分用星号表示
$(function(){ var str = $('#num').text(); if (str.length >15) { var strend = str.substring(4,str. ...
- ajax+jquery上传图片
利用ajax进行图片上传,啥也不说了,上代码~ <input type="file" id="uploadImg"> <span oncli ...
- javascript、jquery 、C#、sqlserveer、mysql、oracle中字符串截取的区别和用法
下标从0开始 ,并且包括起始位 javascript 中字符串截取 : substring(Number start,Number end) var substr = "liuguangfa ...
- jQuery常见面试题(转)
代码以jQuery 1.83 为例 一 :Q: What is the difference between .get(), [], and .eq()? A: eq返回原生jQuery对象,截取某些 ...
随机推荐
- laravel调试神器tinker
一直以来,想调试框架中的某些东西,如想知道 Elpquent 的 create 方法返回值是个什么东西, 以前的话,应该就是在 create 方法调用之后,使用 dd 或者 var_dump 之类的函 ...
- laravel5.1 关联模型保存的方法(使用associate方法)
模型定义 class User { public function customer() { return $this->hasOne('Customer'); } } class Custom ...
- 手脱ACProtect v1.35(有Stolen Code)
1.载入PEID ACProtect v1.35 -> risco software Inc. & Anticrack Soft 2.载入OD,需要注意的是,异常选项除了[内存访问异常] ...
- PowderDesign的使用
(一)PowderDesign的安装 powderDesign下面简称pd,安装的话在网上找到安装包,安装后破解就行了.打开如图: (二)sql导入 操作步骤:File----------->R ...
- 使用nginx做反向代理
很多同学喜欢用nginx做反向代理访问某些网站,原因大家都懂的,今天老高记录一下如何使用nginx做反向代理以及如何配置和优化nginx的反向代理. 准备工作 首先,你需要一个稳定的国外的便宜的VPS ...
- NOJ1659 求值 log10取对+floor
问题描述 给你三个数a,b,c,求a的b次的前c位数(不够c位输出全部即可) 输入 输入数据有多组,每组占一行,有三个整数,之间有空格.(0<a,b<2147483648,0<c ...
- Python学习笔记(十)匿名函数
摘抄自:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431843456 ...
- JVM调优总结(4):分代垃圾回收
为什么要分代 分代的垃圾回收策略,是基于这样一个事实:不同的对象的生命周期是不一样的.因此,不同生命周期的对象可以采取不同的收集方式,以便提高回收效率. 在Java程序运行的过程中,会产生大量的对象, ...
- Assert 的用法
Assert Assert是断言的意思,头文件为assert.h, assert是一个宏 功 能: 测试一个条件并可能使程序终止 用 法: void assert(int test); 在单元测试中经 ...
- 超酷动态图片展示墙JS特效制作方法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...