- 新增阿里云OSS SDK核心类库文件 - 新增OSS插件配置文件及钩子监听 - 新增分类图标SVG资源并优化为轻量化格式 - 替换定位接口为天地图逆地理编码接口 - 新增OSS图片缩略处理工具方法
283 行
9.8 KiB
PHP
283 行
9.8 KiB
PHP
<?php
|
||
/**
|
||
* 全自动生成符合 2.5:1 (750x300) 尺寸的标准全景轮播图并推流阿里云 OSS 入库
|
||
*
|
||
* @return void
|
||
*/
|
||
|
||
set_time_limit(0);
|
||
ini_set('memory_limit', '512M');
|
||
|
||
define('DS', DIRECTORY_SEPARATOR);
|
||
define('ROOT_PATH', dirname(__DIR__) . DS);
|
||
define('APP_PATH', ROOT_PATH . 'application' . DS);
|
||
|
||
require_once ROOT_PATH . 'thinkphp/base.php';
|
||
\think\Loader::addNamespace('OSS', ROOT_PATH . 'addons' . DS . 'alioss' . DS . 'library' . DS . 'OSS' . DS);
|
||
|
||
use OSS\OssClient;
|
||
use OSS\Core\OssException;
|
||
use OSS\Credentials\StaticCredentialsProvider;
|
||
|
||
$apiKey = 'sk-60nh0N9F0qMBbrCmhEg37b0SPngQtflvwNSF2h0KfjC9AwHM';
|
||
$apiUrl = 'https://geeknow.ai/v1/images/generations';
|
||
$bannerTmpDir = dirname(__DIR__) . '/tmp_img/banners';
|
||
if (!is_dir($bannerTmpDir)) {
|
||
@mkdir($bannerTmpDir, 0755, true);
|
||
}
|
||
|
||
// 4 张核心轮播图规划
|
||
$bannersConfig = [
|
||
[
|
||
'title' => '源头直采 · 绿色新鲜直达餐桌',
|
||
'link' => '/pages/category/index',
|
||
'color' => 'linear-gradient(105deg, #00e070 0%, #00c860 42%, #046b3b 100%)',
|
||
'prompt' => 'a panoramic wide banner of sunny organic green orchard and vegetable farm, fresh harvest baskets on rustic table, bright fresh mint green mood, realistic photography, wide shot, no text, no watermark',
|
||
'existing_url' => 'https://r2.geeknow.ai/chatgpt2api/images/2026/11/20/1795140076_e8c409ec28f2461abd29e219ecac0d8d.png'
|
||
],
|
||
[
|
||
'title' => '本地便民 · 专业家政维修极速响应',
|
||
'link' => '/pages/category/channel?categoryId=3',
|
||
'color' => 'linear-gradient(105deg, #00d26a 0%, #00b359 45%, #056636 100%)',
|
||
'prompt' => 'a panoramic wide banner of a modern bright cozy living room with large sunny windows and indoor green plants, spotless clean organized home, fresh mint green mood, realistic interior photography, wide horizontal banner shot, no text, no watermark',
|
||
'existing_url' => null
|
||
],
|
||
[
|
||
'title' => '同城好物 · 闲置流转与房产租售优选',
|
||
'link' => '/pages/category/channel?categoryId=2',
|
||
'color' => 'linear-gradient(105deg, #00e070 0%, #00bc5e 50%, #086b3e 100%)',
|
||
'prompt' => 'a panoramic wide banner of a stylish minimalist modern lifestyle space, warm wood table with books coffee and plants, sunny daylight streaming through window, fresh mint green accents, high quality photography, wide horizontal banner shot, no text, no watermark',
|
||
'existing_url' => null
|
||
],
|
||
[
|
||
'title' => '同城发布 · 免费发帖高效对接买家',
|
||
'link' => '/pages/post/create',
|
||
'color' => 'linear-gradient(105deg, #00d26a 0%, #00a854 50%, #03522b 100%)',
|
||
'prompt' => 'a panoramic wide banner overlooking a picturesque modern suburban eco-town with lush green trees, blue sky with soft white clouds, gentle morning sunlight, refreshing light mint green tone, aerial scenic photography, wide horizontal banner shot, no text, no watermark',
|
||
'existing_url' => null
|
||
]
|
||
];
|
||
|
||
/**
|
||
* 调用 GeekNow 生成 1792x1024 宽幅横版图片
|
||
*
|
||
* @param string $prompt
|
||
* @param string $apiKey
|
||
* @param string $apiUrl
|
||
* @return string|false
|
||
*/
|
||
function callGeekNowBanner($prompt, $apiKey, $apiUrl)
|
||
{
|
||
$body = [
|
||
'model' => 'gpt-image-2.6-flare',
|
||
'prompt' => $prompt,
|
||
'n' => 1,
|
||
'size' => '1792x1024',
|
||
'quality' => 'auto',
|
||
'watermark' => false,
|
||
'response_format' => 'url'
|
||
];
|
||
|
||
for ($i = 0; $i < 2; $i++) {
|
||
$ch = curl_init($apiUrl);
|
||
curl_setopt_array($ch, [
|
||
CURLOPT_POST => true,
|
||
CURLOPT_POSTFIELDS => json_encode($body, JSON_UNESCAPED_UNICODE),
|
||
CURLOPT_HTTPHEADER => [
|
||
'Authorization: Bearer ' . $apiKey,
|
||
'Content-Type: application/json'
|
||
],
|
||
CURLOPT_RETURNTRANSFER => true,
|
||
CURLOPT_TIMEOUT => 90
|
||
]);
|
||
$resp = curl_exec($ch);
|
||
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||
curl_close($ch);
|
||
|
||
if ($code === 200) {
|
||
$json = json_decode($resp, true);
|
||
$url = $json['data'][0]['url'] ?? false;
|
||
if ($url) return $url;
|
||
}
|
||
sleep(2);
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* 智能居中裁剪并等比缩放为 750x300 (2.5:1) 标准 Banner
|
||
*
|
||
* @param string $srcFile
|
||
* @param string $destFile
|
||
* @param int $targetW
|
||
* @param int $targetH
|
||
* @return bool
|
||
*/
|
||
function cropAndResizeBanner($srcFile, $destFile, $targetW = 750, $targetH = 300)
|
||
{
|
||
$imgInfo = @getimagesize($srcFile);
|
||
if (!$imgInfo) return false;
|
||
|
||
$mime = $imgInfo['mime'] ?? '';
|
||
if ($mime === 'image/png') {
|
||
$srcImg = @imagecreatefrompng($srcFile);
|
||
} else {
|
||
$srcImg = @imagecreatefromjpeg($srcFile);
|
||
}
|
||
if (!$srcImg) return false;
|
||
|
||
$srcW = imagesx($srcImg);
|
||
$srcH = imagesy($srcImg);
|
||
$targetRatio = $targetW / $targetH;
|
||
$srcRatio = $srcW / $srcH;
|
||
|
||
if ($srcRatio > $targetRatio) {
|
||
$cropH = $srcH;
|
||
$cropW = (int)($srcH * $targetRatio);
|
||
$cropX = (int)(($srcW - $cropW) / 2);
|
||
$cropY = 0;
|
||
} else {
|
||
$cropW = $srcW;
|
||
$cropH = (int)($srcW / $targetRatio);
|
||
$cropX = 0;
|
||
$cropY = (int)(($srcH - $cropH) / 2);
|
||
}
|
||
|
||
$dstImg = imagecreatetruecolor($targetW, $targetH);
|
||
imagecopyresampled($dstImg, $srcImg, 0, 0, $cropX, $cropY, $targetW, $targetH, $cropW, $cropH);
|
||
|
||
$ok = imagejpeg($dstImg, $destFile, 90);
|
||
imagedestroy($srcImg);
|
||
imagedestroy($dstImg);
|
||
|
||
return ($ok && file_exists($destFile) && filesize($destFile) > 10000);
|
||
}
|
||
|
||
// 初始化数据库
|
||
$pdo = new PDO('mysql:host=127.0.0.1;port=3307;dbname=fenleixinxi;charset=utf8mb4', 'fenleixinxi', 'fenleixinxi', [
|
||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
|
||
]);
|
||
|
||
// 初始化 OSS
|
||
$accessKeyId = 'LTAI5t7S47cwfaK1vo87VxCZ';
|
||
$accessKeySecret = 'LJzowSI57d0NvmnL40d5df3TZJVRNh';
|
||
$endpoint = 'oss-cn-shenzhen.aliyuncs.com';
|
||
$bucket = 'tjxc-xinxi';
|
||
|
||
$oss = new OssClient([
|
||
'accessKeyId' => $accessKeyId,
|
||
'endpoint' => $endpoint,
|
||
'bucket' => $bucket,
|
||
'region' => 'cn-shenzhen',
|
||
'provider' => new StaticCredentialsProvider($accessKeyId, $accessKeySecret),
|
||
'signatureVersion' => OssClient::OSS_SIGNATURE_VERSION_V4,
|
||
]);
|
||
|
||
echo "========================================================\n";
|
||
echo " 重新配置 2.5:1 (750x300) 全景高标准轮播图流水线\n";
|
||
echo "========================================================\n\n";
|
||
|
||
// 清理旧轮播图
|
||
$pdo->exec("DELETE FROM fa_fc_banner");
|
||
|
||
$today = date('Ymd');
|
||
$publicUploadsDir = ROOT_PATH . 'public/uploads/' . $today;
|
||
if (!is_dir($publicUploadsDir)) {
|
||
@mkdir($publicUploadsDir, 0755, true);
|
||
}
|
||
|
||
foreach ($bannersConfig as $idx => $b) {
|
||
$num = $idx + 1;
|
||
echo "[$num/4] 处理: {$b['title']}...\n";
|
||
|
||
$rawUrl = $b['existing_url'];
|
||
if (!$rawUrl) {
|
||
echo " -> 正在通过 GeekNow 生成 1792x1024 宽幅全景图 (@ 0.04元)...";
|
||
$rawUrl = callGeekNowBanner($b['prompt'], $apiKey, $apiUrl);
|
||
if (!$rawUrl) {
|
||
echo " [生成失败! 跳过]\n";
|
||
continue;
|
||
}
|
||
echo " [生成成功]\n";
|
||
} else {
|
||
echo " -> 使用已生成的 1792x1024 原图素材\n";
|
||
}
|
||
|
||
$rawFile = $bannerTmpDir . "/raw_banner_{$num}.png";
|
||
$croppedFile = $bannerTmpDir . "/banner_{$num}_750x300.jpg";
|
||
|
||
echo " -> 正在下载高清原图...";
|
||
file_put_contents($rawFile, file_get_contents($rawUrl));
|
||
echo " (" . round(filesize($rawFile)/1024) . " KB)\n";
|
||
|
||
echo " -> 正在智能居中裁剪为标准 750x300 (2.5:1) 轮播图...";
|
||
if (!cropAndResizeBanner($rawFile, $croppedFile, 750, 300)) {
|
||
echo " [裁剪失败! 跳过]\n";
|
||
continue;
|
||
}
|
||
$szKb = round(filesize($croppedFile)/1024);
|
||
echo " [成功: {$szKb} KB]\n";
|
||
|
||
// 计算 md5 并推流 OSS
|
||
$md5 = md5_file($croppedFile);
|
||
$sha1 = sha1_file($croppedFile);
|
||
$saveName = $md5 . '.jpg';
|
||
$ossKey = 'uploads/' . $today . '/' . $saveName;
|
||
$urlPath = '/' . $ossKey;
|
||
|
||
// 同时保存在 public/uploads/ 本地作为高可用本地副本
|
||
$localFile = $publicUploadsDir . '/' . $saveName;
|
||
copy($croppedFile, $localFile);
|
||
|
||
echo " -> 正在推流至阿里云 OSS ($ossKey)...";
|
||
$ossOk = true;
|
||
try {
|
||
$oss->uploadFile($bucket, $ossKey, $croppedFile);
|
||
echo " [OSS推送成功]\n";
|
||
} catch (\Exception $e) {
|
||
$ossOk = false;
|
||
echo " [OSS暂时提示: " . $e->getMessage() . ",使用本地高可用路径]\n";
|
||
}
|
||
|
||
$now = time();
|
||
$storage = $ossOk ? 'alioss' : 'local';
|
||
|
||
// 写入 fa_attachment
|
||
$stmtAtt = $pdo->prepare("
|
||
INSERT INTO fa_attachment
|
||
(admin_id, user_id, filename, filesize, imagewidth, imageheight, imagetype, mimetype, url, uploadtime, storage, sha1)
|
||
VALUES
|
||
(1, 0, :filename, :filesize, 750, 300, 'jpg', 'image/jpeg', :url, :uploadtime, :storage, :sha1)
|
||
");
|
||
$stmtAtt->execute([
|
||
':filename' => mb_substr($b['title'] . '_轮播图', 0, 100),
|
||
':filesize' => filesize($croppedFile),
|
||
':url' => $urlPath,
|
||
':uploadtime' => $now,
|
||
':storage' => $storage,
|
||
':sha1' => $sha1
|
||
]);
|
||
|
||
// 写入 fa_fc_banner
|
||
$stmtBan = $pdo->prepare("
|
||
INSERT INTO fa_fc_banner
|
||
(title, image, link, color, sort, enabled, createtime, updatetime)
|
||
VALUES
|
||
(:title, :image, :link, :color, :sort, 1, :createtime, :updatetime)
|
||
");
|
||
$stmtBan->execute([
|
||
':title' => $b['title'],
|
||
':image' => $urlPath,
|
||
':link' => $b['link'],
|
||
':color' => $b['color'],
|
||
':sort' => $num,
|
||
':createtime' => $now,
|
||
':updatetime' => $now
|
||
]);
|
||
|
||
echo " -> 已成功入库 fa_fc_banner (#{$num})!\n\n";
|
||
}
|
||
|
||
echo "🎉🎉 全部标准尺寸轮播图生成、裁剪与配置入库完成!\n";
|