3

Here's my code. I'm trying to put image in a div by resizing it. But it is showing this error :"Cannot modify header information,headers already sent by (output started at F:\xampp\htdocs............"

<div class="image" >
<?php header('Content-Type: image/jpeg');
$image=$link;
echo $link;     
$image_size=getimagesize($image);
$image_width=$image_size[0];
$image_height=$image_size[1];
$new_width=200;
$new_height=200;
$new_image = imagecreatetruecolor($new_width,$new_height);
$old_image = imagecreatefromjpeg($image);
imagecopyresized($new_image,$old_image,0,0,0,0,$new_width,$new_height,$image_width,$image_height);
imagejpeg($new_image);
?>

</div>
Masum
  • 145
  • 7
  • Code is missing.. Please add. – vicente Jan 23 '15 at 17:03
  • 1
    Headers must be sent before any output. – Niloct Jan 23 '15 at 17:06
  • *But it is not working* - That isn't much of an explanation as to why it's failing. Neither is *But it is not responding.* - Visit http://php.net/manual/en/function.error-reporting.php and use that. – Funk Forty Niner Jan 23 '15 at 17:07
  • [`Cannot modify header information,headers already sent by...`](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) this question stands to be closed because of this. Edit: It was. – Funk Forty Niner Jan 23 '15 at 17:14
  • PS. This question is not a duplicate of the one entered. This is not the headers already sent error only - this is the inputing php image inside the php page problem. This is much more complicated, and header already sent is just a "result of using wrong method" error. And it is clear how this error works, but it is not clear how to achieve the image in code effect... Look at my answer, and you will see what I mean. – Jacek Kowalewski Jan 23 '15 at 17:24

1 Answers1

2
  1. You can use php header, only if nothing was outputed before... In that case, You have entered <div class="image" >, and the standard text header was send. In that case sending another header is WRONG.

  2. As mentioned in comments, <?php - needs space after.

  3. The solution is simple. Create another php file, and call it image.php. In that file, enter this code (inside <?php ?> tags):

    header('Content-Type: image/jpeg');
    $image=$link;
    $image_size=getimagesize($image);
    $image_width=$image_size[0];
    $image_height=$image_size[1];
    $new_width=200;
    $new_height=200;
    $new_image = imagecreatetruecolor($new_width,$new_height);
    $old_image = imagecreatefromjpeg($image);
    imagecopyresized($new_image,$old_image,0,0,0,0,$new_width,$new_height,$image_width,$image_height);
    imagejpeg($new_image);
    

In Your original code, you can use this:

<div class="image" >
    <img src="image.php">
</div>

In that case, php script is visible as an image. With the header that suggest it is an image. In the real code, You only give a source of this image, which is image.php. The extension, is not important (normally we write image.jpg for exxample), but the headers of that file (header('Content-Type: image/jpeg');) tells browser that this is really an image.


Additional note:

If in Your main script you've got some logic, that you are using to create that image, you must pass all this logic to that image using GET method, or rewrite it inside image.php...

For example, if the size is a variable (in that case it is not, but I'm using an example), you can do:

<img src="image.php?width=<?php echo $width; ?>">

And in image.php

$width = isset($_GET['width']) ? $_GET['width'] : 100; // 100 = default

Remember to not pass any IMPORTANT informations using that method. If for example you are producing captcha image, you should not pass tha captcha code in get ;).


I hope that Your image producing code is good. If not, You can remove the first line, and visit image.php in your browser. Without any headers it will treat it as a normal text page, and will display php errors. If there are no errors, just bit-coded-something, then add the header, and it should work.


Hope it helps, and give you a little clue on how the headers work in php. There is no "on the fly" header, each file lives in its own world of headers.

Jacek Kowalewski
  • 2,761
  • 2
  • 23
  • 36