|
@@ -216,48 +216,24 @@ class HomeController extends Controller
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+ /*
|
|
|
+ * 下载图片
|
|
|
+ */
|
|
|
public function downloadImage(Request $request)
|
|
|
{
|
|
|
Session::put('downloadAllStatus', '1');
|
|
|
-
|
|
|
- // Get the URL from the request parameter
|
|
|
$imageUrl = $request->input('url');
|
|
|
-
|
|
|
-
|
|
|
- try {
|
|
|
- // Initialize cURL
|
|
|
- $ch = curl_init($imageUrl);
|
|
|
-
|
|
|
- // Set cURL options
|
|
|
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Allow HTTPS (disable SSL peer verification)
|
|
|
- curl_setopt($ch, CURLOPT_TIMEOUT, 30); // Set timeout to prevent hanging
|
|
|
-
|
|
|
- // Execute cURL request
|
|
|
- $imageContent = curl_exec($ch);
|
|
|
- $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
-
|
|
|
- if ($httpCode !== 200) {
|
|
|
- Session::put('downloadAllStatus', '2');
|
|
|
- return response()->json(['error' => 'Failed to download image'], 500);
|
|
|
- }
|
|
|
-
|
|
|
- curl_close($ch);
|
|
|
-
|
|
|
- // Generate a random filename
|
|
|
- $fileName = Str::random(32) . '.jpg';
|
|
|
-
|
|
|
- Session::put('downloadAllStatus', '2');
|
|
|
- // Return the image as a download
|
|
|
- return response($imageContent)
|
|
|
- ->header('Content-Type', 'image/jpeg')
|
|
|
- ->header('Content-Disposition', 'attachment; filename="' . $fileName . '"');
|
|
|
- } catch (\Exception $e) {
|
|
|
- Session::put('downloadAllStatus', '2');
|
|
|
- return response()->json(['error' => 'Error downloading image: ' . $e->getMessage()], 500);
|
|
|
- }
|
|
|
+ $imageUrl = strtok($imageUrl, '?');
|
|
|
+ $urlWithoutQuery = parse_url($imageUrl, PHP_URL_PATH);
|
|
|
+ $fileName = basename($urlWithoutQuery);
|
|
|
+ //下载图片
|
|
|
+ $fileContent = file_get_contents($imageUrl);
|
|
|
+ $headers = [
|
|
|
+ 'Content-Type' => 'image/jpeg',
|
|
|
+ 'Content-Disposition' => 'attachment; filename='. $fileName,
|
|
|
+ ];
|
|
|
+ Session::put('downloadAllStatus', '2');
|
|
|
+ return response($fileContent, 200, $headers);
|
|
|
}
|
|
|
|
|
|
|