I've created a page "student_picture.php" using the content-type:image/jpeg. im having problem when I want to add other text in that same page..
here is the sample of my code:
<?php
session_start();
if(!isset($_SESSION["StudentNo"])){
header("location:login.php");
}
$StudentNo = $_SESSION['StudentNo'];
require("includes/connection.php");
$sql = "SELECT StudentPicture from dbo.Students where StudentNo = '$StudentNo'";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$img = $row['StudentPicture'];
if ($img == null ) {
echo "<img src='img/default_pic.gif'>";
} else {
$img = trim($img);
header('Content-Type: image/jpeg');
echo $img;
}
echo $StudentNo;
}
?>
The image is successfully displaying but the echo $StudentNo is not displaying.. can anyone help me with my prob? thanks in advance.
You have to exit
your script, because the header
is sent to the client browser. If the $_SESSION["StudentNo"]
is not present the script will process and will try to ouput your image.
<?php
session_start();
if(!isset($_SESSION["StudentNo"])){
header("location:login.php");
die();
}
And that is not a proper way to output an image, echo "<img src='img/default_pic.gif'>";
with header('Content-Type: image/jpeg');
is not ok, either you return without that header or you read the image:
if ($img == null){
$img = 'img/default_pic.gif';
} else {
$img = trim($img);
}
header('Content-Type: image/jpeg');
readfile($img);
or you lose the header tag
if ($img == null){
$img = "<img src='img/default_pic.gif'>";
} else {
$img = "<img src='".trim($img)."'>";
}
echo $img;
So your script could be:
<?php
session_start();
if(!isset($_SESSION["StudentNo"])){
header("location:login.php");
die();
}
$StudentNo = $_SESSION['StudentNo'];
require("includes/connection.php");
$sql = "SELECT StudentPicture from dbo.Students where StudentNo = '$StudentNo'";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$img = $row['StudentPicture'];
if ($img == null){
$img = "<img src='img/default_pic.gif'>";
} else {
$img = "<img src='".trim($img)."'>";
}
echo $img;
echo $StudentNo;
}
?>
I don't know the functions inside your DB connection, but I think you should use something like $row = sqlsrv_fetch_row( $stmt, SQLSRV_FETCH_ASSOC);
and make without that while
You are outputting an image when it is found in the database, you would either need to include a dynamic img via
<img src="anotherscript.php">
Or generate a jpg file using one of the built in image libraries and storing its file name in that database rather than storing the raw data
I think that you cannot display text while using the Content-Type
of image/jpeg
, or any other kind of image format that I know of. Text can only be displayed with text/?
, or other exceptions such as application/pdf
.
If you don't know how to display the image on a separate page while using a php file, just use:
<img src="path/to/yourphpfile.php" />
Just like any other image.
Hope that helped.
You might need to use following code :
<?php
$image = 'http://www.example.com/image.jpg';
$info = getimagesize($image);
header('Content-Type: '.$info['mime']);
echo file_get_contents($image);
exit;
?>