Upload.php 2.04 KB
<?php

/**
 *
 * @author tongdesheng
 *
 */
class YHMApi_App_V1_Upload extends YHMApi_App_V1_Base
{

    /**
     *
     * @param array $params
     * @param string $fields
     */
    public static function image(array $params, $fields = '*')
    {
        if (empty($params['bucket'])) {
            $params['bucket'] = 'yhfair';
        }
        if (empty($params['thumb_info'])) {
            return self::result(400, '缩略图信息不能为空.');
        }
        $thumbInfo = $params['thumb_info'];
        if (empty($thumbInfo)) {
            return self::result(500, '缩略图信息数据包错误.');
        }
        $thumbInfo = explode('x', $thumbInfo);
        if (count($thumbInfo) != 2) {
            return self::result(500, '缩略图width x height数据错误.');
        }
        list($width, $height) = $thumbInfo;
        if (empty($_FILES)) {
            return self::result(400, '上传对象不能为空.');
        }
        $mode = 1;
        if (!empty($params['mode'])) {
            $mode = $params['mode'];
        }
        $bucket = $params['bucket'];
        try {
            $upload = new YHMUpload_Qiniu_Upload($bucket);
            $uploadResult = $upload->uploadFile();
            if (empty($uploadResult['hit'])) {
                return self::result(500, '上传文件失败', $uploadResult);
            }
            $resultImages = array();
            foreach ($uploadResult['hit'] as $key => $imagePath) {
                if (($imagePath=="")||($imagePath=="null"))
                {
                   return self::result(400, '图片上传失败.');  
                }
                
                $resultImages[] = array(
                    'image_path' => $imagePath,
                    'bucket' => $params['bucket'],
                    'image_url' => YHMUpload_Images::view($imagePath, $width, $height, $mode, $bucket)
                );
            }
        } catch (Exception $e) {
            return self::result(500, $e->getMessage());
        }
        return self::result(200, '上传成功', $resultImages);
    }
}