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);
}
}
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>