How can I not display an image in the browser?

My code:

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

      

I am using imagejpeg () only to buffer the output, I don't need to output to the browser. Any ideas?

+3


source to share


2 answers


Try to analyze what you did there:

// 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();

      

So what did you do wrong that you 1) not clean the outputbuffer and / or 2) reset the vol.



My recommendation would be to use ob_get_clean

( link ) (simple example):

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

      

+2


source


You break the process if it is in a loop. Thus, the OB will not be closed and the output will be at the end of the parsing process. Plus, you don't need to hide but clean. Using:

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

      



Or:

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

      

0


source







All Articles