Unity通过Php上传图片分享

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
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Unity中,你可以使用以下步骤将照片上传到安卓设备: 1. 在Unity中打开你的项目,确保你已经设置了安卓构建平台。 2. 创建一个UI按钮或者其他触发上传事件的方式。 3. 在按钮的OnClick事件中,添加一个方法来处理照片上传。 4. 在上传方法中,你可以使用Unity的插件或者原生的Android API来实现照片上传。以下是一种可能的实现方式: a. 使用Unity的WWW类来上传照片。你可以使用WWWForm来创建一个表单,然后将照片附加到表单中,并使用WWW类的Post方法将表单发送到服务器。 ```csharp IEnumerator UploadPhoto(string url, Texture2D photo) { byte[] bytes = photo.EncodeToPNG(); WWWForm form = new WWWForm(); form.AddBinaryData("photo", bytes, "photo.png", "image/png"); WWW www = new WWW(url, form); yield return www; if (www.error == null) { Debug.Log("Photo uploaded successfully!"); } else { Debug.Log("Error uploading photo: " + www.error); } } ``` b. 如果你更倾向于使用原生的Android API,你可以在Unity中创建一个Java类,然后在该类中实现照片上传的功能。然后,通过Unity的SendMessage方法将上传结果传递回Unity。 ```java public class PhotoUploader { public static void uploadPhoto(String photoPath) { // 实现照片上传逻辑 // 将上传结果通过Unity的SendMessage方法传递回Unity } } ``` 在Unity中调用Java类的方法: ```csharp // 在上传方法中调用Java类的方法 AndroidJavaClass photoUploaderClass = new AndroidJavaClass("com.example.PhotoUploader"); photoUploaderClass.CallStatic("uploadPhoto", photoPath); ``` 这只是一种实现方式,具体的实现取决于你的需求和项目设置。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值