怎么在人物照片上 添加一个动物头像遮住原来人物头像

peitali 2014-12-01 03:29:12
涉及到肖像权的问题,要让用户上传自己照片的时候, 在自己的面部 选择一个动物头像 遮住自己 ,然后合成一张图片, 效果就像APP animal face 那种,
请问怎么实现,求思路,有demo 最好 js html5 都可以,小弟感激不尽
...全文
4557 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
peitali 2014-12-08
  • 打赏
  • 举报
回复
引用 2 楼 liuxingfffff 的回复:
写了个大概的demo。细节 你自己调试吧 思路:背景使用人物图片,头像是动物图片,动物图片是可以拖动的。 这部分功能用js+css来实现的。 上传给后台的数据是2张图片 还有动物头像图片相对于人物图片垂直距离 和 水平距离。 再把这2张图片 合成一张图片。

<!DOCTYPE html>
<html>
<head>
<style>
*{margin:0;padding:0;}
</style>
</head>
<body>
	<div style="display:inline-block;position:relative;">
		<img src="img.jpg" border="0"><div id="view" style="position:absolute;left:0;top:0;display:inline-block;"><img src="2.jpg" border="0" ></div></div>
	<form action="data.ashx" method="post" enctype="multipart/form-data" target="_blank">
        <input type="hidden" name="t" id="t" />
        <input type="hidden" name="l" id="l" />
        <input type="submit" value="提交" id="btnSubmit" />
    </form>
    <script>
        (function () {
            var t = document.getElementById("t");
            var l = document.getElementById("l");
            var head = document.getElementById("view");
            var img_width = 0;
            var img_height = 0;
            var position_x = 0;
            var position_y = 0;
            var btnSubmit = document.getElementById("btnSubmit");

            btnSubmit.onclick = function () {
                t.value = head.style.top.replace("px","");
                l.value = head.style.left.replace("px","");
                return true;
            }

            var img = head.getElementsByTagName("img")[0];
            img.onload = function () {
                img_width = img.width;
                img_height = img.height;
                head.style.width = img_width + "px";
                head.style.height = img_height + "px";
                img.style.display = "none";
                head.style.background = "url(" + img.attributes["src"].nodeValue + ") no-repeat ";
                head.style.backgroundSize = "100px 75px";
            }

            head.onmousedown = function (e) {
                var old_x = e.clientX;
                var old_y = e.clientY;
                head.style.cursor = "move";
                document.onmousemove = function (e) {
                    var x = e.clientX;
                    var y = e.clientY;

                    position_x += x - old_x;
                    position_y += y - old_y;

                    head.style.left = position_x + "px";
                    head.style.top = position_y + "px";

                    old_x = x;
                    old_y = y;
                }
                document.onmouseup = function (e) {
                    document.onmousemove = null;
                    document.onmouseup = null;
                    head.style.cursor = "default";
                }
            }
        })();
	</script>	
</body>
</html>

<%@ WebHandler Language="C#" Class="Data" %>
using System;
using System.Collections.Generic;
using System.Web;

public class Data : IHttpHandler
{
    public bool IsReusable
    {
        get { return false; }
    }

    protected HttpRequest Request;
    protected HttpResponse Response;
    protected HttpServerUtility Server;

