温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用PHP怎么将图片上传到 zimg 服务器

发布时间:2021-02-03 10:54:35 来源:亿速云 阅读:186 作者:Leah 栏目:开发技术

本篇文章为大家展示了使用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;

上述内容就是使用PHP怎么将图片上传到 zimg 服务器,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI