Hello,
I'm writing a simple code to output the image to the browser with imagejpeg(). When I run the code, no image is being displayed, all i get is the text "http://localhost/modifyimage.php" (modifyimage.php is the file that contains the code).

$image=imagecreatefromjpeg('pic1.jpg');
imagefilter($image, IMG_FILTER_GRAYSCALE);
header("Content-type:image/jpeg");
imagejpeg($image);

I can save the image to the correct folder using imagejpeg($image, $newfile) but can't output it to the browser. Am I missing something obvious? Any help would be appreciated, thx

    i guess this is not such a newbie question after all. since nobody here seems to know the answer, should i post this in either the general help or coding forum instead?

      Try some debugging:

      <?php
      ini_set('display_errors', 1);
      error_reporting(E_ALL);
      $image=imagecreatefromjpeg('pic1.jpg') or die('create failed');
      imagefilter($image, IMG_FILTER_GRAYSCALE) or die('imagefilter() failed');
      header("Content-Type:image/jpeg"); // "Type" should be capitalized, but probably doesn't matter
      imagejpeg($image);
      

        I tried your code but there's no error msg and I'm still getting the same output. The funny thing is when i replace the line imagejpeg($image) with imagejpeg($image, $newfilename), it works like it's supposed to. I'm running php on Apache server, does this have anything to do with configuring Apache to output the image correctly?

          I tried running it locally, and it worked OK for me.

          I'm running PHP 5.2.6, Apache 2.2.8, GD 2.0.34 on a Windows Vista pc. I viewed it in both Firefox 3 and IE 7.

          Maybe just for the heck of it, you could try adding the following headers:

          <?php
          ini_set('display_errors', 1);
          error_reporting(E_ALL);
          $image=imagecreatefromjpeg('rmnp8.jpg') or die('create failed');
          imagefilter($image, IMG_FILTER_GRAYSCALE) or die('imagefilter() failed');
          header("Content-Type:image/jpeg");
          header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
          header('Content-transfer-encoding: binary');
          imagejpeg($image);
          

          (Note: I have a different original image filename in the above code.)

            no change, same output, even with those headers.

            you don't think this is a configuration prob with either php or apache?

            when i go to view page source, i see gibberish, the page title says "modifyimage.php (jpeg image)" and when i go to open the image file directly from "http://localhost/images/pic1.jpg" it works correctly. i did extensive debugging and imagejpeg($image) reports successful operation but for some reason the only thing being displayed on the browser is the text "http://localhost/modifyimage.php"

              Is there any other code in the script besides the code you've shown us, above? To serve up an image like that, the image data must be the only thing output by the script, so I was just wondering if anything else might be outputting something (even a newline following a closing "?>" might screw things up.)

              Other than that, you've stumped me. 🙁

                na, this is just a simple test script, i only have those 4 lines in the file. something in the image stream isn't working right, no i take that back, the imagejpeg() function reported successful execution. I think it's either something in the browser or Apache server at this point, unless I need to enable something in php that i don't know about.....

                  nice pic Nogdog!!, that's exactly what i'm trying to write my script to do. this is baffling, i'll have to find out how to config apache and php to stream images correctly, assuming that's what the prob is...

                  but if anyone has any suggestions, please don't hesitate

                    Write a Reply...