    public void ProcessRequest(HttpContext context)
    {
        Request = context.Request;
        Response = context.Response;
        Server = context.Server;
        Response.ContentType = "text/plain";

        string avatorFilePath = Server.MapPath("img.jpg");
        string animalFilePath = Server.MapPath("2.jpg");

        int width = 0;
        int height = 0;
        int top = int.Parse(context.Request.Form["t"]);
        int left = int.Parse(context.Request.Form["l"]);
        
        using (System.Drawing.Image avator = System.Drawing.Image.FromFile(avatorFilePath, true))
        {
            width = avator.Width;
            height = avator.Height;
        }

        using (System.Drawing.Image pickedImage = new System.Drawing.Bitmap(width, height))
        {
            using (System.Drawing.Graphics pickedG = System.Drawing.Graphics.FromImage(pickedImage))
            {
                pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                //加载背景
                using (System.Drawing.Image avatorImage = System.Drawing.Image.FromFile(avatorFilePath, true))
                {
                    System.Drawing.Rectangle fromR = new System.Drawing.Rectangle(0, 0, width, height);
                    System.Drawing.Rectangle toR = new System.Drawing.Rectangle(0, 0, width, height);
                    pickedG.DrawImage(avatorImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
                }

                using (System.Drawing.Image animalImage = System.Drawing.Image.FromFile(animalFilePath, true))
                {
                    System.Drawing.Rectangle fromR = new System.Drawing.Rectangle(0, 0, animalImage.Width, animalImage.Height);
                    System.Drawing.Rectangle toR = new System.Drawing.Rectangle(left, top, animalImage.Width, animalImage.Height);
                    pickedG.DrawImage(animalImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
                }

                //关键质量控制
                //获取系统编码类型数组,包含了jpeg,bmp,png,gif,tiff
                System.Drawing.Imaging.ImageCodecInfo[] icis = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
                System.Drawing.Imaging.ImageCodecInfo ici = null;
                foreach (System.Drawing.Imaging.ImageCodecInfo i in icis)
                {
                    if (i.MimeType == "image/jpeg")
                    {
                        ici = i;
                    }
                }
                System.Drawing.Imaging.EncoderParameters ep = new System.Drawing.Imaging.EncoderParameters(1);
                ep.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)90);

                //保存缩略图
                byte[] bytes;
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    pickedImage.Save(ms, ici, ep);

                    bytes = ms.ToArray();

                    ms.Close();
                    ms.Dispose();
                }
                Response.OutputStream.Write(bytes, 0, bytes.Length);
            }
        }
        Response.CacheControl = "no-cache";
        Response.ContentType = "image/jpeg";
    }
}
http://bbs.csdn.net/topics/390944211?page=1#post-398641348 大神,能不能帮我看下 这个问题,这个功能很着急,我搞了好久还没搞好。。不胜感激
业余草 2014-12-02
  • 打赏
  • 举报
回复
引用 2 楼 liuxingfffff 的回复:
写了个大概的demo。细节 你自己调试吧 思路:背景使用人物图片,头像是动物图片,动物图片是可以拖动的。 这部分功能用js+css来实现的。 上传给后台的数据是2张图片 还有动物头像图片相对于人物图片垂直距离 和 水平距离。 再把这2张图片 合成一张图片。

<!DOCTYPE html>
<html>
<head>
<style>
*{margin:0;padding:0;}
</style>
</head>
<body>
	<div style="display:inline-block;position:relative;">
		<img src="img.jpg" border="0"><div id="view" style="position:absolute;left:0;top:0;display:inline-block;"><img src="2.jpg" border="0" ></div></div>
	<form action="data.ashx" method="post" enctype="multipart/form-data" target="_blank">
        <input type="hidden" name="t" id="t" />
        <input type="hidden" name="l" id="l" />
        <input type="submit" value="提交" id="btnSubmit" />
    </form>
    <script>
        (function () {
            var t = document.getElementById("t");
            var l = document.getElementById("l");
            var head = document.getElementById("view");
            var img_width = 0;
            var img_height = 0;
            var position_x = 0;
            var position_y = 0;
            var btnSubmit = document.getElementById("btnSubmit");

            btnSubmit.onclick = function () {
                t.value = head.style.top.replace("px","");
                l.value = head.style.left.replace("px","");
                return true;
            }

            var img = head.getElementsByTagName("img")[0];
            img.onload = function () {
                img_width = img.width;
                img_height = img.height;
                head.style.width = img_width + "px";
                head.style.height = img_height + "px";
                img.style.display = "none";
                head.style.background = "url(" + img.attributes["src"].nodeValue + ") no-repeat ";
                head.style.backgroundSize = "100px 75px";
            }

            head.onmousedown = function (e) {
                var old_x = e.clientX;
                var old_y = e.clientY;
                head.style.cursor = "move";
                document.onmousemove = function (e) {
                    var x = e.clientX;
                    var y = e.clientY;

                    position_x += x - old_x;
                    position_y += y - old_y;

                    head.style.left = position_x + "px";
                    head.style.top = position_y + "px";

                    old_x = x;
                    old_y = y;
                }
                document.onmouseup = function (e) {
                    document.onmousemove = null;
                    document.onmouseup = null;
                    head.style.cursor = "default";
                }
            }
        })();
	</script>	
</body>
</html>

