3

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?

Piotr Grociak
  • 95
  • 2
  • 7
  • [`imagecopyresampled`](http://www.php.net/imagecopyresampled) expects its first 2 arguments to be resources. You're passing a string as its second arg. – Jeto Nov 08 '18 at 22:02
  • @Jeto the example #1 from http://php.net/manual/en/function.imagecopyresampled.php the structure is the same... – Piotr Grociak Nov 08 '18 at 22:08
  • No, it's not. Look closer :) – Jeto Nov 08 '18 at 22:09
  • @Jeto ok I see the mistake and fixed it unfortunetlly the error is the same. I even copy the code from the link I gaved in comment earlier but still the same effect. – Piotr Grociak Nov 08 '18 at 22:21
  • Can you upload your image somewhere and link it? Code seems fine as is. – Jeto Nov 08 '18 at 22:23
  • https://drive.google.com/file/d/13bmP1LLYXxmJxlcInemDnDwLlqNNJCdQ/view?usp=sharing Here is the link for the JPG. – Piotr Grociak Nov 08 '18 at 22:35
  • Open the Chrome dev tools (f12), are you getting an error? – Stack Underflow Nov 08 '18 at 22:43
  • @StackUnderflow no I don't see any errors there. Maybe the problem is that I'm running the code on the server that is in my local network? – Piotr Grociak Nov 08 '18 at 22:51
  • I don't think it should matter that it's on your local network. Do you have error reporting on? Are you getting any error from PHP? [turn on error reporting](https://stackoverflow.com/a/21429652/3291390) – Stack Underflow Nov 09 '18 at 18:10
  • @StackUnderflow ok now we are getting somewhere. I use the code an commented out header(to see the result). I received fatal error: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 19648 bytes) in /mnt/disk/volume1/myweb/obraz.php on line 16 In this line is: $image = imagecreatefromjpeg($plik); – Piotr Grociak Nov 09 '18 at 23:09
  • I posted an answer that will hopefully be useful to others who run into the same issue. – Stack Underflow Nov 10 '18 at 00:32

4 Answers4

2

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.

cmprogram
  • 1,819
  • 2
  • 10
  • 21
0

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.

Stack Underflow
  • 1,458
  • 1
  • 20
  • 33
0

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!

Piotr Grociak
  • 95
  • 2
  • 7
0

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

Lone Wolf
  • 114
  • 6