php接收base64資料生成圖片並儲存
阿新 • • 發佈:2021-08-30
public function base64(){ //接收base64資料 $image= $_POST['imegse']; //設定圖片名稱 $imageName = "25220_".date("His",time())."_".rand(1111,9999).'.png'; //判斷是否有逗號 如果有就擷取後半部分 if (strstr($image,",")){ $image = explode(',',$image); $image = $image[1]; } //設定圖片儲存路徑 $path = "./".date("Ymd",time()); //判斷目錄是否存在 不存在就建立 if (!is_dir($path)){ mkdir($path,0777,true); } //圖片路徑 $imageSrc= $path."/". $imageName; //生成資料夾和圖片 $r = file_put_contents($imageSrc, base64_decode($image)); if (!$r) { return json(['code'=>0,'message'=>'圖片生成失敗']); }else { return json(['code'=>1,'message'=>'圖片生成成功']); } }
需求:通過canvas.toDataURL();生成的圖片上傳至後臺。
來源https://www.cnblogs.com/junyi-bk/p/11742278.html