<%@ WebHandler Language="C#" Class="Data" %>
using System;
using System.Collections.Generic;
using System.Web;

public class Data : IHttpHandler
{
    public bool IsReusable
    {
        get { return false; }
    }

    protected HttpRequest Request;
    protected HttpResponse Response;
    protected HttpServerUtility Server;

    public void ProcessRequest(HttpContext context)
    {
        Request = context.Request;
        Response = context.Response;
        Server = context.Server;
        Response.ContentType = "text/plain";

        string avatorFilePath = Server.MapPath("img.jpg");
        string animalFilePath = Server.MapPath("2.jpg");

        int width = 0;
        int height = 0;
        int top = int.Parse(context.Request.Form["t"]);
        int left = int.Parse(context.Request.Form["l"]);
        
        using (System.Drawing.Image avator = System.Drawing.Image.FromFile(avatorFilePath, true))
        {
            width = avator.Width;
            height = avator.Height;
        }

        using (System.Drawing.Image pickedImage = new System.Drawing.Bitmap(width, height))
        {
            using (System.Drawing.Graphics pickedG = System.Drawing.Graphics.FromImage(pickedImage))
            {
                pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                //加载背景
                using (System.Drawing.Image avatorImage = System.Drawing.Image.FromFile(avatorFilePath, true))
                {
                    System.Drawing.Rectangle fromR = new System.Drawing.Rectangle(0, 0, width, height);
                    System.Drawing.Rectangle toR = new System.Drawing.Rectangle(0, 0, width, height);
                    pickedG.DrawImage(avatorImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
                }

                using (System.Drawing.Image animalImage = System.Drawing.Image.FromFile(animalFilePath, true))
                {
                    System.Drawing.Rectangle fromR = new System.Drawing.Rectangle(0, 0, animalImage.Width, animalImage.Height);
                    System.Drawing.Rectangle toR = new System.Drawing.Rectangle(left, top, animalImage.Width, animalImage.Height);
                    pickedG.DrawImage(animalImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
                }

                //关键质量控制
                //获取系统编码类型数组,包含了jpeg,bmp,png,gif,tiff
                System.Drawing.Imaging.ImageCodecInfo[] icis = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
                System.Drawing.Imaging.ImageCodecInfo ici = null;
                foreach (System.Drawing.Imaging.ImageCodecInfo i in icis)
                {
                    if (i.MimeType == "image/jpeg")
                    {
                        ici = i;
                    }
                }
                System.Drawing.Imaging.EncoderParameters ep = new System.Drawing.Imaging.EncoderParameters(1);
                ep.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)90);

                //保存缩略图
                byte[] bytes;
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    pickedImage.Save(ms, ici, ep);

                    bytes = ms.ToArray();

                    ms.Close();
                    ms.Dispose();
                }
                Response.OutputStream.Write(bytes, 0, bytes.Length);
            }
        }
        Response.CacheControl = "no-cache";
        Response.ContentType = "image/jpeg";
    }
}
高手啊
peitali 2014-12-01
  • 打赏
  • 举报
回复
大侠,太感谢了,这么快就写了个demo,我研究研究
liuxingfffff 2014-12-01
  • 打赏
  • 举报
回复
写了个大概的demo。细节 你自己调试吧
思路:背景使用人物图片,头像是动物图片,动物图片是可以拖动的。
这部分功能用js+css来实现的。
上传给后台的数据是2张图片 还有动物头像图片相对于人物图片垂直距离 和 水平距离。
再把这2张图片 合成一张图片。



