- 新增阿里云OSS SDK核心类库文件 - 新增OSS插件配置文件及钩子监听 - 新增分类图标SVG资源并优化为轻量化格式 - 替换定位接口为天地图逆地理编码接口 - 新增OSS图片缩略处理工具方法
98 行
3.3 KiB
PHP
98 行
3.3 KiB
PHP
<?php
|
|
/**
|
|
* 将之前入库的 1MB+ 历史大图,全部在本地无损缩放为 240x240 (15KB~20KB) 并更新推送到 OSS
|
|
*
|
|
* @return void
|
|
*/
|
|
|
|
set_time_limit(0);
|
|
ini_set('memory_limit', '512M');
|
|
|
|
require_once dirname(__DIR__) . '/thinkphp/base.php';
|
|
\think\App::initCommon();
|
|
|
|
// 引入 alioss 插件依赖
|
|
\think\Loader::addNamespace('OSS', ROOT_PATH . 'addons' . DS . 'alioss' . DS . 'library' . DS . 'OSS' . DS);
|
|
|
|
$config = include ROOT_PATH . 'addons' . DS . 'alioss' . DS . 'config.php';
|
|
$aliossConfig = [];
|
|
foreach ($config as $item) {
|
|
if (isset($item['name']) && isset($item['value'])) {
|
|
$aliossConfig[$item['name']] = $item['value'];
|
|
}
|
|
}
|
|
|
|
$oss = new \OSS\OssClient([
|
|
'accessKeyId' => $aliossConfig['accessKeyId'],
|
|
'endpoint' => $aliossConfig['endpoint'],
|
|
'bucket' => $aliossConfig['bucket'],
|
|
'region' => str_replace('-internal', '', substr(explode('.', $aliossConfig['endpoint'])[0], 4)),
|
|
'provider' => new \OSS\Credentials\StaticCredentialsProvider($aliossConfig['accessKeyId'], $aliossConfig['accessKeySecret']),
|
|
'signatureVersion' => \OSS\OssClient::OSS_SIGNATURE_VERSION_V4,
|
|
]);
|
|
|
|
$pdo = new PDO('mysql:host=127.0.0.1;port=3307;dbname=fenleixinxi;charset=utf8mb4', 'fenleixinxi', 'fenleixinxi');
|
|
$cdn = rtrim($aliossConfig['cdnurl'] ?? 'https://tjxc-xinxi.oss-cn-shenzhen.aliyuncs.com', '/');
|
|
|
|
// 查询所有大于 100KB 的历史大图附件
|
|
$stmt = $pdo->query("SELECT id, url, filesize, imagewidth, imageheight FROM fa_attachment WHERE storage = 'alioss' AND filesize > 100000");
|
|
$list = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
echo "发现需要瘦身的历史大图附件: " . count($list) . " 个\n";
|
|
|
|
$tmpDir = ROOT_PATH . 'tmp_img' . DS . 'shrink_tmp';
|
|
if (!is_dir($tmpDir)) @mkdir($tmpDir, 0755, true);
|
|
|
|
$done = 0;
|
|
foreach ($list as $att) {
|
|
$ossKey = ltrim($att['url'], '/');
|
|
$imgUrl = $cdn . '/' . $ossKey;
|
|
|
|
$rawFile = $tmpDir . DS . 'raw.jpg';
|
|
$thumbFile = $tmpDir . DS . 'thumb.jpg';
|
|
|
|
// 1. 下载原图
|
|
$content = @file_get_contents($imgUrl);
|
|
if (!$content) continue;
|
|
file_put_contents($rawFile, $content);
|
|
|
|
// 2. 缩放到 240x240
|
|
$srcImg = @imagecreatefromstring($content);
|
|
if (!$srcImg) {
|
|
@unlink($rawFile);
|
|
continue;
|
|
}
|
|
$w = imagesx($srcImg);
|
|
$h = imagesy($srcImg);
|
|
$thumbImg = imagecreatetruecolor(240, 240);
|
|
imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, 240, 240, $w, $h);
|
|
imagejpeg($thumbImg, $thumbFile, 82);
|
|
|
|
imagedestroy($srcImg);
|
|
imagedestroy($thumbImg);
|
|
@unlink($rawFile);
|
|
|
|
$newSize = filesize($thumbFile);
|
|
if ($newSize < 1000) {
|
|
@unlink($thumbFile);
|
|
continue;
|
|
}
|
|
|
|
// 3. 覆盖推送回 OSS
|
|
try {
|
|
$oss->uploadFile($aliossConfig['bucket'], $ossKey, $thumbFile);
|
|
// 4. 更新附件表尺寸与大小
|
|
$upStmt = $pdo->prepare("UPDATE fa_attachment SET filesize = ?, imagewidth = 240, imageheight = 240 WHERE id = ?");
|
|
$upStmt->execute([$newSize, $att['id']]);
|
|
@unlink($thumbFile);
|
|
$done++;
|
|
if ($done % 20 === 0) {
|
|
echo "已成功瘦身覆盖 $done / " . count($list) . " 张 (当前大小约 " . round($newSize/1024) . "KB)\n";
|
|
}
|
|
} catch (\Exception $e) {
|
|
@unlink($thumbFile);
|
|
}
|
|
}
|
|
|
|
echo "历史大图瘦身完成!共成功处理: $done 张\n";
|