Unity通过Php上传图片分享

unity3D 专栏收录该内容
73 篇文章 0 订阅

1.Unity通过截屏保存图片,同时将图片的二进制数据(byte[] bytes = tex.EncodeToPNG();)Post给PHP

using UnityEngine;
using System.Collections;

public class SavePicture : MonoBehaviour {
	string url = "http://localhost/UpLoad/UnityUpload.php";
	string path;
	
	public   Material   image;
	void Start()
	{
		path=Application.dataPath +"/wukuaTurret.jpg";
	}
	void OnGUI()
	{
		if(GUI.Button(new Rect(100,100,100,100),"SavePic"))
		{
			Debug.Log(path);
			StartCoroutine(getTexture2d());
		}
	}

	IEnumerator getTexture2d()  { 
		
		yield return new WaitForEndOfFrame(); 
		
		int width = Screen.width;
		int height = Screen.height;
		Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
		tex.ReadPixels(new Rect(0, 0, width, height), 0, 0,false);
		tex.Apply();
		byte[] bytes = tex.EncodeToPNG();

		WWWForm form = new WWWForm ();
		form.AddField("Name","pic1");
		form.AddBinaryData ("post", bytes);
		WWW www = new WWW (url,form);
		StartCoroutine (PostData (www));
		Destroy(tex);
		
		
		
		System.IO.File.WriteAllBytes(path, bytes); 
		
	} 
	IEnumerator PostData(WWW www)
	{
		yield return www;
		Debug.Log(www.text);
	}
}

UnityUpload.php 把接收到的二进制数据保存成png文件,并打开temp.html,将图片的路径写入文件中另存为dest_page.html。

运行dest_page.html的效果如下

送上代码:
UnityUpload.php
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<?php
function Create($myFile)
{
	header('content-type:text/html; charset=utf-8');//防止生成的页面乱码
	
	$title ="src=".$myFile; //定义变量
	echo $title;
	$temp_file = "temp.html"; //临时文件,也可以是模板文件
	$dest_file = "dest_page.html"; //生成的目标页面
	
	$fp = fopen($temp_file, "r"); //只读打开模板
	$str = fread($fp, filesize($temp_file));//读取模板中内容
	
	$str = str_replace("[src=]", $title, $str);//替换内容
	fclose($fp);
	
	$handle = fopen($dest_file, "w"); //写入方式打开需要写入的文件
	fwrite($handle, $str); //把刚才替换的内容写进生成的HTML文件
	fclose($handle);//关闭打开的文件,释放文件指针和相关的缓冲
}
?>
<?php
 
$myFile = $_FILES["post"]["tmp_name"];
$content = '';
$fh = fopen($myFile, 'r') or die("can't open file");
while (!feof($fh)) {
    $content .= fgets($fh);//filesize($myFile)) or die('can\'t read');
}
fclose($fh);
 
 //文件存储路径
$file_path="upload/";
 if(is_dir($file_path)!=TRUE) mkdir($file_path,0664) ;
$myFile = $file_path.$_REQUEST['Name'].".png";
$fh = fopen($myFile, 'w') or die("can't open file");
//$stringData = $_FILES["fileUpload"];
$stringData = $content;//"START:\n" . join(',\n',headerCustom()) . ' \END';
fwrite($fh, $stringData);
fclose($fh);
echo $myFile;
Create($myFile);
?>
<body>
</body>
</html>

temp.html
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{penglig_site_title}</title>
</head>
<p>
<img [src=] width="800" height="1000">
</p>
<div class="bdsharebuttonbox" data-tag="share_1">
	<a class="bds_mshare" data-cmd="mshare"></a>
	<a class="bds_qzone" data-cmd="qzone" href="#"></a>
	<a class="bds_tsina" data-cmd="tsina"></a>
	<a class="bds_baidu" data-cmd="baidu"></a>
	<a class="bds_renren" data-cmd="renren"></a>
	<a class="bds_tqq" data-cmd="tqq"></a>
	<a class="bds_more" data-cmd="more">更多</a>
	<a class="bds_count" data-cmd="count"></a>
</div>
<script>
	window._bd_share_config = {
		common : {
			bdText : 'baidu一键分享',	
			bdDesc : '自定义分享摘要',	
			bdUrl : 'http://www.penglig.com/post-250.html', 	
			bdPic : 'http://ww2.sinaimg.cn/thumbnail/6da9c426jw1eg3f4z1qegj203k02odfn.jpg'
		},
		share : [{
			"bdSize" : 32
		}],
		
		
	}
	with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?cdnversion='+~(-new Date()/36e5)];
</script>

<body>

</body>
</html>


附件:http://download.csdn.net/download/he_wen_jian/8218639#6874737-tsina-1-86200-3048ae643c93bac2c9a415075e9789bc

  • 1
    点赞
  • 1
    评论
  • 2
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值