php圖片處理之圖片轉為base64格式上傳

來源:互聯網
上載者:User

標籤:php   圖片   處理   

我們在開發系統時,處理圖片上傳是不可避免的,使用thinkphp的肯定很熟悉 import("@.ORG.UploadFile"); 的上傳方式。

今天我們來講一個使用html5 base64上傳圖片的方法。

其實就是用到html5 FileReader的介面,既然是html5的,所支援的瀏覽器我就不多說啦,老生常談的問題了,遠離IE,珍惜生命。

先扔個demo出來給大夥體驗體驗哈。

http://t.lanchenglv.com/lan/index.php/Base64/imagesupload

PS:主要給大夥體驗的,別當網盤儲存圖片哈,定期自動刪除圖片的。

可以大概的講一下思路,其實也挺簡單。選擇了圖片之後,js會先把已選的圖片轉化為base64格式,然後通過ajax上傳到伺服器端,伺服器端再轉化為圖片,進行儲存的一個過程。

咱們先看看前端的代碼。

html部分
<inputtype="file"id="imagesfile">
js部分
$("#imagesfile").change(function (){ var file = this.files[0]; //用size屬性判斷檔案大小不能超過5M ,前端直接判斷的好處,免去伺服器的壓力。 if( file.size > 5*1024*1024 ){  alert( "你上傳的檔案太大了!" )  } //好東西來了 var reader=new FileReader(); reader.onload = function(){ // 通過 reader.result 來訪問產生的 base64 DataURL var base64 = reader.result; //列印到控制台,按F12查看 console.log(base64); //上傳圖片 base64_uploading(base64); } reader.readAsDataURL(file);});//AJAX上傳base64function base64_uploading(base64Data){ $.ajax({ type: ‘POST‘, url: "上傳介面路徑", data: {  ‘base64‘: base64Data }, dataType: ‘json‘, timeout: 50000, success: function(data){ console.log(data); }, complete:function() {}, error: function(xhr, type){ alert(‘上傳逾時啦,再試試‘); } });}

其實前端的代碼也並不複雜,主要是使用了 new FileReader(); 的介面來轉化圖片, new FileReader(); 還有其他的介面,想瞭解更多的介面使用的童鞋,自行Google搜尋 new FileReader(); 。

接下來,那就是伺服器端的代碼了,上面的demo,是用thinkphp為架構編寫的,但其他架構也基本通用的。

function base64imgsave($img){ //檔案夾日期 $ymd = date("Ymd"); //圖片路徑地址  $basedir = ‘upload/base64/‘.$ymd.‘‘; $fullpath = $basedir; if(!is_dir($fullpath)){ mkdir($fullpath,0777,true); } $types = empty($types)? array(‘jpg‘, ‘gif‘, ‘png‘, ‘jpeg‘):$types; $img = str_replace(array(‘_‘,‘-‘), array(‘/‘,‘+‘), $img); $b64img = substr($img, 0,100); if(preg_match(‘/^(data:\s*image\/(\w+);base64,)/‘, $b64img, $matches)){ $type = $matches[2]; if(!in_array($type, $types)){ return array(‘status‘=>1,‘info‘=>‘圖片格式不正確,只支援 jpg、gif、png、jpeg哦!‘,‘url‘=>‘‘); } $img = str_replace($matches[1], ‘‘, $img); $img = base64_decode($img); $photo = ‘/‘.md5(date(‘YmdHis‘).rand(1000, 9999)).‘.‘.$type; file_put_contents($fullpath.$photo, $img); $ary[‘status‘] = 1; $ary[‘info‘] = ‘儲存圖片成功‘; $ary[‘url‘] = $basedir.$photo; return $ary; } $ary[‘status‘] = 0; $ary[‘info‘] = ‘請選擇要上傳的圖片‘; return $ary;}

以上就是PHP代碼,原理也很簡單,拿到介面上傳的base64,然後再轉為圖片再儲存。

建了一個github庫,需要源碼體驗的童鞋可以clone來體驗體驗。

https://github.com/bluefox1688/php_images_base64_uploading

使用的是thinkphp 3.2,無需資料庫,PHP環境直接運行即可。

php目錄路徑為:

Application\Home\Controller\Base64Controller.class.php

html目錄路徑為:

Application\Home\View\Base64\imagesupload.html


php圖片處理之圖片轉為base64格式上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.