| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593 | <?php/*|--------------------------------------------------------------------------| 查看图片的几个方法|--------------------------------------------------------------------------*/namespace Upload\Src;use Dever;use Upload\Lib\Store\Handle;//use Upload\Lib\Lib\Img;use Dever\Support\Img;class View{    public function getFileName()    {        $image['file']  = Dever::input('file');        $image['key']   = Dever::input('key');                //file_put_contents(DEVER_PATH . 'web/data/upload.txt', var_export($image, true));         if ($image['file']) {            $handle = new Handle($image);            $this->output = $handle->name();        }    }    public function manage()    {        $data = array();        $param = Dever::input('param');        $data['key'] = Dever::input('key');        $data['state'] = Dever::input('state');        $data['pic'] = Dever::input('pic');        $data['wh'] = Dever::input('wh');        $data['ratio'] = 16 / 9;        if ($data['wh']) {            if (strstr($data['wh'], '*')) {                $data['ratio'] = $this->ratio('*', $data['wh']);            } elseif (strstr($data['wh'], 'x')) {                $data['ratio'] = $this->ratio('x', $data['wh']);            } elseif (strstr($data['wh'], 'X')) {                $data['ratio'] = $this->ratio('X', $data['wh']);            } else {                $data['ratio'] = 1;            }        }        $data['search_cate'] = 1;        $data['cate'] = Dever::db('upload/cate')->state();        $data['param'] = '';        if (strstr($data['pic'], '.cr_')) {            $temp = explode('.cr_', $data['pic']);            $param = $data['pic'];            $data['pic'] = $temp[0];        }        if (strstr($param, '.cr_')) {            $temp = explode('.cr_', $param);            $data['param'] = str_replace('.jpg', '', $temp[1]);        }        $data = Dever::render('manage', $data);        Dever::out($data);    }    private function ratio($str, $wh)    {        $temp = explode($str, $wh);        $ratio = $temp[0] / $temp[1];        return $ratio;    }        public function files($view)    {        $data = array();        $key = Dever::input('key');        $cate = Dever::input('cate', 1);        $name = Dever::input('name');        $tag = Dever::input('tag');        $state = Dever::input('state', 1);        $upload_search = Dever::input('upload_search', Dever::input('search'));        $data['cur'] = Dever::input('cur');        $data['value'] = Dever::input('value');        $data['search_pg'] = Dever::input('pg', 1);        $param = array();        $param['upload'] = $key;        $param['state'] = $state;        if ($name) {            $param['source_name'] = $name;            $param['name'] = $name;        }        if ($cate && $cate > 0) {            $param['cate'] = $cate;        }        if ($upload_search) {            $param['search'] = $upload_search;        }        $data['search_cate'] = $cate;        $data['search_tag'] = $tag;        $data['search_name'] = $name;        $data['search_name'] = $name;        $data['upload_search'] = $upload_search;        $data['key'] = $key;        if (strstr($key, '_')) {            $temp = explode('_', $key);            if (is_numeric($temp[0]) && $temp[0] > 0) {                $key = $temp[0];            }        }        if (!is_numeric($key)) {            $data += Dever::load($key, $data);        } else {            $data['name'] = '文件名';            $data['file'] = Dever::db('upload/file')->getData($param);            $data['cate'] = Dever::db('upload/cate')->state();            $data['config'] = Dever::db('upload/upload')->one($key);            if ($data['config'] && isset($data['config']['yun']) && $data['config']['yun']) {                $data['yun'] = Dever::db('upload/yun')->one($data['config']['yun']);            }        }                $data['total'] = Dever::total();                if ($data['file']) {            $pic = Dever::config('host')->script . 'img/video.jpg';            foreach ($data['file'] as $k => $v) {                if (!strstr($v['file'], 'http')) {                    if (isset($data['config']) && $data['config']['save_type'] >= 3 && isset($data['yun'])) {                        $data['file'][$k]['url'] = $data['yun']['host'] . $v['file'];                    } elseif (strstr($v['file'], '{uploadRes}')) {                        $data['file'][$k]['url'] = Dever::upload($v['file']);                    } else {                        $data['file'][$k]['url'] = Dever::upload('{uploadRes}' . $v['file']);                    }                } else {                    $data['file'][$k]['url'] = $v['file'];                }                $data['file'][$k]['value_array'] = array();                if (isset($v['value']) && $v['value']) {                    parse_str($v['value'], $data['file'][$k]['value_array']);                }                if (!$view) {                    if ($v['state'] == 1) {                        $data['file'][$k]['stateColor'] = 'red';                        $data['file'][$k]['stateName'] = '删除';                        $data['file'][$k]['stateUrl'] = Dever::url('view.setState?id=' . $v['id'] . '&state=2', 'upload');                    } else {                        $data['file'][$k]['stateColor'] = 'blue';                        $data['file'][$k]['stateName'] = '恢复';                        $data['file'][$k]['stateUrl'] = Dever::url('view.setState?id=' . $v['id'] . '&state=1', 'upload');                    }                    $data['file'][$k]['valueUrl'] = Dever::url('view.setValue?id=' . $v['id'], 'upload');                }                                $data['file'][$k]['check'] = false;                if ($data['value'] && $data['value'] == $v['id']) {                    $data['file'][$k]['check'] = true;                } elseif ($data['cur'] && (isset($v['pic']) && $v['pic'] && strstr($data['cur'], $v['pic']) || isset($v['file']) && $v['file'] && strstr($data['cur'], $v['file']))) {                    $data['file'][$k]['check'] = true;                }                                if (isset($v['pic']) && $v['pic']) {                    $data['file'][$k]['pic'] = $v['pic'];                } elseif (!isset($v['ext'])) {                    $data['file'][$k]['pic'] = $pic;                } else {                    if ($v['ext'] != '.jpg' && $v['ext'] != '.png' && $v['ext'] != '.bmp' && $v['ext'] != '.gif') {                        $data['file'][$k]['pic'] = $pic;                    } else {                        $data['file'][$k]['pic'] = $data['file'][$k]['url'];                    }                }                                if (!isset($data['file'][$k]['pic'])) {                    $data['file'][$k]['pic'] = $pic;                }            }        }        if ($view) {            return $data;        }        $data = Dever::render('show', $data);        Dever::out($data);    }    # 视频截图    public function videoCover()    {        $data = array();        $data['key'] = Dever::input('key');        $data['cur'] = Dever::input('cur');        $data['cover'] = Dever::input('cover');        $data['cover_pic'] = Dever::load('upload/yun')->cover($data['key'], $data['cur']);        $data['video'] = Dever::url('view.video?video=' . Dever::encode($data['cur']), 'upload');        $data['cover_pic_url'] = Dever::url('view.coverPic?key='.$data['key'].'&video=' . Dever::encode($data['cur']), 'upload');        $data = Dever::render('cover', $data);        Dever::out($data);    }    # 展示视频    public function video()    {        $video = Dever::decode(Dever::input('video'));        return '<video src="'.$video.'" height="200" controls="true"/>';    }    # 展示视频截图    public function coverPic()    {        $num = Dever::input('num');        $key = Dever::input('key');        $video = Dever::decode(Dever::input('video'));        $pic = Dever::load('upload/yun')->cover($key, $video, $num);        return $pic;    }    public function setState()    {        $id = Dever::input('id');        $state = Dever::input('state');        $where['where_id'] = $id;        $where['state'] = $state;        Dever::db('upload/file')->update($where);        return 'ok';    }    public function setValue()    {        $id = Dever::input('id');        $value = Dever::input('value');        $where['where_id'] = $id;        $where['value'] = $value;        Dever::db('upload/file')->update($where);        return 'ok';    }    # 根据key search 查找文件    public function search($key = 1, $search)    {        $key = Dever::input('key', $key);        $search = Dever::input('search', $search);        Dever::setInput('key', $key);        Dever::setInput('upload_search', $search);        $data = $this->files(true);        return $data['file'];    }    public function kindeditorFile()    {        $key = Dever::input('key');        $param['upload'] = $key;        $file = Dever::db('upload/file')->state();        $config = Dever::db('upload/upload')->one($key);        $list = array();        if ($file) {            $i = 0;            foreach ($file as $k => $v) {                if ($v['name'] && $v['file']) {                    $list[$i] = array();                    $list[$i]['is_dir'] = false;                    $list[$i]['has_file'] = false;                    $list[$i]['filesize'] = $v['size'];                    $list[$i]['dir_path'] = '';                    $list[$i]['is_photo'] = true;                    $list[$i]['filetype'] = str_replace('.', '', $v['ext']);                    $list[$i]['filename'] = $v['source_name'];                    $list[$i]['path'] = '';                    $list[$i]['file'] = Dever::upload('{uploadRes}' . $v['file']);                    $list[$i]['datetime'] = date('Y-m-d H:i:s', filemtime(Dever::local($list[$i]['file'])));                    $i++;                }            }        }        $result = array();        //相对于根目录的上一级目录        $result['moveup_dir_path'] = $config['id'];        //相对于根目录的当前目录        $result['current_dir_path'] = '';        //当前目录的URL        $result['current_url'] = '';        //文件数        $result['total_count'] = count($list);        //文件列表数组        $result['file_list'] = $list;        Dever::outDiy($result);    }    # webp    public function webp($file)    {        $host = Dever::config('host')->uploadRes;        $root = Dever::data() . 'upload/';        $source = str_replace(array('{uploadRes}', $host), $root, $file);        $dest = $source . '.webp';        if (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'image/webp')) {            if (!is_file($dest)) {                Dever::run('cwebp '.$source.' -o ' . $dest . '');                header('Content-type: image/webp');                readfile($source);            } else {                header('Content-type: image/webp');                readfile($dest);            }        } else {            header('Content-type: image/webp');            readfile($source);        }    }    # 获取云端配置    public function getYun($key)    {        $key = Dever::input('key', $key);        $config = Dever::load('upload/lib/config')->get($key);        if ($config && $config['yun']) {            return $config['yun']['host'];        }        return false;    }	# 根据配置生成缩略图、裁切图	public function get($file = '', $type = '', $id = '')	{        $file = Dever::input('file', $file);        $array = $this->getFile($file, $type, $id);        $source = $array[0];        $type = $array[1];        $id = $array[2];        $host = Dever::config('host')->uploadRes;        $path = $dest = $file;        $handle = true;        $local = false;        $image = array();        if (strstr($file, '{uploadRes}') || strstr($file, $host)) {            # 本地的            $local = true;            $root = Dever::data() . 'upload/';            $source = Dever::pic($source);            $dest = str_replace(array('{uploadRes}', $host), $root, $file);            $path = str_replace($root, '', $dest);            if (strstr($source, $host)) {                                $source = $root . str_replace($host, '', $source);                if (!is_file($source)) {                    $source = $array[0];                }            }            if ($type == 'wp') {                $temp = explode('.', $dest);                $dest = $temp[0] . '.webp';                $file = str_replace($root, $host, $dest);            }            if (is_file($dest)) {                $file = $dest;                $handle = false;            }        } elseif (strstr($file, 'http')) {            $data = parse_url($file);            $path = ltrim($data['path'], '/');            $host = $data['scheme'] . '://' . $data['host'] . '/';            $image['yun'] = Dever::db('upload/yun')->one(array('host' => $host));        }        if ($handle) {            $temp = explode('/', $path);            $key = $temp[0];            $image['file']  = false;            $image['key']   = intval($key);            $handle = new Handle($image);            if ($type) {                $method = 'handle_' . $type;                $file = $handle->$method($id, $source, $dest, $path);            } elseif (!$local && $handle->yun) {                $yun = $handle->yun->getData();                $file = $yun['host'] . $source;            }        }        $file = Dever::uploadRes($file);		return $file;	}	public function source($file)	{		# 搜索文件信息        $array = $this->getFile($file);		return $array[0];	}    private function getFile($file, $type = false, $id = false)    {        if (strstr($file, '_wp')) {            $array = explode('_wp', $file);            $type = 'wp';        } elseif (strstr($file, '_p')) {            $array = explode('_p', $file);            $type = 'p';        } elseif (strstr($file, '_c')) {            $array = explode('_c', $file);            $type = 'c';        } elseif (strstr($file, '_t')) {            $array = explode('_t', $file);            $type = 't';        }        if (isset($array)) {            $temp = explode('.', $array[1]);            $id = $temp[0];            $file = $array[0] . '.' . $temp[1];        }        return array($file, $type, $id);    }	/**     * create 生成图片     * @param array $config     * @param string $filename     *     * @return mixed     */	public function create($config = array(), $filename = '', $type = 'gd', $update = 2)	{		$this->img = isset($this->img) ? $this->img : new Img();        /*        $config = array        (            'background' => '/www/grow/data/upload/1/2018/07/19/a49fe8914df0eada4d4b7d530d7fa5ba.jpg',            'param' => array            (                0 => array                (                    'method' => 'mark',                    'water' => '/www/grow/data/upload/1/2018/07/19/46d5b0c5c25c800cf197ea4f64f503f2.jpg',                    //left,top                    'position' => array(100,200),                    'width' => 100,                    'height' => 100,                ),                1 => array                (                    'method' => 'txt',                    'name' => 'test文字',                    'color' => '#FF0000',                    'position' => array(100,200),                    'size' => 50,                    'angle' => 0,                    'font' => '/www/grow/config/fonts/simsun.ttc',                ),            ),        );        */        if (!$filename) {            $filename = Dever::id();        }        $file =  Dever::pathDay('upload/poster', false) . md5($filename) . '.jpg';        if (!is_file($file) || $update == 1) {            $this->img->setType($type);            $result = $this->img->init($config['background'], $config['param'], true, $file);            if ($result) {                $file = array_pop($result);            }        } else {        }        $host = str_replace(DEVER_APP_NAME . '/', '', Dever::config('host')->base);        return $host . str_replace(DEVER_PROJECT_PATH, '', $file);        $file = Dever::pic($file);        return $file;	}	public function creates($config = array(), $filename = '')	{		if(empty($filename)) header("content-type: image/png");        $imageDefault = array(            'left'=>0,            'top'=>0,            'right'=>0,            'bottom'=>0,            'width'=>100,            'height'=>100,            'opacity'=>100        );        $textDefault = array(            'text'=>'',            'left'=>0,            'top'=>0,            'fontSize'=>32,       //字号            'fontColor'=>'255,255,255', //字体颜色            'angle'=>0,        );        $background = $config['background'];//海报最底层得背景        //背景方法        $backgroundInfo = getimagesize($background);        $backgroundFun = 'imagecreatefrom'.image_type_to_extension($backgroundInfo[2], false);        $background = $backgroundFun($background);        $backgroundWidth = imagesx($background);  //背景宽度        $backgroundHeight = imagesy($background);  //背景高度        $imageRes = imageCreatetruecolor($backgroundWidth,$backgroundHeight);        $color = imagecolorallocate($imageRes, 0, 0, 0);        imagefill($imageRes, 0, 0, $color);        // imageColorTransparent($imageRes, $color);  //颜色透明        imagecopyresampled($imageRes,$background,0,0,0,0,imagesx($background),imagesy($background),imagesx($background),imagesy($background));        //处理了图片        if (!empty($config['image'])) {            foreach ($config['image'] as $key => $val) {                $val = array_merge($imageDefault,$val);                $info = getimagesize($val['url']);                $function = 'imagecreatefrom'.image_type_to_extension($info[2], false);                if ($val['stream']) {   //如果传的是字符串图像流                    $info = getimagesizefromstring($val['url']);                    $function = 'imagecreatefromstring';                }                if (isset($val['radius']) && $val['radius']) {                	$res = $this->radius($val['url'], $val['radius']);                } else {                	$res = $function($val['url']);                }                                $resWidth = $info[0];                $resHeight = $info[1];                //建立画板 ,缩放图片至指定尺寸                $canvas = imagecreatetruecolor($val['width'], $val['height']);                $color   = imagecolorallocate($background, 202, 201, 201); // 为真彩色画布创建白色背景,再设置为透明    			    imagefill($canvas, 0, 0, $color);			    imageColorTransparent($canvas, $color);                //关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h)                imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'],$resWidth,$resHeight);                $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']) - $val['width']:$val['left'];                $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']) - $val['height']:$val['top'];                //放置图像                imagecopymerge($imageRes,$canvas, $val['left'],$val['top'],$val['right'],$val['bottom'],$val['width'],$val['height'],$val['opacity']);//左,上,右,下,宽度,高度,透明度            }        }        # 处理文字        if (!empty($config['text'])) {            foreach ($config['text'] as $key => $val) {                $val = array_merge($textDefault,$val);                list($R,$G,$B) = explode(',', $val['fontColor']);                $fontColor = imagecolorallocate($imageRes, $R, $G, $B);                $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']):$val['left'];                $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']):$val['top'];                imagettftext($imageRes,$val['fontSize'],$val['angle'],$val['left'],$val['top'],$fontColor,$val['fontPath'],$val['text']);            }        }        # 生成图片        if (!empty($filename)) {            $res = imagepng ($imageRes,$filename,9);            imagedestroy($imageRes);            if(!$res) return false;            return $filename;        } else {            imagepng ($imageRes, null, 9);            imagedestroy($imageRes);        }	}}
 |