久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 ajax 和 php 將畫布上下文作為圖像上傳

uploading canvas context as image using ajax and php(使用 ajax 和 php 將畫布上下文作為圖像上傳)
本文介紹了使用 ajax 和 php 將畫布上下文作為圖像上傳的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個畫布,我想使用 ajax 和 php 將畫布上下文上傳到服務器.我希望最終輸出是存儲在服務器上的圖像.我已經使用表單完成了圖像上傳.但是現在我想讓畫布上下文將其轉換為圖像并上傳到服務器!

I have a canvas and I want to upload the canvas context to the server using ajax and php. I want the final output to be an image stored on the server. I have done image uploading using form. But now I want to get the canvas context convert it to image and upload to the server!

那么,我該怎么做?任何建議、算法或解決方案表示贊賞!

So, how can i do that? Any suggestions, algos or solutions are appreciated!

推薦答案

這篇博文恰當地描述了使用 AJAX 查詢將畫布保存到服務器上的方法,我想這應該適合您.

This blog post aptly describes the method of saving canvases onto the server with AJAX queries, I guess this should be fitting for you.

基本上,您需要一個 var canvasData = testCanvas.toDataURL("image/png"); 以在 JavaScript 中檢索畫布的內容.這將是一個 Base64 編碼的字符串,如下所示:data:image/png;base64,fooooooooooobaaaaaaaaaaar==.

Basically, you will need a var canvasData = testCanvas.toDataURL("image/png"); to retrieve the canvas' contents in JavaScript. This will be a Base64 encoded string, something like this: data:image/png;base64,fooooooooooobaaaaaaaaaaar==.

以下代碼將確保 AJAX 查詢將內容發送到 HTML:

The following code will make sure the AJAX query sends the contents to the HTML:

var ajax = new XMLHttpRequest();
ajax.open("POST",'testSave.php',false);
ajax.setRequestHeader('Content-Type', 'application/upload');
ajax.send(canvasData);

在服務器上,在 PHP 腳本中,您將在 $GLOBALS 數組中有一個名為 HTTP_RAW_POST_DATA 的鍵,這將包含我們剛剛獲取的數據.

On the server, in the PHP script, you will have a key named HTTP_RAW_POST_DATA in the $GLOBALS array, this will contain the data we just fetched.

// Remove the headers (data:,) part.
$filteredData=substr($GLOBALS['HTTP_RAW_POST_DATA'], strpos($GLOBALS['HTTP_RAW_POST_DATA'], ",")+1);

// Need to decode before saving since the data we received is already base64 encoded
$decodedData=base64_decode($filteredData);

$fp = fopen( 'test.png', 'wb' );
fwrite( $fp, $decodedData);
fclose( $fp );

當然,test.png 是您要保存的文件名.第一行需要去掉編碼圖像的data:image/png;base64,部分,以便以后可以通過base64_decode().它的輸出 ($decodedData) 將保存到文件中.

Of course, test.png is the filename you will save. The first line is required to remove the data:image/png;base64, part of the encoded image, so that it can later be decoded by base64_decode(). It's output ($decodedData) will be saved to the file.

這篇關于使用 ajax 和 php 將畫布上下文作為圖像上傳的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Joining 2 tables in SELECT(MYSQL/PHP)(在 SELECT(MYSQL/PHP) 中加入 2 個表)
How to make lt;option selected=quot;selectedquot;gt; set by MySQL and PHP?(如何使lt;option selected=“selectedgt;由 MySQL 和 PHP 設置?)
Auto populate a select box using an array in PHP(使用 PHP 中的數組自動填充選擇框)
PHP SQL SELECT where like search item with multiple words(PHP SQL SELECT where like search item with multiple words)
json_encode produce JSON_ERROR_UTF8 from MSSQL-SELECT(json_encode 從 MSSQL-SELECT 產生 JSON_ERROR_UTF8)
MySQL ORDER BY rand(), name ASC(MySQL ORDER BY rand(),名稱 ASC)
主站蜘蛛池模板: 干狠狠| 中文久久 | 亚洲精品国产一区 | av日韩一区 | 久草在线 | 亚洲一区免费在线 | 亚洲精品女人久久久 | 狠狠色网 | 国产一区二区三区不卡av | 久久久女 | 国产欧美精品区一区二区三区 | 羞羞视频免费在线 | 精品国产乱码久久久久久88av | 精品久久久久久亚洲综合网 | 免费观看成人性生生活片 | 欧美亚洲一区二区三区 | av在线免费观看网站 | 亚洲欧美综合精品久久成人 | 在线一区 | 欧美日韩一区二区三区视频 | 国产在线视频一区 | 亚洲午夜av久久乱码 | 自拍偷拍精品 | 蜜桃传媒av | 一区二区成人在线 | 国产91久久久久久久免费 | 一二区电影| 伊人春色成人网 | av一级在线观看 | 最新中文字幕 | 亚洲人成人一区二区在线观看 | 天天拍天天操 | 亚洲精品九九 | 久色 | 欧美伊人 | 九色视频网 | 亚洲成人网在线播放 | 四虎网站在线观看 | 成人免费福利 | 亚洲国产精品久久 | 亚洲第一av|