I want to send simple image through httpgoundation response, from one controller to another. Here's my code:
$string = (string)md5(uniqid());
$string = substr($string, 0, $length);
$image = imagecreate(200, 50);
imagefill($image, 0, 0, "#000000");
$headers= array(
'Content-type'=>'image/jpeg',
'Pragma'=>'no-cache',
'Cache-Control'=>'no-cache'
);
$response = new Response( $image, 200, $headers );
return new Response($response);
The error says The Response content must be a string or object implementing __toString()
. Seeking the answer using google I only found people who want to return file somewhere from server(an asset). What should I do to get this working?
You have to use return $response;
instead of return new Response($response);
And you have to convert image resource to string. Do the following:
$string = (string)md5(uniqid());
$string = substr($string, 0, $length);
$image = imagecreate(200, 50);
imagefill($image, 0, 0, "#000000");
$headers= array(
'Content-type'=>'image/jpeg',
'Pragma'=>'no-cache',
'Cache-Control'=>'no-cache'
);
ob_start();
imagejpeg($img);
$imageString = ob_get_clean();
return new Response( $imageString, 200, $headers );