UploadController.php 3.76 KB
<?php
/**
 * 图片上传
 *
 */

namespace backend\controllers;

use backend\widgets\YHGImage\Upload\Client as UploadClient;
use backend\widgets\YHGImage\Common\Images as CommonImages;

class UploadController extends BaseController
{
    
    /**
     * $_FILES test
     */
    public function indexAction()
    {
        $ret = UploadClient::self()->saveImage('image');
        $url = CommonImages::getSourceUrl($ret);
        $this->renderJson(200,'OK',array(
            'uri'=>$ret,
            'template'=>CommonImages::getTemplate($ret),
            'source'=>CommonImages::getSourceUrl($ret)
        ));
    }
    
    /**
     * 图片上传方法
     */
    public function actionUploadimg()
    {
        $images = UploadClient::self()->saveImage('images');
        if ($images)
        {
            $this->renderJson(200, '上传成功', array(
                'uri' => $images,
                'url' => CommonImages::getSourceUrl($images)
            ));
        }else
        {
            header('HTTP/1.1 403 Forbidden');
            $this->renderJson(403, '上传失败', $images);
        }
    }
    
    /**
     * 身份证照片上传方法
     */
    public function uploadidcardAction()
    {
        $images = UploadClient::self()->uploadIdcard('images');
        if ($images)
        {
            $this->renderJson(200, '上传成功', array(
                'uri' => $images,
                'url' => CommonImages::getPrivateUrl($images)
            ));
        }else
        {
            header('HTTP/1.1 403 Forbidden');
            $this->renderJson(403, '上传失败');
        }
    }
    
    /**
     * 图片上传  返回值包括base64编码
     */
    public function uploadImgReturnFullUrlAction()
    {
        $images = UploadClient::self()->saveImage('images');
        if ($images)
        {
            $qiniu_url = CommonImages::getImageUrl($images, 100,100);
            $file = substr($qiniu_url,0,strrpos($qiniu_url, '?'));
            $data = array('src' => $images, 'full_url' => $file);
            $this->renderJson(200, '上传成功', $data);
        }else
        {
            header('HTTP/1.1 403 Forbidden');
            $this->renderJson(403, '上传失败', array());
        }
    }
    
     /**
     * ckeditor图片上传方法
     */
    public function ckeditorImgAction()
    {
        $param = $this->_getRequests();
        $callback = $param['CKEditorFuncNum'];
        $images = UploadClient::self()->saveImage('upload');
        if ($images)
        {
            $url = CommonImages::getSourceUrl($images);
            exit('<script type="text/javascript">window.parent.parent.CKEDITOR.tools.callFunction('.$callback.', "'.$url.'", "上传成功");</script>');
        }else
        {
            exit('<script type="text/javascript">window.parent.CKEDITOR.tools.callFunction('.$callback.', "", "上传失败");</script>');
        }
    }
    
    /**
     * POST 图片源地址src参数上传,返回绝对路径
     */
    public function uploadimgsrcAction()
    {
        $src = $this->_post('src');
        if ($src){
            /**
             * 读取缓存是否有曾经上传的记录,缓存没有再上传
             */
            $redis = CacheManager::Redis();
            $key = __FUNCTION__.'#'.$src;
            if (!$pic = $redis->get($key)){
                if ($pic = UploadClient::self()->uploadStreamImage($src,true)){
                    $redis->put($key,$pic,1440);
                }
            }
            if ($pic){
                $complete_url = CommonImages::getSourceUrl($pic);
                $this->renderJson(200, '上传成功', array('url' => $pic,'complete_url'=> $complete_url));
            }
        }
        
        header('HTTP/1.1 403 Forbidden');
        $this->renderJson(403, '上传失败', array());
    }
}