27, 'name' => '普工技工', 'filename' => 'job-tech.jpg', 'url' => 'https://images.unsplash.com/photo-1581092335397-9583fe92d232?w=500&q=80&auto=format&fit=crop', ], [ 'id' => 39, 'name' => '鲜活水产', 'filename' => 'poultry-fish.jpg', 'url' => 'https://images.unsplash.com/photo-1524704654690-b56c05c78a00?w=500&q=80&auto=format&fit=crop', ], ]; 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); $res = imagejpeg($dst, $savePath, 82); imagedestroy($src); imagedestroy($dst); return $res; } foreach ($missing as $item) { echo "正在补齐 [{$item['name']}] 真实实拍照: {$item['url']} ...\n"; $raw = @file_get_contents($item['url']); if (!$raw) { // 备用地址 $backupUrl = ($item['id'] === 27) ? 'https://images.unsplash.com/photo-1581092160607-ee22621dd758?w=500&q=80&auto=format&fit=crop' : 'https://images.unsplash.com/photo-1516684732162-798a0062be99?w=500&q=80&auto=format&fit=crop'; $raw = @file_get_contents($backupUrl); } if ($raw) { $tmpName = md5($raw . $item['name']) . '.jpg'; $saveAbs = $targetDir . '/' . $tmpName; if (cropToSquare240($raw, $saveAbs)) { $finalSize = filesize($saveAbs); $url = '/uploads/20261028/' . $tmpName; echo " ✓ 成功补齐: {$url} (" . round($finalSize / 1024, 2) . " KB)\n"; $existing[] = [ '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"; } } file_put_contents($jsonFile, json_encode($existing, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); echo "当前清单已有 " . count($existing) . " 个分类的真实照片!\n";