Ver código fonte

thumbnail image

zhangdehua 11 meses atrás
pai
commit
ccf8573f5f

+ 61 - 1
app/common.php

@@ -494,4 +494,64 @@ function curl_get($url)
     $result = curl_exec($ch);
     curl_close($ch);
     return $result;
-}
+}
+
+function createThumbnail($sourceFile, $thumbnailFile, $thumbnailWidth = 100, $quality = 80) {
+    // 获取原图像信息
+    list($sourceWidth, $sourceHeight, $sourceType) = getimagesize($sourceFile);
+    $sourceMime = image_type_to_mime_type($sourceType);
+
+    // 获取原图像的资源
+    switch ($sourceMime) {
+        case 'image/jpeg':
+            $sourceImage = imagecreatefromjpeg($sourceFile);
+            break;
+        case 'image/png':
+            $sourceImage = imagecreatefrompng($sourceFile);
+            break;
+        case 'image/gif':
+            $sourceImage = imagecreatefromgif($sourceFile);
+            break;
+        default:
+            throw new Exception('Unsupported image type.');
+    }
+
+    // 计算缩放比例
+    $xScale = $thumbnailWidth / $sourceWidth;
+    $scale = min($xScale, 1); // 确保不会超过原图的大小
+
+    // 计算新的尺寸
+    $thumbnailHeight = intval($sourceHeight * $scale);
+    $thumbnailWidth = intval($sourceWidth * $scale);
+
+    // 创建缩略图的资源
+    $thumbnailImage = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight);
+
+    // 调整缩略图的透明度 (如果源图像是PNG或GIF)
+    if ($sourceMime == 'image/png' || $sourceMime == 'image/gif') {
+        imagealphablending($thumbnailImage, false);
+        imagesavealpha($thumbnailImage, true);
+        $transparent = imagecolorallocatealpha($thumbnailImage, 255, 255, 255, 127);
+        imagefilledrectangle($thumbnailImage, 0, 0, $thumbnailWidth, $thumbnailHeight, $transparent);
+    }
+
+    // 缩放并保留透明度 (如果需要)
+    imagecopyresampled($thumbnailImage, $sourceImage, 0, 0, 0, 0, $thumbnailWidth, $thumbnailHeight, $sourceWidth, $sourceHeight);
+
+    // 保存缩略图
+    switch ($sourceMime) {
+        case 'image/jpeg':
+            imagejpeg($thumbnailImage, $thumbnailFile, $quality);
+            break;
+        case 'image/png':
+            imagepng($thumbnailImage, $thumbnailFile);
+            break;
+        case 'image/gif':
+            imagegif($thumbnailImage, $thumbnailFile);
+            break;
+    }
+
+    // 释放内存
+    imagedestroy($sourceImage);
+    imagedestroy($thumbnailImage);
+}

+ 66 - 0
app/common/library/storage/engine/Google.php

@@ -0,0 +1,66 @@
+<?php
+// +----------------------------------------------------------------------
+// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: 萤火科技 <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types=1);
+
+namespace app\common\library\storage\engine;
+
+use Google\Cloud\Storage\StorageClient;
+use think\facade\Log;
+
+/**
+ * 谷歌存储引擎 (GCS)
+ * Class Google
+ * @package app\common\library\storage\engine
+ */
+class Google extends Basics
+{
+    /**
+     * 执行上传
+     * @return bool
+     */
+    public function upload(): bool
+    {
+        $keyFilePath = '/etc/nginx/nginx/html/dihua-01-5690813badec.json';
+        $storage = new StorageClient(['projectId' => 'dihua-01', 'keyFilePath' => $keyFilePath]);
+        $bucket = $storage->bucket('freeshippingvps');
+
+        $filePathAbsolute = public_path('uploads') . $this->getSaveFileInfo()['file_path'];
+        try {
+            $storageObj = $bucket->upload(
+                fopen($filePathAbsolute, 'r'),
+                [
+                    'name' => $this->getSaveFileInfo()['file_path']
+                ]
+            );
+            $name = $storageObj->name();
+            if (empty($name)) {
+                Log::error('上传文件到google云失败');
+                $this->error = '上传文件到google云失败';
+                return false;
+            }
+        } catch (\Exception $e) {
+            $this->error = $e->getMessage();
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * 删除文件
+     * @param string $filePath
+     * @return bool
+     */
+    public function delete(string $filePath): bool
+    {
+        return true;
+    }
+
+}

+ 19 - 0
app/index/controller/Index.php

@@ -15,6 +15,7 @@ use app\index\model\GoodsCategoryRel;
 use app\index\model\user\UserAccessLog;
 use app\store\model\Setting as SettingModel;
 use app\store\model\UploadFile as UploadFileModel;
+use Exception;
 use think\facade\Cache;
 use think\facade\Log;
 use think\facade\Session;
@@ -212,6 +213,24 @@ class Index extends Controller
 
     public function goldTest()
     {
+        $fileInfo['file_path'] = '10001/20241014/9dedcf80308af9dbdd12515d259483ce.png';
+
+        $sourceFile = 'D:\phpstudy_pro\WWW\gors\vape_shop\public\uploads/' . $fileInfo['file_path'];
+
+        list($storeId,$date,$fileName) = explode('/' , $fileInfo['file_path']);
+
+        $thumbnailFile = $storeId.'/'.$date.'/TH'.$fileName;// 缩略图保存路径
+
+        // 使用示例
+        try {
+            $thumbnailFile = 'thumbnail.png'; // 缩略图保存路径
+            createThumbnail($sourceFile, $thumbnailFile, 100, 80); // 创建缩略图
+        } catch (Exception $e) {
+            Log::error($e->getMessage());
+            return $this->renderError($e->getMessage());
+        }
+        dd('haole');
+
         $bjd = fopen("https://storage.googleapis.com/freeshippingvps/10001/20241010/5c046e98da7e1cd98ae0c42798357c0a.jpg","r");
         dd($bjd);
 

+ 8 - 8
app/index/view/index/index.html

@@ -50,8 +50,8 @@
                     <div class="newGoodsItem" data-id="{$goods['goods_id']}">
                         <div class="goodsImgBox">
 <!--                            <img class="lazy" src="/assets/index/asstes/icon/transparent.png"-->
-                            <img class="lazy" src="{$goods['goods_images'][0]['preview_url']}"
-                                 data-original="{$goods['goods_images'][0]['preview_url']}" alt="FreeShippingVapes"
+                            <img class="lazy" src="{$goods['goods_image']}"
+                                 data-original="{$goods['goods_image']}" alt="FreeShippingVapes"
                                  title="FreeShippingVapes">
                         </div>
                         <div class="goodsInfo">
@@ -106,8 +106,8 @@
                 <a href="/index/index/productDetails/goodsId/{$goods['goods_id']}.html">
                     <div class="newGoodsItem" data-id="{$goods['goods_id']}">
                         <div class="goodsImgBox" data-id="{$goods['goods_id']}">
-                            <img class="lazy" src="{$goods['goods_images'][0]['preview_url']}"
-                                 data-original="{$goods['goods_images'][0]['preview_url']}"
+                            <img class="lazy" src="{$goods['goods_image']}"
+                                 data-original="{$goods['goods_image']}"
                                  alt="FreeShippingVapes" title="FreeShippingVapes">
                         </div>
                         <div class="goodsInfo">
@@ -212,8 +212,8 @@
                     <div class="newGoodsTag">New</div>
                     <div class="goodsImgBox">
 <!--                        <img class="lazy" src="/assets/index/asstes/icon/transparent.png"-->
-                        <img class="lazy" src="{$deal['goods_images'][0]['preview_url']}"
-                             data-original="{$deal['goods_images'][0]['preview_url']}" alt="FreeShippingVapes"
+                        <img class="lazy" src="{$deal['goods_image']}"
+                             data-original="{$deal['goods_image']}" alt="FreeShippingVapes"
                              title="FreeShippingVapes">
                     </div>
                     <div class="goodsInfo">
@@ -259,8 +259,8 @@
                 <div class="newGoodsItem" data-id="{$deal['goods_id']}">
                     <div class="newGoodsTag">New</div>
                     <div class="goodsImgBox">
-                        <img class="lazy" src="/assets/index/asstes/icon/transparent.png"
-                             data-original="{$deal['goods_images'][0]['preview_url']}" alt="FreeShippingVapes"
+                        <img class="lazy" src="{$deal['goods_image']}"
+                             data-original="{$deal['goods_image']}" alt="FreeShippingVapes"
                              title="FreeShippingVapes">
                     </div>
                     <div class="goodsInfo">

+ 16 - 5
app/store/controller/Upload.php

@@ -17,6 +17,7 @@ use app\store\model\UploadFile as UploadFileModel;
 use app\common\enum\Setting as SettingEnum;
 use app\common\enum\file\FileType as FileTypeEnum;
 use app\common\library\storage\Driver as StorageDriver;
+use Exception;
 use think\facade\Log;
 use think\response\Json;
 use Google\Cloud\Storage\StorageClient;
@@ -75,19 +76,29 @@ class Upload extends Controller
         $storage = new StorageClient(['projectId' => 'dihua-01', 'keyFilePath' => $keyFilePath]);
         $bucket = $storage->bucket('freeshippingvps');
 
-        //$filePath = config('app.app_host') . '/uploads/' . $fileInfo['file_path'];
-        $filePath = '/etc/nginx/nginx/html/vape_shop/public/uploads/' . $fileInfo['file_path'];
+        $filePathAbsolute = '/etc/nginx/nginx/html/vape_shop/public/uploads/' . $fileInfo['file_path'];
+        list($storeId, $date, $fileName) = explode('/', $fileInfo['file_path']);
 
-        Log::error($filePath);
+        $thumbnailFile = $storeId . '/' . $date . '/TH' . $fileName;// 缩略图保存路径
+        // 使用示例
+        try {
+            createThumbnail($filePathAbsolute, $thumbnailFile, 100, 80); // 创建缩略图
+        } catch (Exception $e) {
+            Log::error($e->getMessage());
+            return $this->renderError($e->getMessage());
+        }
+
+        Log::error($filePathAbsolute);
         $storageObj = $bucket->upload(
-            fopen($filePath, 'r'),
+            fopen($filePathAbsolute, 'r'),
             [
                 'name' => $fileInfo['file_path']
             ]
         );
         $name = $storageObj->name();
-        if (empty($name)){
+        if (empty($name)) {
             Log::error('上传文件到google云失败');
+            return $this->renderError('上传文件到google云失败');
         }
 
         // 图片上传成功