Image issues
I am trying to retrieve an image stored in a blob field in a database
and display it in an html document. I call it into the file where I
want to display it using the following code:
<?php echo '<img src="echo_image.php" title="Personal Thumbnail."
alt="No image available.">';?>
The file echo_image.php contains the following code:
<?php
$tid = '18';
// Connect to the database
require_once ('../includes/scripts/mysql_connect_bh.php');
// Create the query
$sql = "SELECT image FROM tbl_person WHERE person_id=$tid";
// Query the database
$result = mysql_query("$sql") or die("Invalid query: " .
mysql_error());
// Send header
header("Content-type: image");
// Echo the image
echo mysql_result($result, 0);
?>
This works fine. The image is placed in my original document,
The problem is that I want send an id to the echo_image.php file so I
can tell it which record to select the image from. When I set a
session variable in the first file and try to retrieve it in the second
file like this:
<?php
session_start();
$_SESSION['id'] = 18;
php echo '<img src="echo_image.php" title="Personal Thumbnail."
alt="No image available.">';
?>
<?php
$tid = $_SESSION['id'];
// Connect to the database
require_once ('../includes/scripts/mysql_connect_bh.php');
// Create the query
$sql = "SELECT image FROM tbl_person WHERE person_id=$tid";
// Query the database
$result = mysql_query("$sql") or die("Invalid query: " .
mysql_error());
// Send header
header("Content-type: image");
// Echo the image
echo mysql_result($result, 0);
?>
I get an error saying "no image available.
How can I send an id value from the first file to the second so that I
can indicate which record to retrieve the image from?
Is there some other way to do this?
Also, why doesn't the following code work in the second file. When I
do this, the image data gets returned rather than the interpreted
image.
<?php
require_once ('../includes/scripts/mysql_connect_bh.php');
$tid = '18';
// Create the query
$sql = "SELECT image FROM tbl_person WHERE person_id=$tid";
// Query the database
$result = mysql_query("$sql") or die("Invalid query: " .
mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
// Send header
header("Content-type: image");
echo $row['image'];
Re: Image issues
kenoli wrote:
> // Send header
> header("Content-type: image");
>
Invalid content-type, should be image/jpg, image/jpeg, image/gif or
image/png.
> The problem is that I want send an id to the echo_image.php file so I
> can tell it which record to select the image from. When I set a
> session variable in the first file and try to retrieve it in the
> second file like this:
>
> <?php
> $tid = $_SESSION['id'];
>
You are forgetting to start the session with session_start():
session_start();
$tid = $_SESSION['id'];
Alternatively, you could also pass the ID through the image URL and read it
from there:
echo "<img src='echo_image.php?id=$id' ...>";
Make sure that you check that the ID is actually a number.
JW
Re: Image issues
In article <1168327641.173650.214650 [at] v33g2000cwv.googlegroups.com>,
kenoli <kenoli [at] igc.org> wrote:
> The problem is that I want send an id to the echo_image.php file so I
> can tell it which record to select the image from. When I set a
> session variable in the first file and try to retrieve it in the second
> file like this:
You don't need to set a session variable. You just need to adapt your
model of thinking slightly. Since you are effectively calling a PHP
page to generate your image, stop thinking about the resulting image,
and start thinking about the fact that you are working with PHP (
*hint* you can pass parameters! )
I have not validated this code, but the gist of it should be apparent.
<!-- ============ -->
<!-- = Page one = -->
<!-- ============ -->
<?php $id = 123456789; // some arbitrary integer identifier ?>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=utf-8"/>
<title>Testing image</title>
</head>
<body>
<img src="getimage.php?<?=$id?>" alt="Profile image" />
</body>
</html>
<!-- ========================== -->
<!-- = Page 2 :: getimage.php = -->
<!-- ========================== -->
<?php
// Start an output buffer
ob_start();
// Import whatever you need to here
require_once('dbStuff.php');
// Get the querystring
$id = $_SERVER['QUERY_STRING'];
// Check the querystring ( demo expects a number)
// IT IS IMPORTANT TO FILTER YOUR DATA
if( !is_numeric( $id ) )
trigger_error( "Bad ID value", E_USER_ERROR );
// Get the data from the DB...
$result = mysql_query("blah blah");
// clear the buffer
if(ob_get_length()) ob_clean();
// Send a new header ( depends on the mime type )
header("Content-type: image/jpg");
// Dump the image
echo $mysql_result($result,0);
// Stop processing
exit;
?>
--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
Re: Image issues
Thanks JW & Koncept --
I feel silly forgetting to open the session. Of course.
Thanks both for elaborating on passing the info via the URL. I had
actually tried it and it hadn't worked. Must have been a syntax error.
I have been finding that much of my struggle with php comes from not
working from the right perspective. I've appreciated your posts over
time, Koncept, for the assitance they have given me in "thinking php."
This is the stuff that doesn't get communicated via php.net (some in
the user comments) or even most books on php that I've read. I'm
starting to find that some of the more "advanced" books are actually
the most useful for learning php because they get into the headspace
more than the beginners books. It would be useful if more authors
understood how important the conceptual framework is rather than just
feeding code examples and explaining the mechanics.
Incidentally, there is a great book that really communicates the spirit
of Javascript in this way called "PPK on Javascript." Wish someone
would write womething similar on php.
Interested, Koncept????
--Kenoli
Janwillem Borleffs wrote:
> kenoli wrote:
> > // Send header
> > header("Content-type: image");
> >
>
> Invalid content-type, should be image/jpg, image/jpeg, image/gif or
> image/png.
>
> > The problem is that I want send an id to the echo_image.php file so I
> > can tell it which record to select the image from. When I set a
> > session variable in the first file and try to retrieve it in the
> > second file like this:
> >
> > <?php
> > $tid = $_SESSION['id'];
> >
>
> You are forgetting to start the session with session_start():
>
> session_start();
> $tid = $_SESSION['id'];
>
> Alternatively, you could also pass the ID through the image URL and read it
> from there:
>
> echo "<img src='echo_image.php?id=$id' ...>";
>
> Make sure that you check that the ID is actually a number.
>
>
> JW
Re: Image issues
In article <1168531778.932870.265940 [at] p59g2000hsd.googlegroups.com>,
kenoli <kenoli [at] igc.org> wrote:
> Incidentally, there is a great book that really communicates the spirit
> of Javascript in this way called "PPK on Javascript." Wish someone
> would write womething similar on php.
>
> Interested, Koncept????
I've got a long way to go still my friend, but each day I keep chipping
away at it. I appreciate the kind words though.
--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche