2016-11-11 6 views
0

画像介入のstream()関数は何をしているのでしょうか? http://image.intervention.io/api/streamphp laravel画像介入のStream()関数とは何ですか

は、今のところ私はこのようなアマゾンS3に自分の画像をアップロードしています:Intervention Docsあなたは上記きた中で書かれた

public function uploadLargeAndMainImages($file,$adId) 
    { 
    $s3 = Storage::disk('s3'); 
    $extension = $file->guessExtension(); 
    $filename = uniqid() . '.' . $extension; 

    //Create and resize images 
    $image = Image::make($file)->resize(null, 600, function ($constraint) { 
     $constraint->aspectRatio(); 
    }); 
    $image->encode($extension); 

    $imageLarge = Image::make($file)->resize(null, 800, function ($constraint) { 
     $constraint->aspectRatio(); 
    }); 
    $imageLarge->encode($extension); 

    // upload image to S3 
    $s3->put("images/{$adId}/main/" . $filename, (string) $image, 'public'); 
    $s3->put("images/{$adId}/large/" . $filename, (string) $imageLarge, 'public'); 

    // make image entry to DB 
    $file = File::create([ 
     'a_f_id' => $adId, 
     'file_name' => $filename, 
    ]); 

    } 

答えて

2

そのすべて:

stream()方法は、画像を符号化します与えられたフォーマットと 画質が与えられ、画像データに基づいて新しいPSR-7ストリームが作成されます。

PSR-7 streamGuzzleHttp\Psr7\Streamのインスタンスとして返します。

// encode png image as jpg stream 
$stream = Image::make('public/foo.png')->stream('jpg', 60); 

ちょうどデモのために、あなたは、このようにS3とstream()メソッドを使用することができます。

... 
$image_normal = Image::make($image)->widen(800, function ($constraint) { 
    $constraint->upsize(); 
}); 
$image_thumb = Image::make($image)->crop(100,100); 
$image_normal = $image_normal->stream(); 
$image_thumb = $image_thumb->stream(); 

Storage::disk('s3')->put($path.$file, $image_normal->__toString()); 
Storage::disk('s3')->put($path.'thumbnails/'.$file, $image_thumb->__toString()); 

それはあなたがそれを得ると思います!

+0

「画像データに基づいて新しいPSR-7ストリームを作成する」 私はすでにエンコード機能を使用しています。 http://image.intervention.io/api/encodeほとんど同じように見える – user2636197

+0

PSR 7はエンコードしない新しいエンコード標準です。http://www.php-figを参照してください。 org/psr/psr-7/ –

+0

情報をありがとう!私は今あなたの答えを受け入れました:)これは話題ですが、PSR 7に切り替える必要がある理由はありますか? – user2636197

関連する問題