2

I am trying to set the header('Content-Type: image/jpeg'); in one of my files called resizeImage.php (I'm following the tutorial from http://php.net/manual/en/function.imagecopyresampled.php). And I kept getting this error: Warning: Cannot modify header information - headers already sent by (output started at /home/sites/aejhyun.com/public_html/Syrian Project/index.php:557). So I did some research and stumbled upon a very good post called How to fix "Headers already sent" error in PHP and looked at http://www.w3schools.com/php/func_http_header.asp. I tried doing what the tutorials told me to do but none of them worked.

To give you some context my resizeImage.php code is this:

<?php
    $directory = "uploads/";
    $images = glob($directory."*.jpeg");
    header('Content-Type: image/jpeg');
    foreach($images as $filename) {

        // Set a maximum height and width
        $width = 200;
        $height = 200;

        // Get new dimensions
        list($width_orig, $height_orig) = getimagesize($filename);

        $ratio_orig = $width_orig/$height_orig;

        if ($width/$height > $ratio_orig) {
           $width = $height*$ratio_orig;
        } else {
           $height = $width/$ratio_orig;
        }

        // Resample
        $image_p = imagecreatetruecolor($width, $height);
        $image = imagecreatefromjpeg($filename);
        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

        // Output
        imagejpeg($image_p, null, 100);
    }
?>

I kept getting the error, I tried adding ob_start() to my php file but that still caused the error. I tried erasing header('Content-Type: image/jpeg'); from my php file and I tried adding it all the way to the top of my index.php file like so:

<?php
    header('Content-Type: image/jpeg');
?>
<!doctype html>

But that caused all my content on my website to disappear with a picture error icon showing all the way in the left corner. Like so:

enter image description here

Anybody know how to fix this problem?

Community
  • 1
  • 1
Jae Kim
  • 625
  • 3
  • 12
  • 23
  • You have to set the headers before *any* output, which includes the ` `. That doctype tag doesn't make any sense, though, when you're trying to output an image. That's why your images are broken. This is not a good question for this site; even reading what you have written makes the problem obvious. – elixenide Oct 17 '15 at 04:58
  • Don't set the header before any echo and print statement. – Deepak saini Oct 17 '15 at 04:59
  • 1
    @EdCottrell, yea... I am super noob. I am just so confused. I spent so long trying to figure this out... I'm trying to learn this on my own and I would like some guidance... – Jae Kim Oct 17 '15 at 05:02
  • When you say my doctype doesn't make sense what do you mean? – Jae Kim Oct 17 '15 at 05:03
  • @Deepak saini, I thought you were supposed to? All PHP headers have to be before echo and print statements no? – Jae Kim Oct 17 '15 at 05:03
  • @EdCottrell what is the problem? I don't see it. I wish it was obvious to me. – Jae Kim Oct 17 '15 at 05:05
  • @JaeKim think about it. You are telling the browser you are sending an image. Then you are sending the text ` ` as part of the response. So, even if you send an image later, you have already hopelessly corrupted it by injecting this text. – elixenide Oct 17 '15 at 05:07
  • I'm confused. You are trying to process uploaded image right ? Why are you sending the `header('Content-Type: image/jpeg');` to the browser ? Please explain. – frz3993 Oct 17 '15 at 05:27
  • You might as well ditch the header. – frz3993 Oct 17 '15 at 05:36

1 Answers1

1

header() must be called before any actual output is sent.

but if you still got error this mean the php file saved with headers , you should save php file as utf8 without Bom

<?php
/* This will give an error. Note the output
 * above, which is before the header() call */
header('Location: http://www.example.com/');
exit;
?>
Kodr.F
  • 13,932
  • 13
  • 46
  • 91