文件
fenleixinxi/tools/download_and_crop_real_photos.php
MiAn f13fb6cdab feat: 完成二级分类真实实拍图替换与功能优化
本次提交内容包括:
1. 替换13个二级分类为真实商业实拍图,同步更新本地与线上数据库
2. 新增会员拨打额度耗尽引导开通会员功能
3. 优化发布页分类选择逻辑,增加会员权益展示
4. 修复分类名称显示问题,完善相关交互逻辑
5. 新增图片同步工具脚本,确保线上线下数据一致性
2026-10-28 14:13:32 +08:00

177 行
5.9 KiB
PHP

<?php
/**
* 下载真实商业实拍照片并居中裁剪为 240x240 JPEG (12KB~18KB),模拟后台真实上传
*/
$targetDir = __DIR__ . '/../public/uploads/20261028';
if (!is_dir($targetDir)) {
mkdir($targetDir, 0755, true);
}
// 13个子分类真实图源 (Unsplash 真实摄影图)
$realPhotos = [
// 招聘专区 (PID: 25)
[
'id' => 27,
'name' => '普工技工',
'filename' => 'job-tech.jpg',
'url' => 'https://images.unsplash.com/photo-1504917599217-d4dc5ebe6122?w=500&q=80&auto=format&fit=crop',
],
[
'id' => 28,
'name' => '餐饮服务',
'filename' => 'job-catering.jpg',
'url' => 'https://images.unsplash.com/photo-1577219491135-ce391730fb2c?w=500&q=80&auto=format&fit=crop',
],
[
'id' => 29,
'name' => '家政保洁',
'filename' => 'job-clean.jpg',
'url' => 'https://images.unsplash.com/photo-1581578731548-c64695cc6952?w=500&q=80&auto=format&fit=crop',
],
[
'id' => 30,
'name' => '骑手配送',
'filename' => 'job-rider.jpg',
'url' => 'https://images.unsplash.com/photo-1526367790999-0150786686a2?w=500&q=80&auto=format&fit=crop',
],
[
'id' => 31,
'name' => '销售客服',
'filename' => 'job-sales.jpg',
'url' => 'https://images.unsplash.com/photo-1534536281715-e28d76689b4d?w=500&q=80&auto=format&fit=crop',
],
[
'id' => 32,
'name' => '司机出行',
'filename' => 'job-driver.jpg',
'url' => 'https://images.unsplash.com/photo-1449965408869-eaa3f722e40d?w=500&q=80&auto=format&fit=crop',
],
[
'id' => 33,
'name' => '教育培训',
'filename' => 'job-edu.jpg',
'url' => 'https://images.unsplash.com/photo-1509062522246-3755977927d7?w=500&q=80&auto=format&fit=crop',
],
[
'id' => 34,
'name' => '兼职短工',
'filename' => 'job-parttime.jpg',
'url' => 'https://images.unsplash.com/photo-1586528116311-ad8dd3c8310d?w=500&q=80&auto=format&fit=crop',
],
// 肉蛋家禽 (PID: 26)
[
'id' => 35,
'name' => '鲜土鸡蛋',
'filename' => 'poultry-egg.jpg',
'url' => 'https://images.unsplash.com/photo-1582722872445-44dc5f7e3c8f?w=500&q=80&auto=format&fit=crop',
],
[
'id' => 36,
'name' => '散养土鸡',
'filename' => 'poultry-chicken.jpg',
'url' => 'https://images.unsplash.com/photo-1548550023-2bdb3c5beed7?w=500&q=80&auto=format&fit=crop',
],
[
'id' => 37,
'name' => '生鲜猪肉',
'filename' => 'poultry-pork.jpg',
'url' => 'https://images.unsplash.com/photo-1607623814075-e51df1bdc82f?w=500&q=80&auto=format&fit=crop',
],
[
'id' => 38,
'name' => '高山牛肉',
'filename' => 'poultry-beef.jpg',
'url' => 'https://images.unsplash.com/photo-1558030006-450675393462?w=500&q=80&auto=format&fit=crop',
],
[
'id' => 39,
'name' => '鲜活水产',
'filename' => 'poultry-fish.jpg',
'url' => 'https://images.unsplash.com/photo-1534043464124-3be32fe00099?w=500&q=80&auto=format&fit=crop',
],
];
/**
* 将图片内容居中裁剪为 240x240 并输出 JPEG
*
* @param string $binaryData 原始图片二进制流
* @param string $savePath 目标保存路径
* @return bool
*/
function cropToSquare240($binaryData, $savePath)
{
$src = @imagecreatefromstring($binaryData);
if (!$src) {
return false;
}
$srcW = imagesx($src);
$srcH = imagesy($src);
// 确定正方形裁剪区域(居中裁剪)
$cropSize = min($srcW, $srcH);
$cropX = (int)(($srcW - $cropSize) / 2);
$cropY = (int)(($srcH - $cropSize) / 2);
$dst = imagecreatetruecolor(240, 240);
// 高质量重采样
imagecopyresampled($dst, $src, 0, 0, $cropX, $cropY, 240, 240, $cropSize, $cropSize);
// 以 82% 质量保存 JPEG,保证实拍细节同时体积控制在 15KB 左右
$res = imagejpeg($dst, $savePath, 82);
imagedestroy($src);
imagedestroy($dst);
return $res;
}
echo "=== 开始获取真实摄影照片并裁剪为 240x240 实拍图 ===\n";
$results = [];
foreach ($realPhotos as $item) {
echo "正在下载 [{$item['name']}] 真实实拍照: {$item['url']} ...\n";
$opts = [
'http' => [
'method' => 'GET',
'header' => "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)\r\n",
'timeout' => 15,
]
];
$context = stream_context_create($opts);
$raw = @file_get_contents($item['url'], false, $context);
if (!$raw) {
echo " ✗ 下载失败,跳过: {$item['name']}\n";
continue;
}
// 生成 MD5 哈希文件名
$tmpName = md5($raw . $item['name']) . '.jpg';
$saveAbs = $targetDir . '/' . $tmpName;
if (cropToSquare240($raw, $saveAbs)) {
$finalSize = filesize($saveAbs);
$url = '/uploads/20261028/' . $tmpName;
echo " ✓ 成功生成 240x240 真实照片: {$url} (大小: " . round($finalSize / 1024, 2) . " KB)\n";
$results[] = [
'id' => $item['id'],
'name' => $item['name'],
'filename' => $item['filename'],
'local' => $saveAbs,
'rel_url' => $url,
'size' => $finalSize,
'sha1' => sha1_file($saveAbs),
];
} else {
echo " ✗ 裁剪失败: {$item['name']}\n";
}
}
// 将结果输出到 JSON 临时文件供下一步同步脚本读取
file_put_contents(__DIR__ . '/real_photos_result.json', json_encode($results, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
echo "\n已成功生成 " . count($results) . " 张真实二级分类实拍图!\n";