Monday 3 September 2018

PHP how to use the content type: image / jpeg correctly? advertisements

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

?>

0 comments:

Post a Comment