Browse Source

1.修改图库的直接下载功能,点击直接保存和下载;
2. 视频预览图限制高度最高200px;

moshaorui 3 weeks ago
parent
commit
0c8d746ffa

+ 14 - 38
app/Http/Controllers/HomeController.php

@@ -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);
     }
 
 

+ 1 - 0
public/static/css/main.css

@@ -1734,6 +1734,7 @@ table.album_table td {
   color: #555;
 }
 table.album_table img {
+    max-height: 200px;
   border-radius: 5px;
 }
 table.album_table button {

+ 21 - 3
public/static/js/script.js

@@ -284,7 +284,7 @@ $(document).ready(function() {
 
     var checkDownloadStatus = function() {
         var intervalId = setInterval(function() {
-            console.log('checkDownloadStatus ....');
+            //console.log('checkDownloadStatus ....');
             $.ajax({
                 url: '/download-status',
                 method: 'GET',
@@ -382,9 +382,11 @@ $(document).ready(function() {
                     const currentImageSrc = imageSrcArray[currentImageIndex];
                     // 下载图片 js urlencode
                     href_url = '/download-image?url='+encodeURIComponent(currentImageSrc);
-
                     window.location.href = href_url;
-
+                    //延迟执行,等待下载完成
+                    // setTimeout(function() {
+                    //     layer.close(loadIndex); // 关闭加载层
+                    // }, 2000);
                     checkDownloadStatus();
                 }
             });
@@ -512,6 +514,22 @@ $(document).ready(function() {
     }
 
 
+    $(".download-icon").on('click', function() {
+        loadIndex = layer.load(2, {shade: [0.8, '#000']});
+        const currentImageSrc = $(this).attr('data-src');
+        $.ajax({
+            url: '/download-status',
+            method: 'GET',
+            dataType: 'json',
+            data: {reset: 1},
+            success: function (response) {
+                // 下载图片 js urlencode
+                href_url = '/download-image?url=' + encodeURIComponent(currentImageSrc);
+                window.location.href = href_url;
+                checkDownloadStatus();
+            }
+        });
+    });
 });
 
 function changeIframeSrc(url) {

+ 1 - 1
resources/views/gallery-detail.blade.php

@@ -164,7 +164,7 @@
                                 <img  data-src="{{ossUrl($item)}}" src="{{ossUrl($item)}}?x-oss-process=image/resize,w_380"  loading="lazy" alt="1">
                                 <div class="album-mask">
                                     <!-- 下载图标 -->
-                                    <a href="{{ossUrl($item)}}" download class="download-icon" target="_blank">
+                                    <a href="javascript:void(0)" data-src="{{ossUrl($item)}}" download class="download-icon">
                                         <i class="fa fa-cloud-download"></i>
                                     </a>
                                 </div>