PHP实现上传图片到 zimg 服务器

关于zimg的了解


最近我们项目需要一台图片服务器存储用户上传的图片,我们使用 zimg 处理和存储图片,下面简单介绍一下如何使用 PHP 上传图片到 zimg,并获取相应图片的返回信息

其实这是转载 啦啦啦 搬运工一枚,很有借鉴价值哦

php代码:

 

$zimg_domain = C("ZIMG_DOMAIN") ; //图片服务器域名

$zimg_upload_url = $zimg_domain . "upload"; //上传地址


    $ext = substr($imgurl, strrpos($imgurl, ".")+1);

    $realpath = realpath(mb_convert_encoding($imgurl, 'GBK', 'utf8'));


    // 上传图片到zimg图片存储服务

    $ch = curl_init();


    // 关键在这里!

    $post_data = file_get_contents($realpath); // raw_post方式

    // 如果是是一个数组,则content_type自动为multipart/form-data

    //        $post_data = array(

    //            'file' => "@$realpath"

    //        );

    $headers = array();

    $headers[] = 'Content-Type:'.$ext; // 还有这里!


    curl_setopt($ch, CURLOPT_URL, $zimg_upload_url);

    curl_setopt($ch, CURLOPT_HEADER, false);

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    //启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。

    curl_setopt($ch, CURLOPT_POST, true);

    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);


    $info = curl_exec($ch);

    curl_close($ch);


    $json = json_decode($info, true);

    $signature = $json['info']['md5'];


    $imgurl = $zimg_domain . $signature;

    return array('imgurl'=>$imgurl,'imgcode'=>$signature);


以上是把图片上传到图片服务器的源码,有需要的朋友可以看看.


猜你喜欢

转载自blog.csdn.net/guzarish/article/details/80424688