Skip to content

Commit

Permalink
perf: 支持上传mov、webm视频
Browse files Browse the repository at this point in the history
  • Loading branch information
kuaifan committed Aug 6, 2024
1 parent ee6eddf commit 18eaf56
Showing 1 changed file with 62 additions and 10 deletions.
72 changes: 62 additions & 10 deletions app/Module/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,38 @@ public static function rightDelete($string, $find, $lower = false)
return $string;
}

/**
* 替换开头指定字符串
* @param $string
* @param $find
* @param $replace
* @param $lower
* @return mixed|string
*/
public static function leftReplace($string, $find, $replace, $lower = false)
{
if (Base::leftExists($string, $find, $lower)) {
$string = $replace . substr($string, strlen($find));
}
return $string;
}

/**
* 替换结尾指定字符串
* @param $string
* @param $find
* @param $replace
* @param $lower
* @return mixed|string
*/
public static function rightReplace($string, $find, $replace, $lower = false)
{
if (Base::rightExists($string, $find, $lower)) {
$string = substr($string, 0, strlen($find) * -1) . $replace;
}
return $string;
}

/**
* 截取指定字符串
* @param $str
Expand Down Expand Up @@ -2218,7 +2250,7 @@ public static function upload($param)
$type = ['jpg', 'jpeg', 'webp', 'gif', 'png'];
break;
case 'video':
$type = ['rm', 'rmvb', 'wmv', 'avi', 'mpg', 'mpeg', 'mp4', 'webm'];
$type = ['rm', 'rmvb', 'wmv', 'avi', 'mpg', 'mpeg', 'mp4', 'mov', 'webm'];
break;
case 'audio':
$type = ['mp3', 'wma', 'wav', 'amr'];
Expand All @@ -2233,7 +2265,7 @@ public static function upload($param)
$type = ['zip'];
break;
case 'file':
$type = ['jpg', 'jpeg', 'webp', 'png', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'esp', 'pdf', 'rar', 'zip', 'gz', 'ai', 'avi', 'bmp', 'cdr', 'eps', 'mov', 'mp3', 'mp4', 'webm', 'pr', 'psd', 'svg', 'tif'];
$type = ['jpg', 'jpeg', 'webp', 'png', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'esp', 'pdf', 'rar', 'zip', 'gz', 'ai', 'avi', 'bmp', 'cdr', 'eps', 'mp3', 'mp4', 'mov', 'webm', 'pr', 'psd', 'svg', 'tif'];
break;
case 'firmware':
$type = ['img', 'tar', 'bin'];
Expand Down Expand Up @@ -2304,7 +2336,7 @@ public static function upload($param)
}
@chmod($array['file'], $chmod);
//iOS照片颠倒处理
if (in_array($extension, ['jpg', 'jpeg']) && function_exists('exif_read_data')) {
if (in_array($array['ext'], ['jpg', 'jpeg']) && function_exists('exif_read_data')) {
$data = imagecreatefromstring(file_get_contents($array['file']));
$exif = @exif_read_data($array['file']);
if (!empty($exif['Orientation'])) {
Expand All @@ -2320,18 +2352,38 @@ public static function upload($param)
}
}
//
if (in_array($extension, ['mp4', 'webm'])) {
if (in_array($array['ext'], ['mov', 'webm'])) {
// 转换视频格式
$output = Base::rightReplace($array['file'], ".{$array['ext']}", '.mp4');
if ($array['ext'] === 'webm') {
shell_exec("ffmpeg -y -i {$array['file']} -strict experimental {$output} 2>&1");
} else {
shell_exec("ffmpeg -y -i {$array['file']} -c:v copy -c:a copy {$output} 2>&1");
}
if (file_exists($output) && filesize($output) > 0) {
@unlink($array['file']);
$array = array_merge($array, [
"name" => Base::rightReplace($array['name'], ".{$array['ext']}", '.mp4'),
"size" => Base::twoFloat(filesize($output) / 1024, true),
"file" => $output,
"path" => Base::rightReplace($array['path'], ".{$array['ext']}", '.mp4'),
"url" => Base::rightReplace($array['url'], ".{$array['ext']}", '.mp4'),
"ext" => 'mp4',
]);
}
}
if ($array['ext'] == 'mp4') {
// 视频尺寸
$thumbFile = $array['file'] . '_thumb.jpg';
shell_exec("ffmpeg -i {$array['file']} -ss 1 -vframes 1 {$thumbFile} 2>&1");
if (file_exists($thumbFile)) {
shell_exec("ffmpeg -y -i {$array['file']} -ss 1 -vframes 1 {$thumbFile} 2>&1");
if (file_exists($thumbFile) && filesize($thumbFile) > 0) {
$paramet = getimagesize($thumbFile);
$array['width'] = $paramet[0];
$array['height'] = $paramet[1];
$array['thumb'] = $array['path'] . '_thumb.jpg';
}
}
if (in_array($extension, ['jpg', 'jpeg', 'webp', 'gif', 'png'])) {
if (in_array($array['ext'], ['jpg', 'jpeg', 'webp', 'gif', 'png'])) {
//图片尺寸
$paramet = getimagesize($array['file']);
$array['width'] = $paramet[0];
Expand Down Expand Up @@ -2370,12 +2422,12 @@ public static function upload($param)
}
//生成缩略图
$array['thumb'] = $array['path'];
if ($extension === 'gif' && !isset($param['autoThumb'])) {
if ($array['ext'] === 'gif' && !isset($param['autoThumb'])) {
$param['autoThumb'] = false;
}
if ($param['autoThumb'] !== false) {
if ($extension = Image::thumbImage($array['file'], $array['file'] . "_thumb.{*}", 320, 0)) {
$array['thumb'] .= "_thumb.{$extension}";
if ($array['ext'] = Image::thumbImage($array['file'], $array['file'] . "_thumb.{*}", 320, 0)) {
$array['thumb'] .= "_thumb.{$array['ext']}";
}
}
$array['thumb'] = Base::fillUrl($array['thumb']);
Expand Down

0 comments on commit 18eaf56

Please sign in to comment.