<!DOCTYPE html>
<html>
<head>
<style>
*{margin:0;padding:0;}
</style>
</head>
<body>
<div style="display:inline-block;position:relative;">
<img src="img.jpg" border="0"><div id="view" style="position:absolute;left:0;top:0;display:inline-block;"><img src="2.jpg" border="0" ></div></div>
<form action="data.ashx" method="post" enctype="multipart/form-data" target="_blank">
<input type="hidden" name="t" id="t" />
<input type="hidden" name="l" id="l" />
<input type="submit" value="提交" id="btnSubmit" />
</form>
<script>
(function () {
var t = document.getElementById("t");
var l = document.getElementById("l");
var head = document.getElementById("view");
var img_width = 0;
var img_height = 0;
var position_x = 0;
var position_y = 0;
var btnSubmit = document.getElementById("btnSubmit");

btnSubmit.onclick = function () {
t.value = head.style.top.replace("px","");
l.value = head.style.left.replace("px","");
return true;
}

var img = head.getElementsByTagName("img")[0];
img.onload = function () {
img_width = img.width;
img_height = img.height;
head.style.width = img_width + "px";
head.style.height = img_height + "px";
img.style.display = "none";
head.style.background = "url(" + img.attributes["src"].nodeValue + ") no-repeat ";
head.style.backgroundSize = "100px 75px";
}

head.onmousedown = function (e) {
var old_x = e.clientX;
var old_y = e.clientY;
head.style.cursor = "move";
document.onmousemove = function (e) {
var x = e.clientX;
var y = e.clientY;

position_x += x - old_x;
position_y += y - old_y;

head.style.left = position_x + "px";
head.style.top = position_y + "px";

old_x = x;
old_y = y;
}
document.onmouseup = function (e) {
document.onmousemove = null;
document.onmouseup = null;
head.style.cursor = "default";
}
}
})();
</script>
</body>
</html>



<%@ WebHandler Language="C#" Class="Data" %>
using System;
using System.Collections.Generic;
using System.Web;

public class Data : IHttpHandler
{
public bool IsReusable
{
get { return false; }
}

protected HttpRequest Request;
protected HttpResponse Response;
protected HttpServerUtility Server;

public void ProcessRequest(HttpContext context)
{
Request = context.Request;
Response = context.Response;
Server = context.Server;
Response.ContentType = "text/plain";

string avatorFilePath = Server.MapPath("img.jpg");
string animalFilePath = Server.MapPath("2.jpg");

int width = 0;
int height = 0;
int top = int.Parse(context.Request.Form["t"]);
int left = int.Parse(context.Request.Form["l"]);

using (System.Drawing.Image avator = System.Drawing.Image.FromFile(avatorFilePath, true))
{
width = avator.Width;
height = avator.Height;
}

using (System.Drawing.Image pickedImage = new System.Drawing.Bitmap(width, height))
{
using (System.Drawing.Graphics pickedG = System.Drawing.Graphics.FromImage(pickedImage))
{
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

//加载背景
using (System.Drawing.Image avatorImage = System.Drawing.Image.FromFile(avatorFilePath, true))
{
System.Drawing.Rectangle fromR = new System.Drawing.Rectangle(0, 0, width, height);
System.Drawing.Rectangle toR = new System.Drawing.Rectangle(0, 0, width, height);
pickedG.DrawImage(avatorImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
}

using (System.Drawing.Image animalImage = System.Drawing.Image.FromFile(animalFilePath, true))
{
System.Drawing.Rectangle fromR = new System.Drawing.Rectangle(0, 0, animalImage.Width, animalImage.Height);
System.Drawing.Rectangle toR = new System.Drawing.Rectangle(left, top, animalImage.Width, animalImage.Height);
pickedG.DrawImage(animalImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
}

//关键质量控制
//获取系统编码类型数组,包含了jpeg,bmp,png,gif,tiff
System.Drawing.Imaging.ImageCodecInfo[] icis = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
System.Drawing.Imaging.ImageCodecInfo ici = null;
foreach (System.Drawing.Imaging.ImageCodecInfo i in icis)
{
if (i.MimeType == "image/jpeg")
{
ici = i;
}
}
System.Drawing.Imaging.EncoderParameters ep = new System.Drawing.Imaging.EncoderParameters(1);
ep.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)90);

//保存缩略图
byte[] bytes;
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
pickedImage.Save(ms, ici, ep);

bytes = ms.ToArray();

ms.Close();
ms.Dispose();
}
Response.OutputStream.Write(bytes, 0, bytes.Length);
}
}
Response.CacheControl = "no-cache";
Response.ContentType = "image/jpeg";
}
}
peitali 2014-12-01
  • 打赏
  • 举报
回复
大神都去哪儿了 ,自己顶

39,083

社区成员

发帖
与我相关
我的任务
社区描述
HTML5是构建Web内容的一种语言描述方式。HTML5是互联网的下一代标准,是构建以及呈现互联网内容的一种语言方式.被认为是互联网的核心技术之一。
社区管理员
  • HTML5社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