image from MySQL database not working

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

    image from MySQL database not working

    <?php
    include 'dbinc.php';
    $result = mysql_query("SE LECT image,mime_type FROM photos WHERE
    image_id=".intv al($_GET['id']), $link) or die("MyErr:".my sql_error());
    if ($row = mysql_fetch_ass oc($result)) {
    $ext=array('ima ge/jpeg'=>'jpg','i mage/gif'=>'gif','im age/png'=>'png','im age/x-png'=>'png');
    header("Expires : Mon, 26 Jul 2030 05:00:00 GMT");
    header("Content-Type: $row[mime_type]");
    header("Content-Disposition: inline;
    $_GET[id].".$ext[$row['mime_type']]);
    echo "\r\n".stripsla shes($row['image']);
    }
    mysql_free_resu lt($result);
    ?>



    what am I doing wrong? I don't get an image. I've seen thumbnail stuff. I
    don't want a thumbnail (that code didn't work either for some reason).


  • Jerry Stuckle

    #2
    Re: image from MySQL database not working

    Jim Michaels wrote:[color=blue]
    > <?php
    > include 'dbinc.php';
    > $result = mysql_query("SE LECT image,mime_type FROM photos WHERE
    > image_id=".intv al($_GET['id']), $link) or die("MyErr:".my sql_error());
    > if ($row = mysql_fetch_ass oc($result)) {
    > $ext=array('ima ge/jpeg'=>'jpg','i mage/gif'=>'gif','im age/png'=>'png','im age/x-png'=>'png');
    > header("Expires : Mon, 26 Jul 2030 05:00:00 GMT");
    > header("Content-Type: $row[mime_type]");
    > header("Content-Disposition: inline;
    > $_GET[id].".$ext[$row['mime_type']]);
    > echo "\r\n".stripsla shes($row['image']);
    > }
    > mysql_free_resu lt($result);
    > ?>
    >
    >
    >
    > what am I doing wrong? I don't get an image. I've seen thumbnail stuff. I
    > don't want a thumbnail (that code didn't work either for some reason).
    >
    >[/color]

    I have no idea what you did wrong. What do you get? What's in the source?
    What's in the database row?

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

    Comment

    • foaud167

      #3
      Re: image from MySQL database not working

      hi,
      I think the second header call needs quotes for 'mime_type'

      hope it helps

      Comment

      • Jim Michaels

        #4
        Re: image from MySQL database not working


        "Jerry Stuckle" <jstucklex@attg lobal.net> wrote in message
        news:Df2dnciza_ 0aONzZnZ2dnUVZ_ vidnZ2d@comcast .com...[color=blue]
        > Jim Michaels wrote:[color=green]
        >> <?php
        >> include 'dbinc.php';
        >> $result = mysql_query("SE LECT image,mime_type FROM photos WHERE
        >> image_id=".intv al($_GET['id']), $link) or die("MyErr:".my sql_error());
        >> if ($row = mysql_fetch_ass oc($result)) {
        >>
        >> $ext=array('ima ge/jpeg'=>'jpg','i mage/gif'=>'gif','im age/png'=>'png','im age/x-png'=>'png');
        >> header("Expires : Mon, 26 Jul 2030 05:00:00 GMT");
        >> header("Content-Type: $row[mime_type]");
        >> header("Content-Disposition: inline;
        >> $_GET[id].".$ext[$row['mime_type']]);
        >> echo "\r\n".stripsla shes($row['image']);
        >> }
        >> mysql_free_resu lt($result);
        >> ?>
        >>
        >> what am I doing wrong? I don't get an image. I've seen thumbnail stuff.
        >> I don't want a thumbnail (that code didn't work either for some reason).[/color]
        >
        > I have no idea what you did wrong. What do you get? What's in the
        > source? What's in the database row?[/color]

        found the problem. stripslashes and the newlines turned out to be a bug. so
        echo "\r\n".stripsla shes($row['image']);
        became
        echo $row['image'];

        and the problem I was having with error messages about headers already being
        sent came because dbinc.php had some blank lines after the ?>
        :-/
        well, at least I found it.
        The Expires header isn't necessary. I should probably take it out. The ;
        filename after inline isn't necessary either. (maybe it make things easier
        for the cache, but I disabled the page cache via headers elsewhere)

        I found another method would be to use imagecreatefrom string() and that
        somehow you can detect the mime-type from that rather than storing it in the
        db, butyou also have the problem that if the image is hefty (1MB), that
        function will consume 16MB of RAM. combined with the use of another common
        function, it would take a total of 32MB. PHP would then die. this function
        is a memory hog (why?). Then you use imagejpeg().

        I *thought* you were supposed to send a newline to signify end of headers
        before sending image data. It seems I was wrong. I got the right size
        image placeholder, but no image. weird. guess I'd have to look at the
        format to see why that is. (?) maybe PHP does some of this for me
        automatically or something behind the scenes.
        From the command-line I discovered after I sent out headers, imagejpeg()
        secretly inserts \r\n into the image data. I had to get out my editor to
        binary-delete the first 2 bytes for the image to come out right. my old
        copy of TSE 2.8 is still good for a few things. :-)



        Comment

        • Jim Michaels

          #5
          Re: image from MySQL database not working


          "foaud167" <f.mardini@gmai l.com> wrote in message
          news:1145217016 .401545.232700@ j33g2000cwa.goo glegroups.com.. .[color=blue]
          > hi,
          > I think the second header call needs quotes for 'mime_type'[/color]

          that would cause an error.

          [color=blue]
          >
          > hope it helps
          >[/color]


          Comment

          Working...