Unity通过Php上传图片分享
2014-12-03 00:02
218 查看
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
相关文章推荐
- PHP+MySQL微信通过jsapi分享到朋友圈等平台,实现设置图片、链接。
- PHP5.6通过CURL上传图片@符无效的兼容问题
- PHP 图片文件上传代码分享
- php上传图片存入数据库示例分享
- php+js实现异步图片上传实例分享
- php+js实现异步图片上传实例分享
- php 通过curl上传图片
- php+flash+jQuery多图片上传源码分享
- 分享:php 上传图片的代码
- PHP服务端如何通过程序将图上传到指定的图片服务器与图片服务器的优化方案
- 分享---php 上传图片的代码
- 使用localResizeIMG插件上传图片到七牛云返回链接通过PHP进行保存
- Nginx + php 通过 X-Accel-Redirect 防止上传图片嵌入脚本攻击方法
- PHP通过CURL上传图片(微信公众号上传素材)
- 如何通过表单上传图片数据,PHP后端获取并且保存文件数据
- PHP5.6通过CURL上传图片@符无效的兼容问题
- php+flash+jQuery多图片上传源码分享
- php上传图片存入数据库示例分享
- 分享-php 上传图片的代码
- PHP中多张图片上传并校验的实现