繁体   English   中英

PHP SLIM:如何返回动态生成的图像

[英]PHP SLIM : How to return dynamically generated images

我正在尝试显示从SLIM控制器动态生成的图像,但是出现黑屏。 谢谢。

public function textToImage($request, $response, $args) {

    // The text to draw
    $text = "Hello World";
    $length=strlen($text);
    // Create the image

    $im = imagecreatetruecolor($length*12, 30);

    // Create some colors
    $white = imagecolorallocate($im, 255, 255, 255);
    $grey = imagecolorallocate($im, 128, 128, 128);
    $black = imagecolorallocate($im, 44, 62, 80);
    $boxcolor=imagecolorallocate($im, 95, 128, 149);

    $font = 'Helvetica.ttf';
    imagefilledrectangle($im, 0, 0, $length*9+$capitaladjust, 29, $white);
    imagettftext($im, 13, 0, 0, 20, $black, $font, $text);

    $response->getBody()->write(base64_encode($im));
    $response = $response->withHeader('Content-type', 'image/png');     
    return $response;
 }

base64编码$im不会创建有效的PNG文件。 尝试使用imgpng ,然后发送其创建的PNG文件的原始内容。

代码看起来像这样:

ob_start();
imagepng($im);
$data = ob_get_contents();
ob_end_clean();

$response->getBody()->write($data);
$response = $response->withHeader('Content-type', 'image/png');     
return $response;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM