LoginSignup
0
1

More than 5 years have passed since last update.

cakephp3でコントローラーからビーコン的1x1ピクセルの画像(gif)を返す

Posted at

ケースとしてはビーコンくらいでしか利用価値がないかもしれませんが、コントローラーから1x1ピクセルのgifを返します。
どちらを使ってもいいと思いますがストリームを使うのと使わないので2通りあります。

ストリームを使わない

とくに説明は必要ないかと思いますが、見たままです。

public function img()
{
    $this->response->type('gif');
    $this->response->body(base64_decode('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='));
    return $this->response;
}

ストリームを使う

cookbookを見るとこっちのほうが推奨なのかなと思いますが、まあどちらでもいいと思います。

// ストリーム
use Zend\Diactoros\Stream;

public function imgwithstream()
{
    $response = $this->response;
    $response = $response->withType('gif');
    $stream = new Stream('data://text/plain;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==', 'r');
    $response = $response->withBody($stream);
    return $response;
}

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1