如何不将图像输出到浏览器?

时间:2015-05-23 09:13:58

标签: php

我的代码:

ob_start();
imagejpeg($resource, NULL, 75);  break; // best quality
$resource = ob_get_contents();
ob_end_flush();

我只使用imagejpeg()进行输出缓冲,我不需要输出到浏览器。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

让我们试着分析一下你在那里做了什么:

// start output buffering
ob_start();
// output the image - since ob is on: buffer it
imagejpeg($resource, NULL, 75);
// this break could be a problem - if this is in a control structure, remove it
break;
// save the ob in $resouce
$resource = ob_get_contents();

// here is the image now in $resource AND in the output buffer since you didn't clean it (the ob)

// end ob and flush (= send the ob)
ob_end_flush();

所以你做错了,你1)没有clean输出缓冲和/或2)flushed ob。

我的建议是使用ob_get_cleanreference)(简单示例):

$im = imagecreatetruecolor(120, 20);
ob_start();
imagejpeg($im);
$var = ob_get_clean();

答案 1 :(得分:0)

如果它处于循环中,则会中断该过程。因此OB不会被关闭,输出结束于解析过程。此外,您不必冲洗,但要清洁。使用:

ob_start();
imagejpeg($resource, NULL, 75); // best quality
$resource = ob_get_contents();
ob_end_clean();
break;

或者:

ob_start();
imagejpeg($resource, NULL, 75); // best quality
$resource = ob_get_contents();
ob_clean();
// Some other code
ob_end_flush(); // Output the rest
break;