PHP常用函數(shù)之base64圖片上傳功能詳解
本文實(shí)例講述了PHP常用函數(shù)之base64圖片上傳功能。分享給大家供大家參考,具體如下:
HTML頁(yè)面代碼:
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<img id="articleImg" width="180" height="100">
<input type="file" value="上傳" id="articleImgBtn" />
<script type="text/javascript" src = 'jquery-2.1.4.min.js'></script>
<script type="text/javascript">
$('#articleImgBtn').change(function(){
run(this, function (data) {
uploadImage(data);
});
});
function run(input_file, get_data) {
/*input_file:文件按鈕對(duì)象*/
/*get_data: 轉(zhuǎn)換成功后執(zhí)行的方法*/
if (typeof (FileReader) === 'undefined') {
alert("抱歉,你的瀏覽器不支持 FileReader,不能將圖片轉(zhuǎn)換為Base64,請(qǐng)使用現(xiàn)代瀏覽器操作!");
} else {
try {
/*圖片轉(zhuǎn)Base64 核心代碼*/
var file = input_file.files[0];
//這里我們判斷下類型如果不是圖片就返回 去掉就可以上傳任意文件
if (!/image\/\w+/.test(file.type)) {
alert("請(qǐng)確保文件為圖像類型");
return false;
}
var reader = new FileReader();
reader.onload = function () {
get_data(this.result);
}
reader.readAsDataURL(file);
} catch (e) {
alert('圖片轉(zhuǎn)Base64出錯(cuò)啦!' + e.toString())
}
}
}
function uploadImage(img) {
//判斷是否有選擇上傳文件
var imgPath = $("#articleImgBtn").val();
if (imgPath == "") {
alert("請(qǐng)選擇上傳圖片!");
return;
}
//判斷上傳文件的后綴名
var strExtension = imgPath.substr(imgPath.lastIndexOf('.') + 1);
if (strExtension != 'jpg' && strExtension != 'gif'
&& strExtension != 'png' && strExtension != 'bmp') {
alert("請(qǐng)選擇圖片文件");
return;
}
$.ajax({
type: "POST",
url: 'http://localhost/123.php',
// data: {file: img.substr(img.indexOf(',') + 1)}, //視情況將base64的前面字符串data:image/png;base64,刪除
data: {file: img}, //視情況將base64的前面字符串data:image/png;base64,刪除
cache: false,
success: function(data) {
var return_info = JSON.parse(data);
if(return_info.status){
$("#articleImg").attr('src', return_info.path);
alert("上傳成功");
}else{
alert(return_infoerr_info);
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("上傳失敗,請(qǐng)檢查網(wǎng)絡(luò)后重試");
}
});
}
</script>
</body>
</html>
PHP 處理代碼:
function upload_image($file_data){
$upload_result = array('status' => true, 'msg'=>'','err_info'=>'');
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $file_data, $result)) {
//處理base64字符串
$img_base64 = str_replace($result[1], '', $file_data);
$img_base64 = str_replace('=', '', $img_base64);
$source_img = base64_decode($img_base64);
//判斷文件大小
$file_size =
//上傳目錄
$basedir = './img_test';
//后綴
$img_suffix = $result[2];//文件后綴
//文件名
// $filename = uniqid();//文件名
$filename = date('YmdHis',time());//文件名
//文件完整路徑
$filepath = $basedir . "/" . $filename . "." . $img_suffix;
//目錄若果不存在,則創(chuàng)建目錄
if(!is_dir($basedir)){
mkdir($basedir);
chmod($basedir,0777);
}
//上傳文件
try {
file_put_contents($filepath, $img_base64);
$filepath = substr($filepath, 1);
$upload_result = array('status' => true, 'msg'=>'上傳成功','err_info'=>'','path'=>$filepath);
return $upload_result;
} catch (Exception $e) {
$upload_result = array('status' => false, 'msg'=>'上傳失敗','err_info'=>$e->getMessage());
return $upload_result;
}
// if (file_put_contents($filepath, base64_decode(str_replace($result[1], '', $file_data)))) {
// //$size = getimagesize($filepath);
// $filepath = substr($filepath, 1);
// //$arr['filepath'] = $filepath;
// //$arr['size'] = $size[3];
// return $filepath;
// }else{
// return false;
// }
}else{
$upload_result = array('status' => false, 'msg'=>'上傳失敗','err_info'=>'請(qǐng)攜帶base64字符串的前綴');
return $upload_result;
}
}
$res = upload_image($file_data);
echo json_encode($res);
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php文件操作總結(jié)》、《PHP目錄操作技巧匯總》、《PHP常用遍歷算法與技巧總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》及《PHP網(wǎng)絡(luò)編程技巧總結(jié)》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP保存Base64圖片base64_decode的問(wèn)題整理
- PHP實(shí)現(xiàn)將base64編碼字符串轉(zhuǎn)換成圖片示例
- php讀取和保存base64編碼的圖片內(nèi)容
- php實(shí)現(xiàn)base64圖片上傳方式實(shí)例代碼
- php解析base64數(shù)據(jù)生成圖片的方法
- php實(shí)現(xiàn)將base64格式圖片保存在指定目錄的方法
- 利用PHP將圖片轉(zhuǎn)換成base64編碼的實(shí)現(xiàn)方法
- php基于base64解碼圖片與加密圖片還原實(shí)例
- PHP實(shí)現(xiàn)本地圖片轉(zhuǎn)base64格式并上傳
相關(guān)文章
PHP微信開發(fā)之微信錄音臨時(shí)轉(zhuǎn)永久存儲(chǔ)
這篇文章主要為大家詳細(xì)介紹了PHP微信開發(fā)之微信錄音臨時(shí)轉(zhuǎn)永久存儲(chǔ),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
PHP實(shí)現(xiàn)網(wǎng)頁(yè)內(nèi)容html標(biāo)簽補(bǔ)全和過(guò)濾的方法小結(jié)【2種方法】
這篇文章主要介紹了PHP實(shí)現(xiàn)網(wǎng)頁(yè)內(nèi)容html標(biāo)簽補(bǔ)全和過(guò)濾的方法,結(jié)合實(shí)例形式分析了php常見的標(biāo)簽檢查、補(bǔ)全、閉合、過(guò)濾等相關(guān)操作技巧,需要的朋友可以參考下2017-04-04
PHP實(shí)現(xiàn)遞歸無(wú)限級(jí)分類
這篇文章主要介紹了PHP實(shí)現(xiàn)遞歸無(wú)限級(jí)分類的方法,具有一定的參考價(jià)值,需要的朋友可以參考下2015-10-10
PHP+JS實(shí)現(xiàn)的商品秒殺倒計(jì)時(shí)用法示例
這篇文章主要介紹了PHP+JS實(shí)現(xiàn)的商品秒殺倒計(jì)時(shí)用法,結(jié)合實(shí)例形式分析了php+js針對(duì)日期與時(shí)間操作的相關(guān)技巧,需要的朋友可以參考下2016-11-11
php實(shí)現(xiàn)的一個(gè)簡(jiǎn)單json rpc框架實(shí)例
這篇文章主要介紹了php實(shí)現(xiàn)的一個(gè)簡(jiǎn)單json rpc框架實(shí)例,本文給出了RPC服務(wù)端和客戶端代碼以及應(yīng)用實(shí)例,需要的朋友可以參考下2015-03-03
php 將bmp圖片轉(zhuǎn)為jpg等其他任意格式的圖片
php 將bmp圖片轉(zhuǎn)為jpg等其他任意格式的圖片 ,這樣大家就不用為圖片是bmp格式的而發(fā)愁了。2009-06-06
php使用Jpgraph繪制簡(jiǎn)單X-Y坐標(biāo)圖的方法
這篇文章主要介紹了php使用Jpgraph繪制簡(jiǎn)單X-Y坐標(biāo)圖的方法,實(shí)例分析了Jpgraph繪制坐標(biāo)圖及繪制曲線的相關(guān)技巧,需要的朋友可以參考下2015-06-06
PHP7新特性之抽象語(yǔ)法樹(AST)帶來(lái)的變化詳解
這篇文章主要介紹了PHP7新特性之抽象語(yǔ)法樹(AST)帶來(lái)的變化,結(jié)合實(shí)例形式分析了PHP7抽象語(yǔ)法樹的相關(guān)概念、功能、特性、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2018-07-07

