我想通过httpgoundation响应从一个控制器发送到另一个控制器的简单图像。这是我的代码:
$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 Response content must be a string or object implementing __toString()
。使用谷歌寻求答案我只找到了想要从服务器(资产)某处返回文件的人。我该怎么办才能让它发挥作用?
答案 0 :(得分:2)
您必须使用return $response;
代替return new Response($response);
您必须将图像资源转换为字符串。执行以下操作:
$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 );