PHP 网站照片截图

时间:2022-01-18 15:21:43
专家 大侠们:
 我最近用PHP 在从事一个交友网站的开发
  
 我遇到了一个很棘手的问题

  我的网站要求会员能够上传照片
  且可以选择一张作为形象照(头像)
  
问题:被选择做为形象照的照片必须截取其中的一部分(比如只截取照片头部)做为头像
     前台怎么实现,后台怎么用PHP 实现 存在到MYSQL里面  

10 个解决方案

#1


<input type="file" name="myphoto" /> 自动找你照片本地路径
$mysqlphoto=addslashes(fread(fopen($myphoto,"r"),$myphotosize)); 把这个放到数据库的字段中

对于截取其中一部分,我不清楚了

#2


截取函数也很简单.
js部设置个层,获取截取部分的坐标.
将坐标提交到后台.
用gd库来处理.
然后保存到mysql数据库.

#3


同意楼上的,前台用javascript 获得坐标的四个点,在后台提交的时候根据四个点来用gd截取图片

#4


比较有难度,我看DZ,是FLASH做的

#5


关键是如何分析头部所在的位子,要很智能还是很难

#6


需要掌握 人脸识别技术的..

目前一部分网站是有这种后台技术的..

可以去买...

#7


2 楼的兄弟
能否
  给下具体的js 代码(最好后台PHP也给下)
  
谢谢拉
  
就是前台的JS 我不会写
 我给你再加份

#8


来学习

#9


自己整合一下吧,呵呵,直接拿来是不能用的,你得修改一下.可以结帖了.

CSDN的前台部分,用的是prototype

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
裁减头像 - CSDN个人空间
</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><meta http-equiv="Content-Language" content="zh-CN" /> 
<script src="js/prototype.js" type="text/javascript"></script>
  <script src="js/scriptaculous.js?load=builder,effects,dragdrop" type="text/javascript"></script>
<script src="js/ImageCropper/cropper.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8">

var x1;
var y1;
var width;
var height;

document.onkeydown = function(e) {
    if (e ? e.which : event.keyCode == 13) {
        crop();
    }


function onEndCrop( coords, dimensions ) {
x1 = coords.x1;
y1 = coords.y1;
width = dimensions.width;
height = dimensions.height;
}

Event.observe( 
window, 
'load', 
function() { 
new Cropper.ImgWithPreview( 
'tempPicture',

minWidth: 100, 
minHeight: 100,
ratioDim: { x: 100, y: 100 },
displayOnInit: true, 
onEndCrop: onEndCrop,
previewWrap: 'previewArea'



);

function closeWindow()
{
    window.opener.openWindowUnload();
    window.close();
}

function crop()
{
    if (!$('cropButton').disabled)
    {
        $('cropButton').disabled = true;
        $('cropButton').value = '正在裁减图片,请稍候...';
    
        var opt={
        method:'post',
        parameters:{x1: x1, y1: y1, width: width, height: height},
                    onSuccess: function(){ closeWindow(); },
                    onFailure: function() {
                        alert('裁减图片错误');
                $('cropButton').value = '裁减';
                $('cropButton').disabled = false;
                    }
        }
        
        new Ajax.Request('CropPicture.aspx', opt);
    }
}

function resize()
{
    var w = $('tempPicture').offsetWidth + 170;
    var h = $('tempPicture').offsetHeight + 173;
            var x = (screen.width-w)/2;
            var y = (screen.height-h)/2;
    self.moveTo(x, y);
    self.resizeTo(w, h);
}


</script>
<style type="text/css">
        *{margin:0;padding:0;}
        .exprt_noticeboxstysmall{background:url(images/croppicture_bg.gif) repeat-x;font:normal 12px verdana,sans-serif;margin:5px;border:1px solid #f60;}
        .exprt_noticeboxstysmall h3{font-size:14px;color:#FFF;line-height:28px;text-indent:0.5em;}
        .exprt_noticeboxstysmall h4{font-size:12px;color:#000;padding:0.5em 0.5em 0;}
        .exprt_noticeboxstysmall p{color:#f00;padding:0.5em;}
        .exprt_noticeboxstysmall input{border:1px solid #000;margin:0 6px 0 0;font:normal 12px Tahoma,sans-serif;}

#image {
float: left;
margin: 10px 0 10px 10px;
display:inline;
}

#previewArea {
margin: 20px 0 0 20px;
float: left;
}

#buttons {
clear: both;
    margin-left: 10px;
    margin-bottom: 10px;
}
</style>
</head>
<body onload="javascript:resize();">
    <form name="form1" method="post" action="CropPicture.aspx?t=3084" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMjAxNTc3MDgzOWRkKY8ohS2j+S5LF8cBa/1DEh76fjk=" />
</div>

    <div class="exprt_noticeboxstysmall">
        <h3>裁减头像</h3>
    <div id="image">
    <img src="ShowTempPicture.aspx?t=1133" alt="" id="tempPicture" />
    </div>
    <div id="previewArea"></div>
    <div id="buttons">
        <input id="cropButton" type="button" value="裁减" onclick="javascript:crop();" />
        <input type="button" value="取消" onclick="javascript:window.close();" />
    </div>
        
    </div>
    </form>
</body>
</html>


后台部分就是用gd库操作

header("Content-type: image/jpeg");
$imgName = $_GET['image'];
$startingW = $_GET['startingW'];
$width = $_GET['width'];
$startingH = $_GET['startingH'];
$height = $_GET['height'];
$imgName = "test.jpg";
$startingW = 300;
$startingH = 300;
$width= 400;
$height = 400;
$im = imagecreatefromjpeg($imgName);
$im1 = imagecreatetruecolor($width, $height);
//imagecopy($im1, $im, 0, 0, $startingW, $startingH, $width, $height);
imagecopyresampled($im1, $im, 0, 0, $startingW, $startingH, $width, $height, $width, $height);
imagejpeg($im1);
imagedestroy($im);
imagedestroy($im1);

#10


http://download.csdn.net/source/724007
给你一个完整的吧,这是使用JavaScript和php完成的,是我根据cloudgamer 改写的php后台切割图片。是采用的php完成的。 http://download.csdn.net/source/724007

#1


<input type="file" name="myphoto" /> 自动找你照片本地路径
$mysqlphoto=addslashes(fread(fopen($myphoto,"r"),$myphotosize)); 把这个放到数据库的字段中

对于截取其中一部分,我不清楚了

#2


截取函数也很简单.
js部设置个层,获取截取部分的坐标.
将坐标提交到后台.
用gd库来处理.
然后保存到mysql数据库.

#3


同意楼上的,前台用javascript 获得坐标的四个点,在后台提交的时候根据四个点来用gd截取图片

#4


比较有难度,我看DZ,是FLASH做的

#5


关键是如何分析头部所在的位子,要很智能还是很难

#6


需要掌握 人脸识别技术的..

目前一部分网站是有这种后台技术的..

可以去买...

#7


2 楼的兄弟
能否
  给下具体的js 代码(最好后台PHP也给下)
  
谢谢拉
  
就是前台的JS 我不会写
 我给你再加份

#8


来学习

#9


自己整合一下吧,呵呵,直接拿来是不能用的,你得修改一下.可以结帖了.

CSDN的前台部分,用的是prototype

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
裁减头像 - CSDN个人空间
</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><meta http-equiv="Content-Language" content="zh-CN" /> 
<script src="js/prototype.js" type="text/javascript"></script>
  <script src="js/scriptaculous.js?load=builder,effects,dragdrop" type="text/javascript"></script>
<script src="js/ImageCropper/cropper.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8">

var x1;
var y1;
var width;
var height;

document.onkeydown = function(e) {
    if (e ? e.which : event.keyCode == 13) {
        crop();
    }


function onEndCrop( coords, dimensions ) {
x1 = coords.x1;
y1 = coords.y1;
width = dimensions.width;
height = dimensions.height;
}

Event.observe( 
window, 
'load', 
function() { 
new Cropper.ImgWithPreview( 
'tempPicture',

minWidth: 100, 
minHeight: 100,
ratioDim: { x: 100, y: 100 },
displayOnInit: true, 
onEndCrop: onEndCrop,
previewWrap: 'previewArea'



);

function closeWindow()
{
    window.opener.openWindowUnload();
    window.close();
}

function crop()
{
    if (!$('cropButton').disabled)
    {
        $('cropButton').disabled = true;
        $('cropButton').value = '正在裁减图片,请稍候...';
    
        var opt={
        method:'post',
        parameters:{x1: x1, y1: y1, width: width, height: height},
                    onSuccess: function(){ closeWindow(); },
                    onFailure: function() {
                        alert('裁减图片错误');
                $('cropButton').value = '裁减';
                $('cropButton').disabled = false;
                    }
        }
        
        new Ajax.Request('CropPicture.aspx', opt);
    }
}

function resize()
{
    var w = $('tempPicture').offsetWidth + 170;
    var h = $('tempPicture').offsetHeight + 173;
            var x = (screen.width-w)/2;
            var y = (screen.height-h)/2;
    self.moveTo(x, y);
    self.resizeTo(w, h);
}


</script>
<style type="text/css">
        *{margin:0;padding:0;}
        .exprt_noticeboxstysmall{background:url(images/croppicture_bg.gif) repeat-x;font:normal 12px verdana,sans-serif;margin:5px;border:1px solid #f60;}
        .exprt_noticeboxstysmall h3{font-size:14px;color:#FFF;line-height:28px;text-indent:0.5em;}
        .exprt_noticeboxstysmall h4{font-size:12px;color:#000;padding:0.5em 0.5em 0;}
        .exprt_noticeboxstysmall p{color:#f00;padding:0.5em;}
        .exprt_noticeboxstysmall input{border:1px solid #000;margin:0 6px 0 0;font:normal 12px Tahoma,sans-serif;}

#image {
float: left;
margin: 10px 0 10px 10px;
display:inline;
}

#previewArea {
margin: 20px 0 0 20px;
float: left;
}

#buttons {
clear: both;
    margin-left: 10px;
    margin-bottom: 10px;
}
</style>
</head>
<body onload="javascript:resize();">
    <form name="form1" method="post" action="CropPicture.aspx?t=3084" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMjAxNTc3MDgzOWRkKY8ohS2j+S5LF8cBa/1DEh76fjk=" />
</div>

    <div class="exprt_noticeboxstysmall">
        <h3>裁减头像</h3>
    <div id="image">
    <img src="ShowTempPicture.aspx?t=1133" alt="" id="tempPicture" />
    </div>
    <div id="previewArea"></div>
    <div id="buttons">
        <input id="cropButton" type="button" value="裁减" onclick="javascript:crop();" />
        <input type="button" value="取消" onclick="javascript:window.close();" />
    </div>
        
    </div>
    </form>
</body>
</html>


后台部分就是用gd库操作

header("Content-type: image/jpeg");
$imgName = $_GET['image'];
$startingW = $_GET['startingW'];
$width = $_GET['width'];
$startingH = $_GET['startingH'];
$height = $_GET['height'];
$imgName = "test.jpg";
$startingW = 300;
$startingH = 300;
$width= 400;
$height = 400;
$im = imagecreatefromjpeg($imgName);
$im1 = imagecreatetruecolor($width, $height);
//imagecopy($im1, $im, 0, 0, $startingW, $startingH, $width, $height);
imagecopyresampled($im1, $im, 0, 0, $startingW, $startingH, $width, $height, $width, $height);
imagejpeg($im1);
imagedestroy($im);
imagedestroy($im1);

#10


http://download.csdn.net/source/724007
给你一个完整的吧,这是使用JavaScript和php完成的,是我根据cloudgamer 改写的php后台切割图片。是采用的php完成的。 http://download.csdn.net/source/724007