imagejpeg generating string not image even with header("Content-type:image/jpeg")

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cman

    imagejpeg generating string not image even with header("Content-type:image/jpeg")

    does anyone know why i can't generate images with:
    header("Content-type:image/jpeg");
    imagejpeg($img_ number);

    i've tried different examples but i always get a text output as if the
    header doesn't make a difference at all.

    <?php
    //random_number.p hp
    $img_number = imagecreate(100 ,50);
    $white = imagecoloralloc ate($img_number ,255,255,255);
    $black = imagecoloralloc ate($img_number ,0,0,0);
    $grey_shade = imagecoloralloc ate($img_number ,204,204,204);

    imagefill($img_ number,0,0,$gre y_shade);
    ImageRectangle( $img_number,5,5 ,94,44,$black);
    ImageRectangle( $img_number,0,0 ,99,49,$black);

    header("Content-type:image/jpeg");
    imagejpeg($img_ number);
    ?>

  • samudas

    #2
    Re: imagejpeg generating string not image even with header(&quot;Co ntent-type:image/jpeg&quot;)

    You're code runs fine on my server. Are you outputting some other
    content before this script?

    Comment

    • Chung Leong

      #3
      Re: imagejpeg generating string not image even with header(&quot;Co ntent-type:image/jpeg&quot;)

      cman wrote:[color=blue]
      > does anyone know why i can't generate images with:
      > header("Content-type:image/jpeg");
      > imagejpeg($img_ number);
      >
      > i've tried different examples but i always get a text output as if the
      > header doesn't make a difference at all.
      >
      > <?php
      > //random_number.p hp
      > $img_number = imagecreate(100 ,50);
      > $white = imagecoloralloc ate($img_number ,255,255,255);
      > $black = imagecoloralloc ate($img_number ,0,0,0);
      > $grey_shade = imagecoloralloc ate($img_number ,204,204,204);
      >
      > imagefill($img_ number,0,0,$gre y_shade);
      > ImageRectangle( $img_number,5,5 ,94,44,$black);
      > ImageRectangle( $img_number,0,0 ,99,49,$black);
      >
      > header("Content-type:image/jpeg");
      > imagejpeg($img_ number);
      > ?>[/color]

      You must be testing the code using IE, which has a stupid mime-type
      caching mechanism. If your script had initially produced an error
      message, IE will keep seeing the contents as text.

      Try testing the script again.

      Comment

      • Ewoud Dronkert

        #4
        Re: imagejpeg generating string not image even with header(&quot;Co ntent-type:image/jpeg&quot;)

        cman wrote:[color=blue]
        > header("Content-type:image/jpeg");[/color]

        The standard requires a space after the colon, but the mimetype caching
        issue is the better bet I think.

        --
        E. Dronkert

        Comment

        • cman

          #5
          Re: imagejpeg generating string not image even with header(&quot;Co ntent-type:image/jpeg&quot;)

          no i'm not outputing other content before it, i tried running only that
          script to be sure

          Comment

          • cman

            #6
            Re: imagejpeg generating string not image even with header(&quot;Co ntent-type:image/jpeg&quot;)

            i'm actually testing the code using firefox

            Comment

            • cman

              #7
              Re: imagejpeg generating string not image even with header(&quot;Co ntent-type:image/jpeg&quot;)

              i tried with the space after the colon and it doesn't make a difference
              either

              Comment

              • jshanman

                #8
                Re: imagejpeg generating string not image even with header(&quot;Co ntent-type:image/jpeg&quot;)

                cman wrote:[color=blue]
                > i tried with the space after the colon and it doesn't make a difference
                > either[/color]

                while (@ob_end_clean( ));

                place this before your header function

                if you server has output buffering on, then it won't send the image
                data correctly.

                At least, that is the problem I had for my generated images...

                - JS


                Comment

                • cman

                  #9
                  Re: imagejpeg generating string not image even with header(&quot;Co ntent-type:image/jpeg&quot;)

                  i was hoping that would work, but sadly it didn't
                  i still see "ÿØÿà" in the browser

                  Comment

                  • jshanman

                    #10
                    Re: imagejpeg generating string not image even with header(&quot;Co ntent-type:image/jpeg&quot;)

                    cman wrote:[color=blue]
                    > i tried with the space after the colon and it doesn't make a difference
                    > either[/color]

                    while (@ob_end_clean( ));

                    try adding this before the header. If your server has output buffering
                    on, you need this to generate image.

                    Hope this helps.

                    - JS


                    Comment

                    • Jerry Stuckle

                      #11
                      Re: imagejpeg generating string not image even with header(&quot;Co ntent-type:image/jpeg&quot;)

                      cman wrote:[color=blue]
                      > does anyone know why i can't generate images with:
                      > header("Content-type:image/jpeg");
                      > imagejpeg($img_ number);
                      >
                      > i've tried different examples but i always get a text output as if the
                      > header doesn't make a difference at all.
                      >
                      > <?php
                      > //random_number.p hp
                      > $img_number = imagecreate(100 ,50);
                      > $white = imagecoloralloc ate($img_number ,255,255,255);
                      > $black = imagecoloralloc ate($img_number ,0,0,0);
                      > $grey_shade = imagecoloralloc ate($img_number ,204,204,204);
                      >
                      > imagefill($img_ number,0,0,$gre y_shade);
                      > ImageRectangle( $img_number,5,5 ,94,44,$black);
                      > ImageRectangle( $img_number,0,0 ,99,49,$black);
                      >
                      > header("Content-type:image/jpeg");
                      > imagejpeg($img_ number);
                      > ?>
                      >[/color]

                      Are you including this file in another possibly? Or do you have
                      anything else in the file before this code (even a DOCTYPE)?

                      --
                      =============== ===
                      Remove the "x" from my email address
                      Jerry Stuckle
                      JDS Computer Training Corp.
                      jstucklex@attgl obal.net
                      =============== ===

                      Comment

                      • cman

                        #12
                        Re: imagejpeg generating string not image even with header(&quot;Co ntent-type:image/jpeg&quot;)

                        i'm only running the following code in a file name 'test.php':

                        <?php
                        //random_number.p hp
                        $img_number = imagecreate(100 ,50);
                        $white = imagecoloralloc ate($img_number ,255,255,255);
                        $black = imagecoloralloc ate($img_number ,0,0,0);
                        $grey_shade = imagecoloralloc ate($img_number ,204,204,204);

                        imagefill($img_ number,0,0,$gre y_shade);
                        ImageRectangle( $img_number,5,5 ,94,44,$black);
                        ImageRectangle( $img_number,0,0 ,99,49,$black);

                        header("Content-type: image/jpeg");
                        imagejpeg($img_ number);
                        ?>

                        Comment

                        • jshanman

                          #13
                          Re: imagejpeg generating string not image even with header(&quot;Co ntent-type:image/jpeg&quot;)


                          cman wrote:[color=blue]
                          > i'm only running the following code in a file name 'test.php':
                          >
                          > <?php
                          > //random_number.p hp
                          > $img_number = imagecreate(100 ,50);
                          > $white = imagecoloralloc ate($img_number ,255,255,255);
                          > $black = imagecoloralloc ate($img_number ,0,0,0);
                          > $grey_shade = imagecoloralloc ate($img_number ,204,204,204);
                          >
                          > imagefill($img_ number,0,0,$gre y_shade);
                          > ImageRectangle( $img_number,5,5 ,94,44,$black);
                          > ImageRectangle( $img_number,0,0 ,99,49,$black);
                          >
                          > header("Content-type: image/jpeg");
                          > imagejpeg($img_ number);
                          > ?>[/color]

                          Did you try the

                          while (@ob_end_clean( ));

                          as the first call in your document?

                          - JS


                          Comment

                          • cman

                            #14
                            Re: imagejpeg generating string not image even with header(&quot;Co ntent-type:image/jpeg&quot;)

                            yes i tryed 'while (@ob_end_clean( ));' before header and tried it as
                            the first command also

                            Comment

                            • Jerry Stuckle

                              #15
                              Re: imagejpeg generating string not image even with header(&quot;Co ntent-type:image/jpeg&quot;)

                              cman wrote:[color=blue]
                              > i'm only running the following code in a file name 'test.php':
                              >
                              > <?php
                              > //random_number.p hp
                              > $img_number = imagecreate(100 ,50);
                              > $white = imagecoloralloc ate($img_number ,255,255,255);
                              > $black = imagecoloralloc ate($img_number ,0,0,0);
                              > $grey_shade = imagecoloralloc ate($img_number ,204,204,204);
                              >
                              > imagefill($img_ number,0,0,$gre y_shade);
                              > ImageRectangle( $img_number,5,5 ,94,44,$black);
                              > ImageRectangle( $img_number,0,0 ,99,49,$black);
                              >
                              > header("Content-type: image/jpeg");
                              > imagejpeg($img_ number);
                              > ?>
                              >[/color]

                              OK, then, are you sure you have the correct libs compiled into PHP?

                              It works for me, also (on Firefox).

                              Live HTTP headers is showing a response of :

                              HTTP/1.x 200 OK
                              Date: Fri, 03 Mar 2006 03:03:33 GMT
                              Server: Apache/2.0.50 (Unix) DAV/2 PHP/5.0.4 mod_ssl/2.0.50 OpenSSL/0.9.7e
                              X-Powered-By: PHP/5.0.4
                              Content-Length: 1668
                              Keep-Alive: timeout=15, max=100
                              Connection: Keep-Alive
                              Content-Type: image/jpeg

                              Also, when I view the source, the first line I get is:

                              ÿØÿàJFIFÿþ>CREA TOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality


                              Try reporting all errors to the display and see of you get a message
                              about something wrong.


                              Do you have a URL we can try?

                              --
                              =============== ===
                              Remove the "x" from my email address
                              Jerry Stuckle
                              JDS Computer Training Corp.
                              jstucklex@attgl obal.net
                              =============== ===

                              Comment

                              Working...