Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Images in PHP script

    Hi guys!

    I ve decided after weighing up loads of pros & cons to store an image for news stories within my SQL DB as a BLOB.

    Now the news stories may or may not have one image attached. I have managed to get the "admin" part of the site working correctly but now I am having issues displaying the images which would be either JPGs or GIFS.

    On the news page I would want them to follow the story itself. Have banged my head against a wall so far though and haven't been able to get working. The field within the DB is called pix.

    Can anyone please suggest how to add this to the below php script? It would be very much appreciated!!!

    Thanks Patrick

    Code:
    <?php
    
    /* Program to display the full profile, based on selection in profiles.php*/
    
    include("rhino.inc");
    
    $connection = mysql_connect($host, $user, $password)
    or die ("couldn't connect to server");
    $db = mysql_select_db($database, $connection)
    or die ("couldn't connect to database");
    
    /*this gets all the records for the urn chosen on prev page */
    $get_news = "SELECT * FROM news WHERE urn = $_GET[news_id]";
    $get_news_res = mysql_query($get_news) or die ("something wrong with query");
    $nrows = mysql_num_rows($get_news_res);
    /*display the news text in the banner */
    for ($i=0;$i<$nrows;$i++)
    {
    $n = $i +1; /* add 1 so that numbers dont start with 0 */
    $row = mysql_fetch_array($get_news_res);
    extract($row);
    
    echo "<table width='550'><tr><td> $title</td></tr>
    	<tr><td>added $date by $author </td></tr>
    	<tr><td>$main</td></tr>
    
    	</table>";
    
    }
    ?>
    Last edited by vinyl-junkie; 01-26-2010 at 01:00 AM. Reason: Added code tags

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    The only thing you'd add to this is an <img src="path/to/my/script.php?pixID=whatever" alt="" />.
    Images require their own script to serve them. You need to send the proper headers to identify the script as an image itself. So you cannot just embed the binary into the html code (though I believe FF will handle base64 encoded data, I assume its limited in size).

    So, you'll need to write a new script, when given a unique id of sorts will look up the associated pix data and serve the image with a simple print. I hope you're storing what type of image it is as well, otherwise you'll need to either scan the binary to determine the image type, or try to construct the image with the createimagefrom* methods so you can send its image type.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    you need a separate script that passes the binary data to the browser. imagine something like (pseudo code)
    PHP Code:
    // HTML script //
    // connect & query to DB

    echo '<img src="img.php?id='$row['id'], '" alt="…">'
    PHP Code:
    // image script //
    // connect to DB

    // $sql fetches the binary image data
    $res mysql_query(sprintf($sqlvalidate($_GET['id'])));

    // get the data
    echo $data

  • #4
    New Coder
    Join Date
    Jan 2010
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for input guys,

    Im obv doing something wrong!

    Have so far got this

    Code:
    <HTML>
    <BODY>
    <?php
    
    /* Program to display the full profile, based on selection in profiles.php*/
    
    include("rhino.inc");
    
    $connection = mysql_connect($host, $user, $password)
    or die ("couldn't connect to server");
    $db = mysql_select_db($database, $connection)
    or die ("couldn't connect to database");
    
    /*this gets all the records for the urn chosen on prev page */
    $get_news = "SELECT * FROM news WHERE urn = $_GET[news_id]";
    $get_news_res = mysql_query($get_news) or die ("something wrong with query");
    $nrows = mysql_num_rows($get_news_res);
    /*display the news text in the banner */
    for ($i=0;$i<$nrows;$i++)
    {
    $n = $i +1; /* add 1 so that numbers dont start with 0 */
    $row = mysql_fetch_array($get_news_res);
    extract($row);
    
    echo "<table width='550'><tr><td> $title</td></tr>
    	<tr><td>added $date by $author </td></tr>
    	<tr><td>$main</td></tr>
    	<tr><td><img src='img.php?id=', $row[id],'> </td></tr>
    	</table>";
    
    }
    ?>
    </BODY>
    </HTML>
    with the img.php...

    Code:
    <HTML>
    <BODY>
    <?php
    
    /* Program to display the full profile, based on selection in profiles.php*/
    
    include("rhino.inc");
    
    $connection = mysql_connect($host, $user, $password)
    or die ("couldn't connect to server");
    $db = mysql_select_db($database, $connection)
    or die ("couldn't connect to database");
    
    // $sql fetches the binary image data
    $res = mysql_query(sprintf($sql, validate($_GET['id'])));
    
    // get the data
    echo $data;
    ?>
    </BODY>
    </HTML>
    experiencing no errors now just

    http://www.hainaultbulldogs.co.uk/sh...php?news_id=23


    source code around this point says

    Code:
    <table width='550'><tr><td> test pic</td></tr>
    	<tr><td>added 2010-01-25 16:53:57 by test </td></tr>
    	<tr><td>testing the pic</td></tr>
    	<tr><td><img src='img.php?id=', ,'> </td></tr>
    	</table>

    Last edited by vinyl-junkie; 01-26-2010 at 01:02 AM. Reason: Added code tags

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    First, $row['id'] is complex. So it cannot be evaluated as you have it here. The , should be periods:
    PHP Code:
    echo "<table width='550'><tr><td> $title</td></tr>
    <tr><td>added $date by $author </td></tr>
    <tr><td>$main</td></tr>
    <tr><td><img src='img.php?id=" 
    $row['id'] . "'> </td></tr>
    </table>"

    Next, the img.php script is wrong. You cannot have any text in it, so remove this part:
    PHP Code:
    <HTML>
    <BODY>
    <?php
    And ensure that the <?php is the very first line of the code. You'll need to send a header to identify the image type, which I presume you've been storing in you're database.
    PHP Code:
    $sQry 'SELECT pix, imagetype FROM pixTable WHERE id = ' . (int)$_GET['id'];
    // I assumed an integer for id, if not, clean it out with that validate function
    if ($result mysql_query($sQry))
    {
        if (
    $row mysql_fetch_assoc($result))
        {
            
    header('Content-type: ' $result['imagetype']);
            print 
    $result['pix'];
        }

    Something like that. You MUST send the header to identify the content-type, which will be something like image/jpeg, image/png, etc. otherwise the browser doesn't know how to render it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #6
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    This is essentially paraphrasing the portion of your script that creates the IMG tag. Does this look right to you?
    PHP Code:
    echo "<img src='img.php?id=', $row[id],'>"
    Here's your markup:
    Code:
    <table width='550'><tr><td> test pic</td></tr>
    <tr><td>added 2010-01-25 16:53:57 by test </td></tr>
    <tr><td>testing the pic</td></tr>
    <tr><td><img src='img.php?id=', ,'> </td></tr>
    </table>
    What appears to be happening is twofold; you're not wrapping the IMG tag attributes correctly, and the $row[id] value is not coming through. For that matter, you use extract() on the $row array, then reference the `id` field as $row[id].

    Try this:
    PHP Code:
    echo "<img src='img.php?id={$id}'>"
    Obviously that's not your entire string, but you can see the difference.

    Also, are you certain the field name is `id` and not `ID`?

    Note, you're using straight REQUEST data (i.e. $_GET[news_id]) in your SQL statement. Be sure you validate and escape this data!!

    Please be sure to use the PHP code tags when you post (or for that matter, learn how to properly open and close the CODE / PHP tags).
    Last edited by bdl; 01-25-2010 at 04:17 PM.

  • #7
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Ah, Fou-Lu beat me to the punch.

    Quote Originally Posted by Fou-Lu View Post
    First, $row['id'] is complex. So it cannot be evaluated as you have it here. The , should be periods:
    Technically the commas are fine as concatenation operators, just not in this context.

    Quote Originally Posted by Fou-Lu View Post
    Something like that. You MUST send the header to identify the content-type, which will be something like image/jpeg, image/png, etc. otherwise the browser doesn't know how to render it.
    I neglected to mention this, but this was the next step once the IMG tag nonsense was cleared up.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •