php將base64字符串轉換為圖片


昨天用一個js插件
[鏈接]: http://www.erdangjiade.com/js/910.html
實行了圖片裁剪並預覽,不過它生產的圖片資源是一個base64字符串,不好保存后來在網上找到利用php將base64字符串轉換為圖片並保存的方法:

                //獲取圖片base64字符串
                $imgBase64 = $_REQUEST['img'];

		if (preg_match('/^(data:\s*image\/(\w+);base64,)/',$imgBase64,$res)) {
             //獲取圖片類型	
                        $type = $res[2];

            //圖片保存路徑
			$new_file = "static/images/".date('Ymd',time()).'/';

			if (!file_exists($new_file)) {

				mkdir($new_file,0755,true);
			}

            //圖片名字
			$new_file = $new_file.time().'.'.$type;

			if (file_put_contents($new_file,base64_decode(str_replace($res[1],'', $imgBase64)))) {

				$msg['msg'] = 'ok';

				echo json_encode($msg);
			} else {

				$msg['msg'] = 'no';

				echo json_encode($msg);
			}
		}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM