如何使用ob_get_contents获取图像内容

How get content of image using ob_get_contents

提问人:user2918057 提问时间:3/14/2017 最后编辑:user2918057 更新时间:3/15/2017 访问量:673

问:

我想从中获取文件的内容,在我的代码中,它返回空。imagePng()

只是为了测试,如果我删除它并且它正在打印内容,但我不知道我该如何获得它。我想我的代码中缺少一些东西。ob_end_clean()

public function create_thumb($content_file, $percent=5){

        $result = array(
            'success'   => true,
            );

        list($width, $height)  = @getimagesize($content_file);
        $n_width  = $width * $percent;
        $n_height = $height * $percent;
        $image_src = @imageCreateFromPng($content_file);

        if($image_src){

            ob_start();

            $image = @imagecreatetruecolor($n_width, $n_height);
            @imagecopyresampled($image, $image_src, 0, 0, 0, 0, $n_width, $n_height, $width, $height);
            @imagepng($image, null, 100);
            $result['thumb'] = ob_get_contents();
            ob_end_clean();
        }else{
            $result['success'] = false;
            $result['image_src'] = $image_src;
        }


        return $result;
    }

我的回报:

{
      "success": true,
      "thumb": {
        "success": true,
        "thumb": ""
      }
 }

溶液

我的代码中缺少它。base64_encode(ob_get_contents())

ob_start();
@imagepng($image, null, 100);
$result['thumb']  = 'data:image/png;base64,' . base64_encode(ob_get_contents())
ob_end_clean();
php ob-get-contents

评论

0赞 AbraCadaver 3/14/2017
不确定,但你可能想要它或其他什么。bas64_encode()
0赞 user2918057 3/15/2017
@AbraCadaver 谢谢,你明白了...... :)

答: 暂无答案