2017-11-09 10 views
0

Laravelで画像変換を行っています。私が欲しいのは、画面に表示されている画像がページに読み込まれ、サムネイルに変換されて画面に表示されるときです。今はサムネイルに変換されていない画像を画面に表示することができるので、ページをロードすると画面に画像を表示するには時間がかかりすぎます。私は誰かが私を助けることを願っていますLaravelで画面に表示中に実行時にサムネイルを作成する

答えて

0

私はLaravel 5でImagineライブラリを使用しました。 飛行中に画像サムネイルを作成する場合は、これを行います。

  1. イメージコントローラImageController.phpを作成し、コードの下に貼り付けて、このRoute::get('/images1/{file}', '[email protected]');
  2. にweb.phpにルートを追加https://github.com/orchestral/imagine

  3. を通じてこのライブラリを統合します。

    <?php 
    

    名前空間App \ Http \ Controllers; Illuminate \ Foundation \ Bus \ DispatchesJobsを使用します。 Illuminate \ Routing \ ControllerをBaseControllerとして使用します。 Illuminate \ Foundation \ Validation \ ValidatesRequestsを使用します。 イルミネーション\ Foundation \ Auth \ Access \ AuthorizesRequestsを使用します。 イルミネーション\ Support \ Facades \ Fileを使用します。 Illuminate \ Support \ Facades \ Configを使用します。 セッションを使用します。 ビューを使用します。 ルートを使用します。 応答を使用します。

    クラスImageControllerがBaseControllerを拡張{

    保護$想像。

    パブリック関数のgetImage($ファイル名){

    //if (!$this->imagine) { 
    // if (!$this->library and class_exists('Imagick')) { 
    //  $this->imagine = new \Imagine\Imagick\Imagine(); 
    // } else { 
    //  $this->imagine = new \Imagine\Gd\Imagine(); 
    // } 
    //} 
    $this->imagine = new \Imagine\Gd\Imagine(); 
    
    
    // Append the filename to the path where our images are located 
    $path = "C:\\xampp\\htdocs\\Eidolon_laravel-Git\\public\\Images\\".$filename; //$filename; 
    $filename1 = $filename; 
    // Initialize an instance of Symfony's File class. 
    // This is a dependency of Laravel so it is readily available. 
    $filename = new \Symfony\Component\HttpFoundation\File\File($path); 
    
    // Make a new response out of the contents of the file 
    // Set the response status code to 200 OK 
    $response = Response::make(
        //\File::get($path), 
        $this->resize($filename, 150,150), 
        200 
    ); 
    
    // Modify our output's header. 
    // Set the content type to the mime of the file. 
    // In the case of a .jpeg this would be image/jpeg 
    $response->header(
        'Content-type', 
        'image/jpg' //$file->getMimeType() 
    ); 
    
    // We return our image here. 
    //return view('value.pushMessages', $response); 
    return $response; 
    

    }

    /*

    • 関数のサイズを変更します。
    • @param列sizeString *
    • @returnブロブ画像内容

    • @param文字列のファイル名。 */ public function resize($ filename、$ width、$ height){

      //設定ファイルから出力パスを読み取ることができます。 // $ outputDir = \ Config :: get( 'assets.images.paths.output'); $ outputDir = "C:\ xampp \ htdocs \ Eidolon_laravel-Git \ public \ Images \ Output";

      //サイズとファイル名から出力ファイルパスを作成します。 $ outputFile = $ outputDir。 $ filename;

      //サイズ変更されたファイルが既に存在する場合は、それを返します。 if(\ File :: isFile($ outputFile)){ return \ File :: get($ outputFile); }

      //ファイルはまだ存在しないため、オリジナルのサイズを変更します。 // $ inputDir = \ Config :: get( 'assets.images.paths.input'); // $ inputFile = $ inputDir。 '/'。 $ filename;

      $ inputFile = $ filename; //設定ファイルから選択したサイズの幅と高さを取得します。 // $ sizeArr = Config :: get( 'assets.images.sizes。'。$ sizeString); // $ width = $ sizeArr ['width']; // $ height = $ sizeArr ['height'];

      //リサイズモードとサイズを設定するために画像を切り抜きたい。 $ size = new \ Imagine \ Image \ Box($ width、$ height); $ mode = \ \ Image \ ImageInterface :: THUMBNAIL_OUTBOUNDを想像してください。

      //出力ディレクトリがまだ存在しない場合は作成します。 if(!\ File :: isDirectory($ outputDir)){ \ File :: makeDirectory($ outputDir); }

      //ファイルを開き、サイズを変更して保存します。 $ this-> imagine-> open($ inputFile) - >サムネイル($ size、$ mode) - > save($ outputFile、array( 'quality' => 90));

      //サイズ変更されたファイルを返します。 return \ File :: get($ outputFile);

    }

    /**

    • @param列$ファイル名
    • @return列MIMEタイプ */ パブリック関数のgetMimeType($ファイル名){

      ////入力ファイルのパスを作ります。 // $ inputDir = \ Config :: get( 'assets.images.paths.input'); // $ inputFile = $ inputDir。 '/'。 $ filename; $ inputFile = $ filename; // Symfony Fileクラスを使用してファイルのMIMEタイプを取得します。 $ file = new \ Symfony \ Component \ HttpFoundation \ File \ File($ inputFile); return $ file-> getMimeType();

      }}

    ?>

今あなたのコードを実行します。

関連する問題