Piotr Grociak
Piotr Grociak

Reputation: 101

PHP black screen instead of image

I am trying to simply show an image with code bellow:

<?php
$plik = 'test.JPG';
header('Content-Type: image/jpeg');
$percent = 0.1;

list($width, $height) = getimagesize($plik);
$new_width = $width * $percent;
$new_height = $height * $percent;
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($plik);        
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

imagejpeg($image_p, NULL, 70);

imagedestroy($image_p); ?> 

unfortunetlly instead of image I can see only black screen with small sqare inside. enter image description here

  1. The file test.JPG exist and it is inside the same catolgue with file obraz.php which consist only code above.
  2. I'm running it on Chrome.
  3. PHP Version 5.2.11
  4. GD Version bundled (2.0.34 compatible)

Any clue what can I do to fix it?

Upvotes: 3

Views: 5592

Answers (4)

Lone Wolf
Lone Wolf

Reputation: 444

try this, Place ob_clean(); before the header line .

Your PHP script is emitting a UTF-8 byte order mark (EF BB BF) before outputting the PNG image content. This marker is probably causing PHP to default the content type to text/html. Your subsequent call to set the header is ignored because PHP has already sent the BOM.

calling PHP's ob_clean() function clears output buffer before calling the header function.

I got the answer from the link below.

https://stackoverflow.com/a/22565248/10349485

Upvotes: 0

cmprogram
cmprogram

Reputation: 1884

Am I missing something, or can you simply end your php tag and place an HTML IMG instead?

<?php
$plik = 'test.JPG'; 
$percent = 0.1; 
list($width, $height) = getimagesize($plik); 
$new_width = $width * $percent;
$new_height = $height * $percent; 
?> 

<img alt="test image" src="<?php echo $plik;?>" height="<?php echo $new_height;?>" width="<?php echo $new_width;?>"> 

Unless I am grossly missing something, I don't understand the need for such a complicated method of creating an image when you can just do this.

Upvotes: 2

Piotr Grociak
Piotr Grociak

Reputation: 101

So I put the file on other server and check it. There was a Warning about the header (probably that it can't be declared again). I checked and read about this warning and found a solution to my problem. I simply opened the file with notepad++ and formated the text to UTF-8 no BOM. That made my file working!

Thanks guys for all the help and effort!

Upvotes: 0

Stack Underflow
Stack Underflow

Reputation: 2453

To diagnose a problem like this, I suggest making sure that PHP error reporting is on and checking if you get any error from PHP.

It seems you are running low on available memory and getting a fatal error when trying to allocate memory with $image = imagecreatefromjpeg($plik);.

You can increase the allowed memory size with

ini_set('memory_limit', '16M');
ini_set('memory_limit', -1); // no limit

But you may want to make sure your application is not leaking memory.

The PHP memory management documentation may also be useful.

Upvotes: 0

Related Questions