Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP unable to show jpg image, shows black screen with small rectangle inside

Tags:

php

jpeg

Trying to open a jpeg image in any browser and it won't show, shows a black screen with a small white rectangle in it.

enter image description here

I've tried setting errors display in php.ini and increasing the memory size. The jpg file is only 219 KB.

ini_set('memory_limit', '16M');
ini_set('memory_limit', -1);

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

However, no errors pop up and it fails to open on IE, chrome, or firefox. I've also checked under the IIS manager under the MIME.types to check if the extension "jpeg, jpg, jpe" was listed there and they are. Below is my PHP code:

$file = 'test.jpg';
header('Content-type: image/jpeg');
$image = file_get_contents($file);
echo $image; 
//readme($file) <-- also fails

Any guidance on this would be greatly appreciated.

like image 467
Yodavish Avatar asked Dec 03 '25 09:12

Yodavish


1 Answers

I had the same problem, solved this issue by adding below ob clean while loop after header

$file = 'test.jpg';
header('Content-type: image/jpeg');
/*It will clean output buffer*/
while (ob_get_level() && @ob_end_clean()) {
        ;
}
$image = file_get_contents($file);
echo $image; 
like image 92
Vijay Maurya Avatar answered Dec 04 '25 22:12

Vijay Maurya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!