PHP實現上傳圖片到 zimg 伺服器_php執行個體

來源:互聯網
上載者:User

最近我們項目需要一台圖片伺服器儲存帳戶圖片,我們使用 zimg 處理和儲存圖片,下面簡單介紹一下如何使用 PHP 上傳圖片到 zimg,並擷取相應圖片的返回資訊

使用 curl 庫實現上傳

根據 zimg 使用文檔,我們想要讓 zimg 返回 json 資訊,就必須 raw_post 上傳圖片,下面是示範代碼

$upload_url = 'http://192.168.0.99:5000/upload';$image_file = './test.jpg';// 擷取圖片尾碼$value = explode(".", $image_file);$extension = strtolower(array_pop($value));// 產生本地臨時儲存路徑,並產生相應檔案夾$dir = 'aurthur';$save_path = 'uploads/'.$dir.'/'.date('Y').'/'.date('md').'/';$save_rule = md5(uniqid(mt_rand(), true));if(!is_dir($save_path)){  if(false === mkdir($save_path, 0700, true)){    exit('建立檔案夾失敗');  }}$save_image_file = $save_path.$save_rule.".$extension";// 把圖片儲存到臨時路徑file_put_contents($save_image_file, file_get_contents($image_file));// 擷取臨時儲存的圖片的真真實位址(絕對路徑)$realpath = realpath($save_image_file);// 上傳圖片到 zimg 圖片儲存服務$ch = curl_init();// 將圖片內容讀取到變數 $post_data ;$post_data = file_get_contents($realpath);$headers = array();// 一定要添加此 header$headers[] = 'Content-Type:'.$extension;curl_setopt($ch, CURLOPT_URL, $upload_url);curl_setopt($ch, CURLOPT_HEADER, false);curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);// raw_post方式$info = curl_exec($ch);curl_close($ch);$json = json_decode($info, true);$signature = $json['info']['md5'];echo $signature;

如果想測試代碼的話,請把上面的uploadurl改成你自己的zimg伺服器位址,並把image_file 改成你需要上傳的圖片的路徑

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.