2016-05-29 126 views
0

我想上傳,轉換並存儲在Laravel使用圖像Magick圖像。存儲上傳的圖像與Laravel 5

App\Http\Controllers\ArticleController

$image = $this->storeMainImage($request->file('thumbnail')); 

功能:

private function storeMainImage($file) { 
    $folder = 'uploads/images/'; <--- ????? 
    $code = uniqid(); 
    $thumb_code = $folder . 'thumb_' . $code . '.jpg'; 
    $image_code = $folder . $code . '.jpg'; 
    if(@is_array(getimagesize($file))){ 
    exec('convert '.$file.' -thumbnail 225x225^ -gravity center -extent 225x225 -compress JPEG -quality 70 -background fill white -layers flatten -strip -unsharp 0.5x0.5+0.5+0.008 '.$thumb_code); 
    exec('convert '.$file.' -compress JPEG -quality 70 -background fill white -layers flatten -strip -unsharp 0.5x0.5+0.5+0.008 '.$image_code); 
    return $image_code; 
    } else { 
    return false; 
    } 
} 

我沒有得到任何這方面的錯誤,但我不知道,如果它的實際上傳的文件,並在那裏abouts它的存儲它。

+1

試圖改變自己的'$ folder'爲'public_path ('uploads/images /')',然後在'p'中查找圖像ublic/uploads/images'目錄。 –

+0

或者,您可以將它們存儲在您的存儲目錄中,並使用路線將它們顯示回來,如果您不希望它們可公開訪問的話。 – Ohgodwhy

回答

0

要使用Image Magick,首先必須確定服務器是否具有該模塊。否則你可以安裝它。 See this to install imagemagick

否則,您可以通過在配置文件夾中配置image.php文件來使用gd。 GD是默認提供

因爲$folder = 'uploads/images/'; <--- ?????也沒有指定的起點,起點將是你在哪裏運行腳本時。因此,要確保存儲路徑,如果要分別存儲在storagepublic文件夾中,則應使用storage_path()public_path()來定義路徑。根據您使用的版本,您需要使用Check here for more paths available。給定鏈接是爲Laravel 5.2。您可以更改頁面右上角的版本。

1

$請求 - >文件()可以返回:\的Symfony \分量\ HttpFoundation \文件\ UploadedFile的陣列

你應該在處理之前檢查它。只是的var_dump($文件)DD($文件)傾倒。不確定,但它不應該是字符串。

使用public_path()爲$文件夾變量,它將幫助您在將來避免任何問題。

而且還檢查了這真棒包Laravel:http://image.intervention.io/getting_started/introduction

+0

那個包太棒了!不知道如何在Mac OSX上安裝ImageMagick,但默認情況下使用GD,所以很好。真的很容易使用,謝謝。 – frosty

0

我做了一個腳本,支持上傳中Laravel,它使用Image Intervention封裝中的多個圖像/文件。這可能對你和其他人有用。我添加了一些評論並刪除了不必要的代碼,以更好地理解發生的事情。

HTML標記

<form method="post" action="{{ route('admin.upload.store') }}" enctype="multipart/form-data"> 
    {{!! csrf_field() !!}} 
    <input type="file" name="files[]" accept="image/gif, image/jpeg, image/png"> 
    <button type="submit">Upload image(s)</button> 
</form> 

UploadController

使用Laravel的內置REST的資源控制路線

/** 
* Store a newly created resource in storage. 
* Supports uploading multiple files at once. 
* 
* @param Request $request 
* @return Response 
*/ 
public function store(Request $request) 
{ 
    // Loop through the files array 
    foreach($request->file('files') as $file) { 

     // Validate each file, we want images only 
     $validator = Validator::make(compact('file'), [ 
      'files' => 'mimes:jpeg,bmp,gif,png' 
     ]); 

     if ($validator->fails()) { 
      return redirect()->route('admin.upload.index')->withErrors($validator); 
     } 

     // Create a new upload model for the file 
     $upload = new Upload([ 
      'name'  => pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME) . '_' . Str::random(2), 
      'extension' => strtolower($file->getClientOriginalExtension()), 
      'mimetype' => $file->getMimeType(), 
      'size'  => $file->getSize(), 
     ]); 

     // Create the image 
     $file = Image::make($file)->widen(1200, function($constraint) { 
      $constraint->upsize(); // Prevent upsizing the image if doesn't exceed the maximum width 
     })->encode($upload->extension); 

     // Store it within 'storage/app/uploads' 
     Storage::disk('uploads')->put($upload->fullName(), $file); 

     // Save the upload file details in the database 
     $upload->save(); 
    } 

    return redirect()->route('admin.upload.index')->with(['success' => 'Files uploaded']); 
}