From 18eaf56ff9740c654182b0efd4fbd108c6ae47f8 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Tue, 6 Aug 2024 13:19:25 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=94=AF=E6=8C=81=E4=B8=8A=E4=BC=A0mov?= =?UTF-8?q?=E3=80=81webm=E8=A7=86=E9=A2=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Module/Base.php | 72 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 62 insertions(+), 10 deletions(-) diff --git a/app/Module/Base.php b/app/Module/Base.php index 2e4a47bd6..db805a4f0 100755 --- a/app/Module/Base.php +++ b/app/Module/Base.php @@ -1252,6 +1252,38 @@ class Base 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 @@ -2218,7 +2250,7 @@ class Base $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']; @@ -2233,7 +2265,7 @@ class Base $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']; @@ -2304,7 +2336,7 @@ class Base } @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'])) { @@ -2320,18 +2352,38 @@ class Base } } // - 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]; @@ -2370,12 +2422,12 @@ class Base } //生成缩略图 $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']);