...

View Full Version : Images in PHP script



paddy100
01-25-2010, 02:59 PM
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



<?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>";

}
?>

Fou-Lu
01-25-2010, 03:18 PM
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.

Dormilich
01-25-2010, 03:21 PM
you need a separate script that passes the binary data to the browser. imagine something like (pseudo code)

// HTML script //
// connect & query to DB

echo '<img src="img.php?id=', $row['id'], '" alt="">';

// image script //
// connect to DB

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

// get the data
echo $data;

paddy100
01-25-2010, 04:02 PM
Thanks for input guys,

Im obv doing something wrong!

Have so far got this




<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...




<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/shownews.php?news_id=23


source code around this point says



<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>



:confused:

Fou-Lu
01-25-2010, 04:13 PM
First, $row['id'] is complex. So it cannot be evaluated as you have it here. The , should be periods:


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:


<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.


$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.

bdl
01-25-2010, 04:14 PM
This is essentially paraphrasing the portion of your script that creates the IMG tag. Does this look right to you?


echo "<img src='img.php?id=', $row[id],'>";


Here's your markup:


<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:


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).

bdl
01-25-2010, 04:16 PM
Ah, Fou-Lu beat me to the punch.


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.



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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum