本文實例講述了php實現將base64格式圖片保存在指定目錄的方法。分享給大家供大家參考,具體如下:
<?php
header('Content-type:text/html;charset=utf-8');
$base64_image_content = $_POST['imgBase64'];
//匹配出圖片的格式
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
$type = $result[2];
$new_file = "upload/active/img/".date('Ymd',time())."/";
if(!file_exists($new_file))
{
//檢查是否有該文件夾,如果沒有就創建,並給予最高權限
mkdir($new_file, 0700);
}
$new_file = $new_file.time().".{$type}";
if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
echo '新文件保存成功:', $new_file;
}else{
echo '新文件保存失敗';
}
}
?>
PS:這裡再為大家推薦幾款在線圖片工具供大家參考使用
圖片轉換為Base64編碼在線工具:
http://tools.jb51.net/transcoding/img2base64
在線Email郵箱圖標制作工具:
http://tools.jb51.net/email/emaillogo
在線PS圖像處理工具:
http://tools.jb51.net/aideddesign/webps
在線圖片格式轉換(jpg/bmp/gif/png)工具:
http://tools.jb51.net/aideddesign/picext
ICO圖標在線生成工具:
http://tools.jb51.net/aideddesign/ico_img
更多關於PHP相關內容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結》、《php面向對象程序設計入門教程》、《PHP網絡編程技巧總結》、《PHP數組(Array)操作技巧大全》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。