You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

397 lines
12 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
// 文件类
/**
* 创建文件夹并赋予777权限
* @param string $dir 目录 最前面加./标识项目根目录 后面目录截取到最后一位/
* @date 2021-01-22
*/
function create_directory($dir)
{
// 最后一位是'/',不用做任何改动即为文件夹路径
if (substr($dir, -1) == '/') {
$path = $dir;
// 最后一位不是'/',获取文件夹路径
} else {
$path = dirname($dir);
}
// 文件夹不存在时创建文件夹路径并赋777权限
if (!file_exists($path)) {
$result = mkdir($path, 0777, true);
}
}
/**
* 获取文件后缀
* @param string $file_path 文件路径
* @date 2022-05-12
*/
function get_suffix($file_path)
{
// 以.分割文件路径
$array = explode('.', $file_path);
// 获取最后一部分,即为后缀
$suffix = end($array);
// 返回数据
return $suffix;
}
/**
* 获取文件名称(含文件后缀)
* @param string $file_path 文件路径
* @date 2022-05-12
*/
function get_file_name($file_path)
{
// 以/分割文件路径
$array = explode('/', $file_path);
// 获取最后一部分,即为后缀
$file_name = end($array);
// 返回数据
return $file_name;
}
/**
* 利用服务器ffmpeg命令截取视频帧数
* @param string $local_file_url 服务器文件路径,以./开头
* @param string $screenshot_local_file_url 保存的视频截帧图片路径,以./开头
* @param string $time 截取视频秒数
* @date 2021-06-24
*/
function get_video_screenshot_by_ffmpeg($local_file_url, $screenshot_local_file_url, $time = 0)
{
return exec('ffmpeg -i ' . $local_file_url . ' -y -f image2 -ss ' . $time . ' -t 0.001 ' . $screenshot_local_file_url);
}
/**
* 去除域名获得相对路径
* @param string $file_url 文件路径
* @param string $split 以什么格式的域名进行分割
* @date 2022-05-12
*/
function getFileRealativePath($file_url, $split = '.com/')
{
//获取object
$array = explode($split, $file_url);
$object = $array[1];
return $object;
}
/**
* 保存OSS文件到本地
* @param string $file_url 文件URL
* @param string $local_file_url 需要下载到的本地路径
* @date 2020-07-27
*/
function saveFileToLocal($file_url, $local_file_url = '')
{
if (stripos($file_url, 'http') !== false) { //全路径
if (!$local_file_url) {
//获取后缀
$array = explode('.', $file_url);
$postfix = end($array);
$local_file_url = './temp/' . md5(microtime(true)) . '.' . $postfix;
}
//获取object
$array = explode('.com/', $file_url);
$object = $array[1];
$uid = defined('UID') ? UID : 0;
$oss_param = get_ali_oss_config($uid);
$oss_class = new \ali\oss\Oss($oss_param);
$local_file_url = $oss_class->getObject($object, $local_file_url);
} else {
$local_file_url = '.' . $file_url;
}
return $local_file_url;
}
/**
* 创建二维码(保存到服务器)
* @param string $link_url 扫码跳转链接
* @param string $file_dirname 存储文件夹,以"./"开头
* @param string $file_name 存储名称,必填
* @return string
*/
function create_qr_code_to_server($link_url, $file_dirname, $file_name)
{
// 本地存储位置
$local_file_path = './temp/' . $file_dirname . '/' . $file_name;
// 文件夹不存在时,先生成文件夹
create_directory($local_file_path);
//创建二维码
$writer = new \Endroid\QrCode\Writer\PngWriter();
$qrCode = \Endroid\QrCode\QrCode::create($link_url)
->setSize(200) //二维码尺寸
->setMargin(5)
// 设置二维码的rgb颜色和透明度a这里是黑色
->setForegroundColor(new \Endroid\QrCode\Color\Color(0, 0, 0))
// 设置二维码图片的背景底色,这里是白色
->setBackgroundColor(new \Endroid\QrCode\Color\Color(255, 255, 255));
$result = $writer->write($qrCode);
// 保存到本地路径
$result->saveToFile($local_file_path);
// 返回本地路径
return $local_file_path;
}
/**
* 创建二维码(先传到本地 后上传至OSS并删除本地文件)
* @param string $link_url 扫码跳转链接
* @param string $oss_file_dirname OSS对象存储文件夹
* @param string $file_path OSS对象存储名称必填
* @date 2022-11-18
* @return string
*/
function create_qr_code_to_oss($link_url, $oss_file_dirname, $oss_file_name)
{
// 本地存储位置
$local_file_path = './temp/' . $oss_file_dirname . '/' . $oss_file_name;
// 文件夹不存在时,先生成文件夹
create_directory($local_file_path);
// 创建二维码
$writer = new \Endroid\QrCode\Writer\PngWriter();
$qrCode = \Endroid\QrCode\QrCode::create($link_url)
->setSize(200) //二维码尺寸
->setMargin(5)
// 设置二维码的rgb颜色和透明度a这里是黑色
->setForegroundColor(new \Endroid\QrCode\Color\Color(0, 0, 0))
// 设置二维码图片的背景底色,这里是白色
->setBackgroundColor(new \Endroid\QrCode\Color\Color(255, 255, 255));
$result = $writer->write($qrCode);
// 保存到本地路径
$result->saveToFile($local_file_path);
// 上传到OSS
$uid = defined('UID') ? UID : 2;
$oss_param = get_ali_oss_config($uid);
$oss_class = new \ali\oss\Oss($oss_param);
$res = $oss_class->uploadFile($local_file_path, $oss_file_dirname, $oss_file_name);
return $res['url'];
}
function batchDownImg($zip_name, $img_array)
{
$zip_path = 'zip/' . $zip_name . '.zip';
$zip = new \ZipArchive();
$zip->open($zip_path, \ZipArchive::CREATE); //打开压缩包
$img_path = [];
foreach ($img_array as $key => $value) {
foreach ($value as $k => $val) {
$file_suffix = explode('.', $val)[count(explode('.', $val)) - 1];
$local_file = saveFileToLocal($val);
$img_path[] = $local_file['local_file'];
$zip->addFile($local_file['local_file'], $key . '/' . $k . '.' . $file_suffix);
}
}
$zip->close();
if (!file_exists($zip_path)) {
header('HTTP/1.1 404 NOT FOUND');
} else {
$down_file = './' . $zip_path;
// 下载文件
ob_clean();
header('Pragma: public');
header('Last-Modified:' . gmdate('D, d M Y H:i:s') . 'GMT');
header('Cache-Control:no-store, no-cache, must-revalidate');
header('Cache-Control:pre-check=0, post-check=0, max-age=0');
header('Content-Transfer-Encoding:binary');
header('Content-Encoding:none');
header('Content-type:multipart/form-data');
header('Content-Disposition:attachment; filename="' . $zip_name . '.zip"'); //设置下载的默认文件名
header('Content-length:' . filesize($down_file));
$fp = fopen($down_file, 'r');
while (connection_status() == 0 && $buf = @fread($fp, 8192)) {
echo $buf;
}
fclose($fp);
@unlink($down_file);
@flush();
@ob_flush();
foreach ($img_path as $k => $val) {
@unlink($val);
}
exit();
}
}
/**
* 音频amr格式转mp3格式
* @param string $amr_file_path AMR格式文件地址
* @param string $mp3_file_path MP3格式文件地址
* @date 2022-08-15
*/
function amr_to_mp3($amr_file_path, $mp3_file_path)
{
// 执行命令ffmpeg
$str = "ffmpeg -y -i " . $amr_file_path . " " . $mp3_file_path;
exec($str);
return $mp3_file_path;
}
/**
* 将网络图片保存到服务器
* @param string $file_url 网络图片地址
* @param string $local_file_dirname 本地图片文件夹
* @param string $local_file_name 本地图片名称
* @date 2022-08-26
*/
function file_save_to_server($file_url, $local_file_dirname = '', $local_file_name = '')
{
// 默认存储目录
if (empty($local_file_dirname)) {
$local_file_dirname = './temp';
}
// 默认存储名称 当前微秒数md5加密+原文件后缀
if (empty($local_file_name)) {
// 文件名为当前微秒加密
$local_file_name = md5(microtime(true));
// 获取后缀名
$suffix = get_suffix($file_url);
// 文件名
$local_file_name .= '.' . $suffix;
}
// 本地存储路径
$local_file_path = $local_file_dirname . '/' . $local_file_name;
// 文件夹不存在时,先生成文件夹
create_directory($local_file_path);
// 将网络图片保存到服务器
$file = file_get_contents($file_url);
file_put_contents($local_file_path, $file);
// 返回本地路径
return $local_file_path;
}
/**
* 网络图片转base64
* @param string $img
* @param bool $imgHtmlCode
* @date 2022-11-23
*/
function image_to_base64($img = '', $imgHtmlCode = true)
{
// $base64 = "" . chunk_split(base64_encode(file_get_contents($img)));
if ($imgHtmlCode) {
$imageInfo = getimagesize($img);
return 'data:' . $imageInfo['mime'] . ';base64,' . str_replace(array("\r\n", "\r", "\n"), '', chunk_split(base64_encode(file_get_contents($img))));
} else {
return str_replace(array("\r\n", "\r", "\n"), '', chunk_split(base64_encode(file_get_contents($img))));
}
// $img = file_get_contents($file);
//
// $img_info = getimagesize($file);
//
// $file_content = base64_encode($img);
////判读图片类型
// switch ($img_info[2]) {
// case 1:
// $img_type = "gif";
// break;
// case 2:
// $img_type = "jpg";
// break;
// case 3:
// $img_type = "png";
// break;
// }
//
//// 显示输出
// $img_base64 = 'data:image/' . $img_type . ';base64,' . $file_content;//合成图片的base64编码
// return $img_base64;
}
/**
* 获取视频第一帧截图
* @param string $url 视频位置OSS
* @date 2021-06-24
*/
function get_oss_video_screenshot($file_url, $time = 0)
{
$uid = defined('UID') ? UID : 2;
$oss_param = get_ali_oss_config($uid);
$oss_class = new \ali\oss\Oss($oss_param);
// 下载OSS文件到服务器
$result = $oss_class->getObject($file_url);
// 服务器文件路径
$local_file_url = $result['local_file_url'];
// 视频第一帧路径
$screenshot_local_file_url = './temp/' . md5(microtime(true)) . '_img.jpg';
// 利用服务器ffmpeg命令截取视频帧数
$result = get_video_screenshot_by_ffmpeg($local_file_url, $screenshot_local_file_url, $time);
// 将图片存储到OSS
$oss_file_dirname = 'uid' . $uid . '/video_screenshot';
$res = $oss_class->uploadFile($screenshot_local_file_url, $oss_file_dirname);
// 删除服务器视频文件
unlink($local_file_url);
return $res;
}
/**
* 获取COS的视频第一帧截图
* @param string $url 视频位置OSS
* @date 2022-12-08
*/
function get_cos_video_screenshot($file_url, $time = 0)
{
$uid = defined('UID') ? UID : 2;
$cos_param = get_tencent_cos_config($uid);
$file_class = new \tencent\cos\object\File($cos_param);
// 下载COS文件到服务器
$result = $file_class->download($file_url);
// 服务器文件路径
$local_file_url = $result['local_file_url'];
// 视频截帧图片存储路径
$screenshot_local_file_url = './temp/' . md5(microtime(true)) . '_img.jpg';
// 利用服务器ffmpeg命令截取视频帧数
$result = get_video_screenshot_by_ffmpeg($local_file_url, $screenshot_local_file_url, $time);
// 将图片存储到COS
$oss_file_dirname = 'uid' . $uid . '/video_screenshot';
$result = $file_class->uploadFile($screenshot_local_file_url, $oss_file_dirname);
// 删除服务器视频文件
unlink($local_file_url);
return $result;
}