<?php
namespace App\Http\Controllers\Commons;
/**
* 公共类
* 樊佑剑
* 20150505
* 功能 :给该系统提供所有公共方法调用
*/
class Util
{
/**
* 清除 缓存文件
* f y j
*
*/
public static function deleteStoreFile($file)
{
$filedata = explode('/', $file);
$num = count($filedata);
$newfile_tmp= '';
$newfile = '';
foreach ($filedata as $key => $value) {
if(($key+2)<=$num)
{
$newfile .=empty($key)?$value:'/'.$value;
}
if(($key+1)<=$num-2)
{
$newfile_tmp .=empty($key)?$value:'/'.$value;
}
}
if (file_exists($file)) {
Util::deldir($newfile);
rmdir($newfile_tmp);
}
}
/**
* 清空 文件夹下面所有 子文件夹
* @param [type] $dir [description]
* @return [type] [description]
*/
public static function deldir($dir) {
//先删除目录下的文件:
$dh=opendir($dir);
while ($file=readdir($dh)) {
if($file!="." && $file!="..") {
$fullpath=$dir."/".$file;
if(!is_dir($fullpath)) {
unlink($fullpath);
} else {
Util::deldir($fullpath);
}
}
}
closedir($dh);
if(rmdir($dir)) {
return true;
}
return false;
}
/**
* 创建 目录
* @param [type] $dir [description]
* @return [type] [description]
*/
public static function createDir($dir)
{
return is_dir($dir) or (Util::createDir(dirname($dir)) and mkdir($dir, 0777));
}
/**
* 等比例输小图片
* type 1 直播 2 新闻 3 讲者
* @param [type] $oldfilepath 原始文件
* @param [type] $per 输小比例
* @param [type] $nfilepath 文件新路径
* @param [type] $extention 图片格式 png jpeg jpg
*/
public static function resetImage($type,$oldfilepath,$per,$newfilename)
{
$thum_file_path='';
switch ($type) {
case '1':
# code...
$thum_file_path = config('msgconfig.UPLOAD_FILE_PATH').'/'.date("Ymd");
break;
case '2':
$thum_file_path = config('msgconfig.UPLOAD_FILE_NEWS').'/'.date("Ymd");
# code...
break;
case '3':
# code...
$thum_file_path = config('msgconfig.UPLOAD_FILE_HEADER_PATH').'/'.date("Ymd");
break;
default:
# code...
break;
}
list($width, $height)=getimagesize($oldfilepath);
$n_w=$width*$per;
$n_h=$height*$per;
$new=imagecreatetruecolor($n_w, $n_h);
$imginfo = getimagesize($oldfilepath);
$type = $imginfo[2];
switch ($type) {
case '2':
$img = imagecreatefromjpeg($oldfilepath);
break;
case '3':
# code...
$img = imagecreatefrompng($oldfilepath);
break;
}
// $img=imagecreatefromjpeg($filename);
//copy部分图像并调整
imagecopyresized($new, $img,0, 0,0, 0,$n_w, $n_h, $width, $height);
//图像输出新图片、另存为
imagejpeg($new, $thum_file_path.'/'.$newfilename);
imagedestroy($new);
imagedestroy($img);
// return $nfilepath.'/'.$newfilename;
}
/**
* 调用远程API
* $type 1 登录验证 2 注册 3 更新
* $param 提交数据
* $param['username'=>eee,'password'=>'111']
* @return json_decode();
*/
public static function apiPostPush($type,$param)
{
$url='';
switch ($type) {
case '1':
$url = config('msgconfig.API_LOGIN');
break;
case '2':
$url = config('msgconfig.API_REGISTER');
break;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//1 输出 html
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$param);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
}
/**
* 远程调用 API
* get 方式
*/
// public static function apiGetPush($url)
// {
// $ch = curl_init($url);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回
// curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
// $this->result = curl_exec($ch) ;
// curl_close($ch);
// return $this->result;
// }
}
php 图片压缩 CURL 请求 删除文件夹目录和子文件 创建 目录
最新推荐文章于 2022-04-08 09:50:19 发布