2016-05-29 10 views
0

Image Magickを使用してLaravelに画像をアップロード、変換、保存しようとしています。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

がpublic_path 'にあなたの' $ folder'を変更しようとする使用します( 'uploads/images /') 'を実行し、' p ublic/uploads/images'ディレクトリにあります。 –

+0

また、Storageディレクトリに格納しておきたい場合は、Storageディレクトリに格納してルートを使用して表示することもできます。 – Ohgodwhy

答えて

0

Image Magickを使用するには、まずサーバーにそのモジュールがあるかどうかを確認する必要があります。それ以外の場合はインストールできます。 See this to install imagemagick

これ以外の場合は、configフォルダにimage.phpファイルを設定してgdを使用できます。 gdはデフォルトで利用可能です

$folder = 'uploads/images/'; <--- ?????には開始点が指定されていないため、スクリプトを実行するときの開始点になります。したがって、保存パスを確認するには、それぞれstorageまたはpublicフォルダに格納する場合は、storage_path()またはpublic_path()を使用してパスを定義する必要があります。使用しているバージョンに応じてCheck here for more paths available与えられたリンクはLaravel 5.2です。ページの右上にあるバージョンを変更することができます。

1

$要求 - >は(ファイル)を返すことができます。のSymfony \コンポーネント\ \ HttpFoundation \ \にUploadedFileまたは配列またはヌルファイル

あなたは処理の前にそれを確認する必要があります。 var_dump($ファイル)またはdd($ファイル)でダンプしてください。わかりませんが、文字列であってはいけません。

$ folder変数にpublic_path()を使用すると、今後問題を防ぐのに役立ちます。

もLaravelのために、この素​​晴らしいパッケージチェック:http://image.intervention.io/getting_started/introduction

+0

そのパッケージは最高です! Mac OSXにImageMagickをインストールする方法は全く分かっていませんでしたが、デフォルトでGDを使用しています。本当に使いやすい、ありがとう。 – frosty

0

を私はImage Interventionパッケージを使用していますLaravel、内の複数の画像/ファイルのアップロードをサポートするスクリプトを作りました。これはおそらくあなたと他の人にとっては役に立ちます。いくつかのコメントを追加し、何が起こっているのかをよりよく理解するために不要なコードを削除しました。

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の内蔵RESTfulなリソースコントローラルート

/** 
* 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']); 
} 
関連する問題