فهرست منبع

修复图片上传表单无法使用[intervention/image]组件bug

jqh 6 سال پیش
والد
کامیت
dd99d3208c
3فایلهای تغییر یافته به همراه28 افزوده شده و 8 حذف شده
  1. 9 0
      src/Form/Field/Image.php
  2. 4 2
      src/Form/Field/ImageField.php
  3. 15 6
      src/Form/Field/UploadField.php

+ 9 - 0
src/Form/Field/Image.php

@@ -112,4 +112,13 @@ class Image extends File
         return $this->dimensions(['ratio' => $ratio]);
     }
 
+    /**
+     * @param UploadedFile $file
+     *
+     */
+    protected function prepareFile(UploadedFile $file)
+    {
+        $this->callInterventionMethods($file->getRealPath(), $file->getClientMimeType());
+    }
+
 }

+ 4 - 2
src/Form/Field/ImageField.php

@@ -34,16 +34,18 @@ trait ImageField
      *
      * @return mixed
      */
-    public function callInterventionMethods($target)
+    public function callInterventionMethods($target, $mime)
     {
         if (!empty($this->interventionCalls)) {
             $image = ImageManagerStatic::make($target);
 
+            $mime = $mime ?: finfo_file(finfo_open(FILEINFO_MIME_TYPE), $target);
+
             foreach ($this->interventionCalls as $call) {
                 call_user_func_array(
                     [$image, $call['method']],
                     $call['arguments']
-                )->save($target);
+                )->save($target, null, $mime);
             }
         }
 

+ 15 - 6
src/Form/Field/UploadField.php

@@ -147,7 +147,7 @@ trait UploadField
                 return $this->responseError(403, 'Missing id');
             }
 
-            if (!($file = $this->mergeChunks($id, $file))) {
+            if (! ($file = $this->mergeChunks($id, $file))) {
                 return $this->response(['merge' => 1]);
             }
 
@@ -161,6 +161,8 @@ trait UploadField
 
             $this->renameIfExists($file);
 
+            $this->prepareFile($file);
+
             if (!is_null($this->storagePermission)) {
                 $result = $this->storage->putFileAs($this->getDirectory(), $file, $this->name, $this->storagePermission);
             } else {
@@ -190,6 +192,13 @@ trait UploadField
         }
     }
 
+    /**
+     * @param UploadedFile $file
+     */
+    protected function prepareFile(UploadedFile $file)
+    {
+    }
+
     /**
      * @param string $id
      * @param UploadedFile $file
@@ -219,15 +228,15 @@ trait UploadField
 
         if (!$done) return;
 
-        $this->tempFilePath = $tmpDir . '/' . $newFilename . '.tmp';
+        $this->tempFilePath = $tmpDir.'/'.$newFilename.'.tmp';
         $this->putTmpFileContent($chunks, $tmpDir, $newFilename);
 
         return new UploadedFile(
             $this->tempFilePath,
-           $file->getClientOriginalName(),
-           'application/octet-stream',
-           UPLOAD_ERR_OK,
-           true
+            $file->getClientOriginalName(),
+            'application/octet-stream',
+            UPLOAD_ERR_OK,
+            true
         );
     }