Browse Source

jpg resize improved, add quality config

master
filesite 2 months ago
parent
commit
bc932b4b12
  1. 3
      conf/app.php
  2. 2
      plugins/Html.php
  3. 96
      themes/beauty/controller/M3u8Controller.php
  4. 6
      themes/beauty/controller/SiteController.php

3
conf/app.php

@ -3,7 +3,7 @@
* Config * Config
*/ */
$configs = array( $configs = array(
'version' => '0.3.0', 'version' => '0.3.1',
'releaseDate' => '2024-9-28', 'releaseDate' => '2024-9-28',
'showVersion' => false, //默认不显示版本号和发布日期 'showVersion' => false, //默认不显示版本号和发布日期
@ -62,6 +62,7 @@ $configs = array(
'enableSmallImage' => true, //列表页面是否开启缩略图,true 为显示缩略图,false 则显示原图 'enableSmallImage' => true, //列表页面是否开启缩略图,true 为显示缩略图,false 则显示原图
'enableSmallImageForWan' => false, //外网使用时,点击图片打开fancybox时是否显示缩略图:true 显示缩略图, false 则显示原图 'enableSmallImageForWan' => false, //外网使用时,点击图片打开fancybox时是否显示缩略图:true 显示缩略图, false 则显示原图
'smallImageQuality' => 95, //缩略图压缩比率,0 - 100,数字越大,清晰度越高,系统默认:95
//关闭服务器端生成缩略图,如果在cpu性能较低的设备(如路由器)里运行,开启此配置可以减少cpu消耗 //关闭服务器端生成缩略图,如果在cpu性能较低的设备(如路由器)里运行,开启此配置可以减少cpu消耗
//如果在外网运行,开启此配置,则可能会因为服务器带宽较小导致图片加载缓慢 //如果在外网运行,开启此配置,则可能会因为服务器带宽较小导致图片加载缓慢

2
plugins/Html.php

@ -297,7 +297,7 @@ eof;
if (!empty($files)) { if (!empty($files)) {
foreach($files as $item) { foreach($files as $item) {
if ($item['filename'] == $filename && in_array($item['extension'], $imgExts)) { if (!empty($item['filename']) && $item['filename'] == $filename && in_array($item['extension'], $imgExts)) {
$matchedImage = $item; $matchedImage = $item;
break; break;
} }

96
themes/beauty/controller/M3u8Controller.php

@ -8,11 +8,11 @@ require_once __DIR__ . '/../../../plugins/Common.php';
Class M3u8Controller extends Controller { Class M3u8Controller extends Controller {
//参数 //参数
//@id - 文件id //@id - 文件id
//@cid - 数据缓存id //@cid - 数据缓存id
//支持nginx secure防盗链:md5={$md5}&expires={$expires} //支持nginx secure防盗链:md5={$md5}&expires={$expires}
public function actionIndex() { public function actionIndex() {
$videoId = $this->get('id', ''); $videoId = $this->get('id', '');
$cacheParentDataId = $this->get('cid', ''); $cacheParentDataId = $this->get('cid', '');
if (empty($videoId) || empty($cacheParentDataId)) { if (empty($videoId) || empty($cacheParentDataId)) {
@ -31,17 +31,17 @@ Class M3u8Controller extends Controller {
} }
if (empty($cachedParentData[$videoId])) { if (empty($cachedParentData[$videoId])) {
$erro = "缓存数据中找不到当前视频,请返回上一页重新进入!"; $erro = "缓存数据中找不到当前视频,请返回上一页重新进入!";
throw new Exception($err, 404); throw new Exception($err, 404);
}else if (!empty($cachedParentData)) { }else if (!empty($cachedParentData)) {
$m3u8 = $cachedParentData[$videoId]; $m3u8 = $cachedParentData[$videoId];
$m3u8Content = $this->getM3u8Content($m3u8['realpath'], $cachedParentData); $m3u8Content = $this->getM3u8Content($m3u8['realpath'], $cachedParentData);
if (!empty($m3u8Content)) { if (!empty($m3u8Content)) {
return $this->renderM3u8($m3u8Content); return $this->renderM3u8($m3u8Content);
}else { }else {
$err = 'm3u8内容为空!'; $err = 'm3u8内容为空!';
throw new Exception($err, 500); throw new Exception($err, 500);
} }
} }
} }
@ -67,50 +67,50 @@ Class M3u8Controller extends Controller {
#EXT-X-ENDLIST #EXT-X-ENDLIST
**/ **/
protected function getM3u8Content($m3u8_realpath, $cachedParentData = array()) { protected function getM3u8Content($m3u8_realpath, $cachedParentData = array()) {
$m3u8Content = file_get_contents($m3u8_realpath); $m3u8Content = file_get_contents($m3u8_realpath);
if (empty($m3u8Content) || strpos($m3u8Content, 'EXTM3U') === false) { if (empty($m3u8Content) || strpos($m3u8Content, 'EXTM3U') === false) {
return false; return false;
} }
$lines = preg_split("/[\r\n]/", $m3u8Content); $lines = preg_split("/[\r\n]/", $m3u8Content);
$newContent = ''; $newContent = '';
foreach($lines as $index => $line) { foreach($lines as $index => $line) {
if (strpos($line, '.ts') !== false) { if (strpos($line, '.ts') !== false) {
$newContent .= $this->getRelativePathOfTs($line, $m3u8_realpath, $cachedParentData) . "\n"; $newContent .= $this->getRelativePathOfTs($line, $m3u8_realpath, $cachedParentData) . "\n";
}else if (!empty($line)) { }else if (!empty($line)) {
$newContent .= $line . "\n"; $newContent .= $line . "\n";
} }
} }
return $newContent; return $newContent;
} }
//返回ts相对当前m3u8文件的相对路径 //返回ts相对当前m3u8文件的相对路径
//TODO: 支持防盗链 //TODO: 支持防盗链
protected function getRelativePathOfTs($ts_filename, $m3u8_realpath, $cachedParentData = array()) { protected function getRelativePathOfTs($ts_filename, $m3u8_realpath, $cachedParentData = array()) {
if (!empty($cachedParentData)) { if (!empty($cachedParentData)) {
$matchedTs = null; $matchedTs = null;
foreach($cachedParentData as $item) { foreach($cachedParentData as $item) {
if ($item['extension'] == 'ts' && strpos($item['path'], $ts_filename) !== false) { if (!empty($item['extension']) && $item['extension'] == 'ts' && strpos($item['path'], $ts_filename) !== false) {
$matchedTs = $item; $matchedTs = $item;
break; break;
} }
} }
if (!empty($matchedTs)) { if (!empty($matchedTs)) {
return $matchedTs['path']; return $matchedTs['path'];
}else { }else {
$webroot = FSC::$app['config']['content_directory']; $webroot = FSC::$app['config']['content_directory'];
$rootDir = __DIR__ . '/../../../www/' . $webroot; $rootDir = __DIR__ . '/../../../www/' . $webroot;
$rootDir = realpath($rootDir); $rootDir = realpath($rootDir);
$m3u8Dir = dirname($m3u8_realpath); $m3u8Dir = dirname($m3u8_realpath);
$relativeDir = str_replace("{$rootDir}/", '', $m3u8Dir); $relativeDir = str_replace("{$rootDir}/", '', $m3u8Dir);
return "/{$webroot}{$relativeDir}/{$ts_filename}"; return "/{$webroot}{$relativeDir}/{$ts_filename}";
} }
} }
return dirname($m3u8_realpath) . "/{$ts_filename}"; return dirname($m3u8_realpath) . "/{$ts_filename}";
} }
} }

6
themes/beauty/controller/SiteController.php

@ -527,11 +527,11 @@ Class SiteController extends Controller {
//保存base64格式的缩略图到缓存文件 //保存base64格式的缩略图到缓存文件
if (!empty($imgSource)) { if (!empty($imgSource)) {
$dst_img = imagecreatetruecolor($width, $height); $dst_img = imagecreatetruecolor($width, $height);
$copy_done = imagecopyresized($dst_img, $imgSource, 0, 0, 0, 0, $width, $height, $naturalWidth, $naturalHeight); $copy_done = imagecopyresampled($dst_img, $imgSource, 0, 0, 0, 0, $width, $height, $naturalWidth, $naturalHeight);
if ($copy_done) { if ($copy_done) {
ob_start(); ob_start();
imagejpeg($dst_img); $quality = !empty(FSC::$app['config']['smallImageQuality']) ? FSC::$app['config']['smallImageQuality'] : 90;
imagejpeg($dst_img, null, $quality);
$img_data = ob_get_clean(); $img_data = ob_get_clean();
ob_end_clean(); ob_end_clean();
} }

Loading…
Cancel
Save