php的阿里云oss图片单图或批量上传、查询图片是否存在

php

浏览数:423

2019-1-7

    /**
     * Base64上传文件
     * @param string|array $images
     * @param string $model_path
     * @param string $model_type
     * @param string $upload_path
     * @param bool $autoName
     * @return array
     */
    protected function oss_base64_upload($images, $model_path = '', $model_type = 'images', $upload_path = '', $autoName = true)
    {
        $accessKeyId = $this->oss_config['accessKeyId'];
        $accessKeySecret = $this->oss_config['accessKeySecret'];
        $endpoint = $this->oss_config['endpoint'];
        $bucket = $this->oss_config['bucket'];
        $isCName = true;

        if (empty($images)) {
            return ['Code' => 0, 'Msg' => '文件列表不能为空'];
        }

        $file_raw = [];
        $file_type = ['pjpeg', 'jpeg', 'jpg', 'gif', 'bmp', 'png', 'mov', '3gp', 'mp4', 'avi'];
        $preg_type = "image";
        $model_type = strtolower($model_type);
        if ($model_type == 'video') {
            $preg_type = $model_type;
        }

        //数组批量上传
        if (is_array($images) && count($images) > 0) {
            /*
             * $images 批量上传示例(值为一维单列或多列数组)
             * $images = [
             *      "base64/image1..........."
             *      "base64/image2..........."
             * ]
             */

            foreach ($images as $key => $value) {
                $value = trim($value);
                if (preg_match("/^(data:\s*$preg_type\/(\w+);base64,)/", $value, $result)) {
                    $type = strtolower($result[2]);
                    if (in_array($type, $file_type)) {
                        $file_raw[] = [
                            'raw' => base64_decode(str_replace($result[1], '', $value)), //文件流
                            'extension' => $type, //文件后缀
                            'index' => $key,
                        ];
                    } else {
                        return ['Code' => 0, 'Msg' => '文件类型错误'];
                    }
                } else {
                    return ['Code' => 0, 'Msg' => '文件base64格式不合法'];
                }
            }
        }

        //字符串单图上传
        if (is_string($images)) {
            /*
             * $images 上传单个示例,字符串
             * $images = "base64/image..........."
             */

            $images = trim($images);
            if (preg_match("/^(data:\s*$preg_type\/(\w+);base64,)/", $images, $result)) {
                $type = strtolower($result[2]);
                if (in_array($type, $file_type)) {
                    $file_raw[] = [
                        'raw' => base64_decode(str_replace($result[1], '', $images)), //文件流
                        'extension' => $type, //文件后缀
                        'index' => 0,
                    ];
                } else {
                    return ['Code' => 0, 'Msg' => '文件类型错误'];
                }
            } else {
                return ['Code' => 0, 'Msg' => '文件base64格式不合法'];
            }
        }

        if (empty($upload_path)) {
            $model_path = strstr('/', $model_path) ? $model_path : $model_path . '/';
            $upload_path = "{$model_type}/{$model_path}" . date('Y-m-d') . '/';
        }

        require_once(THINK_PATH . 'Extend/Vendor/aliyun-oss/autoload.php');
        $ossClient = new \OSS\OssClient($accessKeyId, $accessKeySecret, $endpoint, $isCName);

        $photo_list = [];
        try {
            if (!empty($file_raw)) {
                foreach ($file_raw as $value) {
                    $name = substr(md5(base64_encode($value['raw']) . base64_encode(time() . mt_rand(33, 126))), 8, 16);
                    if ($autoName === true) {
                        $file_name = $upload_path . $name . "." . strtolower($value['extension']);
                    } else {
                        $file_name = $upload_path;
                    }
                    $getOssInfo = $ossClient->putObject($bucket, $file_name, $value['raw']);
                    $getOssPdfUrl = $getOssInfo['info']['url'];
                    if ($getOssPdfUrl) {
                        $photo_list[$value['index']] = self::http_to_https($getOssPdfUrl);
                    }
                }
            }
        } catch (OssException $e) {
            return ['Code' => 0, 'Msg' => $e->getMessage()];
        }

        return ['Code' => 1, 'Msg' => $photo_list];
    }

    /**
     * OSS内文件是否存在
     * @param string $object @文件路径
     * @return bool
     */
    protected function doesObjectExist($object = '')
    {
        $exist = false;
        if (empty($object)) {
            return $exist;
        }

        $accessKeyId = $this->oss_config['accessKeyId'];
        $accessKeySecret = $this->oss_config['accessKeySecret'];
        $endpoint = $this->oss_config['endpoint'];
        $bucket = $this->oss_config['bucket'];
        $isCName = true;

        require_once(THINK_PATH . 'Extend/Vendor/aliyun-oss/autoload.php');
        $ossClient = new \OSS\OssClient($accessKeyId, $accessKeySecret, $endpoint, $isCName);

        try {
            $exist = $ossClient->doesObjectExist($bucket, $object);
        } catch (OssException $e) {
            $exist = false;
            //printf($e->getMessage() . "\n");
        }

        return $exist;
    }