工作中遇到的处理图片相关问题(需要下载)

 

        在工作中,合作方调用接口会向我们传递一些数据,中间会有图片,但是可能合作方传递的数据不符合要求而又不能强制要求甲方爸爸来改动这些参数,所以只能将图片下载下来,来获取图片的宽高等属性。

/**
 * 下载图片
 * @param $picUrl
 * @return array|bool
 */
public static function downloadPic($picUrl){

    if(empty($picUrl)){

        return false;
    }


    $picUrlInfo = parse_url($picUrl);

    if(!isset($picUrlInfo['host']) || empty($picUrlInfo['host'])){

        return false;
    }


    $headers = get_headers($picUrl);

    $isWebP = false;

    foreach ($headers as $header) {

        if(stripos($header,'image/webp') !== false){

            $isWebP = true;

            break;
        }
    }


    try{

        $picFile = CommonModel::saveRemoteFile($picUrl);
    } catch (\Exception $e) {
        CommonLog::info([

            'msg' => '下载图片失败',

            'pic_url' => $picUrl,
        ]);

        return false;
    }

    if(!$picFile) {

        return false;
    }


    if($isWebP) {

        return [

            'type'   => 'webp',

            'file_path' => $picFile,

            'file_md5' => md5_file($picFile)
        ];
    }


    if(@imagecreatefromstring(file_get_contents($picFile)) === false){
        CommonLog::info([

            'msg' => '下载图片失败-下载的文件不是图片',

            'pic_url' => $picUrl,

            'filesize'  => filesize($picFile)
        ]);
        @unlink($picFile);


        $picFile = self::downloadPicWithHeader($picUrl);


        if(!$picFile)  return false;
    }


    list($width, $height, $type, $attr) = getimagesize($picFile);


    $imageTypeMap = [

        1 => 'gif',

        2 => 'jpg',

        3 => 'png',

        4 => 'swf',

        5 => 'psd',

        6 => 'bmp',

        7 => 'tiff',

        8 => 'tiff',

        9 => 'jpc',

        10 => 'jp2',

        11 => 'jpx',

        12 => 'jb2',

        13 => 'swc',

        14 => 'iff',

        15 => 'wbmp',

        16 => 'xbm',
    ];


    return [

        'width'  => $width,

        'height' => $height,

        'type'   => $imageTypeMap[$type],

        'attr'   => $attr,

        'file_path' => $picFile,

        'file_md5' => md5_file($picFile)
    ];

}

 

/**
 * 下载远程图片
 * @param $url
 * @param int $connectTimeout //单位秒
 * @param int $queryTimeout //单位秒
 * @return bool|string
 */
public static function saveRemoteFile($url, $connectTimeout = 10, $queryTimeout = 1800)
{

    Yii::$app->params['connect_timeout'] = $connectTimeout * 1000;
    Yii::$app->params['query_timeout'] = $queryTimeout * 1000;

    $content = CurlServiceDao::get($url, [], 'curl_download', false);

    if (empty($content)) {

        return false;
    }

    $pathInfo = pathinfo($url);

    $savePath = sys_get_temp_dir() . '/' . md5(microtime(true) . $url) . '.' .         $pathInfo['extension'];

    file_put_contents($savePath, $content);

    return $savePath;
}

 

/**
 * 使用Curl获取Curl头来下载图片
 * @param $picUrl
 * @return bool|string
 */
public static function downloadPicWithHeader($picUrl){

    try{

        $curl = new CurlCommon();

        $curl->setOpt(CURLOPT_HEADER, 1);

        $curl->setOpt(CURLOPT_NOSIGNAL, true);

        $curl->setOpt(CURLOPT_CONNECTTIMEOUT , 10);

        $curl->setOpt(CURLOPT_TIMEOUT, 1800);

        $curl->setOpt(CURLOPT_FOLLOWLOCATION, 1);

        $result = $curl->get($picUrl);

        $pathInfo = pathinfo($picUrl);

        $picFile = sys_get_temp_dir() . '/' . md5(microtime(true).$picUrl). '.' .         $pathInfo['extension'];

        file_put_contents($picFile,$result->response);

        unset($result);

        if(@imagecreatefromstring(file_get_contents($picFile)) === false){
            CommonLog::info([

                'msg' => '下载图片失败-下载的文件不是图片',

                'pic_url' => $picUrl,

                'filesize'  => filesize($picFile)
            ]);
            @unlink($picFile);


            return false;
        }

        return $picFile;
    } catch (\Exception $e) {
        CommonLog::info([

            'msg' => '下载图片失败',

            'pic_url' => $picUrl,
        ]);

        return false;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值