'芒果', 'filename' => 'fruit-mango.jpg', 'url' => 'https://images.unsplash.com/photo-1553279768-865429fa0078?w=500&q=80&auto=format&fit=crop', ], [ 'name' => '香蕉', 'filename' => 'fruit-banana.jpg', 'url' => 'https://images.unsplash.com/photo-1571771894821-ce9b6c11b08e?w=500&q=80&auto=format&fit=crop', ], [ 'name' => '蓝莓', 'filename' => 'fruit-blueberry.jpg', 'url' => 'https://images.unsplash.com/photo-1498557850523-fd3d118b962e?w=500&q=80&auto=format&fit=crop', ], [ 'name' => '车厘子', 'filename' => 'fruit-cherry.jpg', 'url' => 'https://images.unsplash.com/photo-1528825871115-3581a5387919?w=500&q=80&auto=format&fit=crop', ], [ 'name' => '哈密瓜', 'filename' => 'fruit-melon.jpg', 'url' => 'https://images.unsplash.com/photo-1571575173700-afb9492e8c50?w=500&q=80&auto=format&fit=crop', ], [ 'name' => '火龙果', 'filename' => 'fruit-dragon.jpg', 'url' => 'https://images.unsplash.com/photo-1527324688151-0e627063f2b1?w=500&q=80&auto=format&fit=crop', ], [ 'name' => '柠檬', 'filename' => 'fruit-lemon.jpg', 'url' => 'https://images.unsplash.com/photo-1533089860892-a7c6f0a88666?w=500&q=80&auto=format&fit=crop', ], [ 'name' => '荔枝', 'filename' => 'fruit-lychee.jpg', 'url' => 'https://images.unsplash.com/photo-1596386461350-326ccb383e9f?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; } echo "=== [1/3] 下载真实水果实拍照并裁剪为 240x240 ===\n"; $downloaded = []; foreach ($fruits as $f) { echo "正在下载 [{$f['name']}] 实拍图: {$f['url']} ...\n"; $opts = [ 'http' => [ 'method' => 'GET', 'header' => "User-Agent: Mozilla/5.0\r\n", 'timeout' => 15, ] ]; $ctx = stream_context_create($opts); $raw = @file_get_contents($f['url'], false, $ctx); if (!$raw) { // 尝试备用图 echo " ✗ 下载超时或失败: {$f['name']}\n"; continue; } $md5Name = md5($raw . $f['name']) . '.jpg'; $savePath = $targetDir . '/' . $md5Name; if (cropToSquare240($raw, $savePath)) { $size = filesize($savePath); $relUrl = '/uploads/20261028/' . $md5Name; echo " ✓ 生成成功: {$f['name']} -> {$relUrl} (" . round($size / 1024, 2) . " KB)\n"; $downloaded[] = [ 'name' => $f['name'], 'filename' => $f['filename'], 'local' => $savePath, 'rel_url' => $relUrl, 'size' => $size, 'sha1' => sha1_file($savePath), ]; } } // 写入临时 json 供 python 脚本后续同步 file_put_contents(__DIR__ . '/more_fruits_result.json', json_encode($downloaded, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); echo "\n已准备就绪 " . count($downloaded) . " 个水果二级分类图片!\n";