重慶分公司,新征程啟航
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)
本篇文章為大家展示了使用PHP怎么將圖片上傳到 zimg 服務(wù)器,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
使用 curl 庫實(shí)現(xiàn)上傳
根據(jù) 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)); // 生成本地臨時(shí)存儲(chǔ)路徑,并生成相應(yīng)文件夾 $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('創(chuàng)建文件夾失敗'); } } $save_image_file = $save_path.$save_rule.".$extension"; // 把圖片存儲(chǔ)到臨時(shí)路徑 file_put_contents($save_image_file, file_get_contents($image_file)); // 獲取臨時(shí)保存的圖片的真實(shí)地址(絕對路徑) $realpath = realpath($save_image_file); // 上傳圖片到 zimg 圖片存儲(chǔ)服務(wù) $ch = curl_init(); // 將圖片內(nèi)容讀取到變量 $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;
上述內(nèi)容就是使用PHP怎么將圖片上傳到 zimg 服務(wù)器,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。