返回动态图像zf2 [英] return dynamic image zf2

查看:126
本文介绍了返回动态图像zf2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用zend框架2并尝试返回使用gd2库jpeg图像创建的。但它不起作用。你看看我的代码有什么问题吗?我的代码通常使用普通的php运行但是在zf2问题中?

I use zend framework 2 and try to return an created with gd2 library jpeg image . but it doesn't work. could you look my code what's the problem? My code is run with plain php in normally but in zf2 problem?

class PictureController extends AbstractActionController
{
    public function colorPaletteAction(){

        ....
        ....
        //canvas created at above.

        imagejpeg($canvas);
        imagedestroy($canvas);
        $response = $this->getResponse();

        return $response->getHeaders()->addHeaderLine('Content-Type', 'image/jpeg');
    }
}


推荐答案

imagejpeg 立即输出你不想做的数据。您可以使用输出缓冲区来捕获此数据,也可以先将其写入文件。输出缓冲区可能最简单:

imagejpeg outputs the data immediately which you don't want to do. You can either use the output buffer to capture this data or write it to a file first. The output buffer is probably easiest:

public function colorPaletteAction()
{
    // [create $canvas]

    ob_start();
    imagejpeg($canvas);
    $imageData = ob_get_contents();
    ob_end_clean();

    imagedestroy($canvas);

    $response = $this->getResponse();

    $response->getHeaders()->addHeaderLine('Content-Type', 'image/jpeg');
    $response->setContent($imageData);

    return $response;
}

如果这不起作用,请暂时注释掉Content-Type标题行看看你得到了什么输出。确保输出中没有任何错误或HTML。

If this doesn't work, temporarily comment out the Content-Type header line to see what output you're getting. Make sure there aren't any errors or HTML in the output.

这篇关于返回动态图像zf2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