ajax と php を使用して画像を保存するスニペットを作成しました。
ここにコードがあります、
アヤックス:
$jq("#up").click(function() {
var canvasData = upcan.toDataURL("image/png");
var ajax = new XMLHttpRequest();
ajax.open("POST",'testsave.php',false);
ajax.setRequestHeader('Content-Type', 'application/upload');
ajax.send(canvasData);
});
クリックしたボタンはこちらです。canvasData には、イメージ形式のキャンバス コンテキスト データがあります。
Php:
<?php
if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
// Get the data
$imageData=$GLOBALS['HTTP_RAW_POST_DATA'];
// Remove the headers (data:,) part.
// A real application should use them according to needs such as to check image type
$filteredData=substr($imageData, strpos($imageData, ",")+1);
// Need to decode before saving since the data we received is already base64 encoded
$unencodedData=base64_decode($filteredData);
//echo "unencodedData".$unencodedData;
$random_digit=rand(0000,9999);
// Save file. This example uses a hard coded filename for testing,
// but a real application can specify filename in POST variable
$fp = fopen( 'canvas/canvas'.$random_digit.'.png', 'wb' );
fwrite( $fp, $unencodedData);
fclose( $fp );
}
?>
画像は正常に保存されました。保存した画像の名前をajaxで取得して、変数に保存してさらに使用できるようにしたいと考えています。では、どうすればそれを行うことができますか?