文件
yjyf/scripts/prepare_goods_images.php
MiAn 5908cf6b16 chore(front): 删除静态商品图片及相关同步脚本逻辑
- 删除多个商品静态图片文件
- 移除同步脚本中对app静态目录的引用和复制逻辑
- 不再将商品图片同步至yjyf-app静态目录
2026-11-10 17:32:40 +08:00

128 行
7.1 KiB
PHP

<?php
define('APP_PATH', __DIR__ . '/../server/application/');
require __DIR__ . '/../server/thinkphp/base.php';
\think\App::initCommon();
use think\Loader;
Loader::addNamespace('Qcloud\Cos', __DIR__ . '/../server/addons/cos/library/Qcloud/Cos/');
Loader::addNamespace('GuzzleHttp\Command', __DIR__ . '/../server/addons/cos/library/Guzzle/command/src/');
Loader::addNamespace('GuzzleHttp\Command\Guzzle', __DIR__ . '/../server/addons/cos/library/Guzzle/guzzle-services/src/');
Loader::addNamespace('GuzzleHttp\UriTemplate', __DIR__ . '/../server/addons/cos/library/Guzzle/uri-template/src/');
$cosClient = new \Qcloud\Cos\Client([
'region' => 'ap-guangzhou',
'schema' => 'https',
'credentials' => [
'secretId' => 'AKIDxOo34sUu892lWFoEnFVYS9A58D84pa8X',
'secretKey' => 'IMDwjS5Xpwtc9WiV7dz95rTvHUbYJtSv'
]
]);
$localDir = __DIR__ . '/../server/public/uploads/goods';
if (!is_dir($localDir)) {
mkdir($localDir, 0777, true);
}
$images = [
'sofa_leather' => 'https://images.unsplash.com/photo-1555041469-a586c61ea9bc?w=600&auto=format&fit=crop&q=80',
'wardrobe_sliding' => 'https://images.unsplash.com/photo-1595428774223-ef52624120d2?w=600&auto=format&fit=crop&q=80',
'bed_luxury' => 'https://images.unsplash.com/photo-1505693416388-ac5ce068fe85?w=600&auto=format&fit=crop&q=80',
'dining_table' => 'https://images.unsplash.com/photo-1617806118233-18e1de247200?w=600&auto=format&fit=crop&q=80',
'toilet_smart' => 'https://images.unsplash.com/photo-1584622650111-993a426fbf0a?w=600&auto=format&fit=crop&q=80',
'shower_thermo' => 'https://images.unsplash.com/photo-1620626011761-996317b8d101?w=600&auto=format&fit=crop&q=80',
'bathroom_cabinet' => 'https://images.unsplash.com/photo-1552321554-5fefe8c9ef14?w=600&auto=format&fit=crop&q=80',
'ceiling_lamp' => 'https://images.unsplash.com/photo-1513506003901-1e6a229e2d15?w=600&auto=format&fit=crop&q=80',
'chandelier' => 'https://images.unsplash.com/photo-1540932239986-30128078f3c5?w=600&auto=format&fit=crop&q=80',
'smart_lock' => 'https://images.unsplash.com/photo-1558002038-1055907df827?w=600&auto=format&fit=crop&q=80',
'mechanical_lock' => 'https://images.unsplash.com/photo-1517487881594-2787fef5ebf7?w=600&auto=format&fit=crop&q=80',
'curtain_fabric' => 'https://images.unsplash.com/photo-1513694203232-719a280e022f?w=600&auto=format&fit=crop&q=80',
'curtain_motor' => 'https://images.unsplash.com/photo-1505691938895-1758d7feb511?w=600&auto=format&fit=crop&q=80',
'wall_decor' => 'https://images.unsplash.com/photo-1579783900882-c0d3dad7b119?w=600&auto=format&fit=crop&q=80',
'water_purifier' => 'https://images.unsplash.com/photo-1585771724684-38269d6639fd?w=600&auto=format&fit=crop&q=80',
'clothes_hanger' => 'https://images.unsplash.com/photo-1582735689369-4fe89db7114c?w=600&auto=format&fit=crop&q=80',
'wood_door' => 'https://images.unsplash.com/photo-1534349762230-e0cadf78f5da?w=600&auto=format&fit=crop&q=80',
'security_door' => 'https://images.unsplash.com/photo-1509644851169-2acc08aa25b5?w=600&auto=format&fit=crop&q=80',
'washing_machine' => 'https://images.unsplash.com/photo-1626806787461-102c1bfaaea1?w=600&auto=format&fit=crop&q=80',
'air_conditioner' => 'https://images.unsplash.com/photo-1621905251189-08b45d6a269e?w=600&auto=format&fit=crop&q=80',
'refrigerator' => 'https://images.unsplash.com/photo-1584992236310-6edddc08acff?w=600&auto=format&fit=crop&q=80',
'kitchen_hood' => 'https://images.unsplash.com/photo-1556911220-e15b29be8c8f?w=600&auto=format&fit=crop&q=80',
'water_heater' => 'https://images.unsplash.com/photo-1584622781564-1d987f7333c1?w=600&auto=format&fit=crop&q=80',
'tv_mounted' => 'https://images.unsplash.com/photo-1593359677879-a4bb92f829d1?w=600&auto=format&fit=crop&q=80',
'projector' => 'https://images.unsplash.com/photo-1517604931442-7e0c8ed2963c?w=600&auto=format&fit=crop&q=80',
'shower_room' => 'https://images.unsplash.com/photo-1600585154526-990dced4db0d?w=600&auto=format&fit=crop&q=80',
'commercial_stove' => 'https://images.unsplash.com/photo-1556912172-45b7abe8b7e1?w=600&auto=format&fit=crop&q=80',
'commercial_hood' => 'https://images.unsplash.com/photo-1590794056226-79ef3a8147e1?w=600&auto=format&fit=crop&q=80',
'commercial_steamer' => 'https://images.unsplash.com/photo-1544025162-d76694265947?w=600&auto=format&fit=crop&q=80',
'treadmill' => 'https://images.unsplash.com/photo-1576678927484-cc907957088c?w=600&auto=format&fit=crop&q=80',
'exercise_bike' => 'https://images.unsplash.com/photo-1594737625785-a6cbdabd333c?w=600&auto=format&fit=crop&q=80'
];
$resultUrls = [];
foreach ($images as $name => $url) {
$filename = $name . '.jpg';
$localFile = $localDir . '/' . $filename;
$appFile = $appStaticDir . '/' . $filename;
$cosKey = 'goods/' . $filename;
echo "Processing {$name}... ";
$data = null;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200 && strlen($data) > 1000) {
file_put_contents($localFile, $data);
echo "downloaded (" . strlen($data) . " bytes). ";
} else {
echo "download failed ({$httpCode}), using existing or placeholder. ";
if (!file_exists($localFile)) {
// 生成备用图片
$im = imagecreatetruecolor(600, 450);
$bg = imagecolorallocate($im, 245, 246, 248);
$primary = imagecolorallocate($im, 240, 113, 13);
imagefill($im, 0, 0, $bg);
imagerectangle($im, 10, 10, 589, 439, $primary);
imagejpeg($im, $localFile, 90);
imagedestroy($im);
}
$data = file_get_contents($localFile);
}
// 上传到 COS
try {
$cosRes = $cosClient->putObject([
'Bucket' => 'yjyf-1512102519',
'Key' => $cosKey,
'Body' => $data,
'ContentType' => 'image/jpeg'
]);
$cosUrl = 'https://yjyf-1512102519.cos.ap-guangzhou.myqcloud.com/' . $cosKey;
$resultUrls[$name] = [
'cos' => $cosUrl,
'local' => '/uploads/goods/' . $filename,
'static' => '/static/goods/' . $filename
];
echo "Uploaded to COS: {$cosUrl}\n";
} catch (\Throwable $e) {
echo "COS upload error: " . $e->getMessage() . "\n";
$resultUrls[$name] = [
'cos' => '/uploads/goods/' . $filename,
'local' => '/uploads/goods/' . $filename,
'static' => '/static/goods/' . $filename
];
}
}
file_put_contents(__DIR__ . '/goods_images_map.json', json_encode($resultUrls, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
echo "\nDone! Saved to goods_images_map.json\n";