So basically if I go to the getImage link, the image that comes from the database is displayed, but if I use it in a .php file where it will display the image but will resize it to fit the card, it won't show and the alt (which is avatar) shows.
<center>
<img src="getImage.php" class="w3-circle" style="position:absolute; bottom:-20%; left:35.5%; width:30%" alt="Avatar">
</center>
Then the code for the getImage.php:
<?php
session_start();
require './Database.php';
// do some validation here to ensure id is safe
$sql = "SELECT register.FULLNAME, register.IMAGE, gameData.NBA_SCORE FROM register inner join gameData on register.ID = gameData.ID WHERE NBA_SCORE = (select max(NEW_SCORE) from gameData)";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($result);
echo "<img src='".$row['IMAGE']."'>";
?>
Anything wrong with what I am doing?
Problem
HTML trys to get an valid image file like jpg or png but gets an php/text file with the content <img src="...">.
Solution
You need to change the php files content type to an image and output the image files data:
header("Content-Type: image/jpeg");//or image/png
echo file_get_contents("$imagepath");//file path not url!!!
Code
<?php
session_start();
require './Database.php';
header("Content-Type: image/jpeg");
// do some validation here to ensure id is safe
$sql = "SELECT register.FULLNAME, register.IMAGE, gameData.NBA_SCORE FROM register inner join gameData on register.ID = gameData.ID WHERE NBA_SCORE = (select max(NEW_SCORE) from gameData)";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($result);
$path = $row['IMAGE'];//Maybe you need to change this if you only save an url in the database
echo file_get_contents($path);
?>
Related
I am trying to retrieve a png image file from the database
Here is the call from within the <img> tag inside body:
<img src="..\BankLogin\man.php?id=2" style="width:128px;height:150px">
Here's the man.php file:
<?php
$link = mysqli_connect("localhost","root","","images");
$imgId = $_GET['id'];
if (!empty($imgId)) {
$sqliCommand = "SELECT image FROM images WHERE id = $imgId";
$result = mysqli_query($sqliCommand,$link);
$row = mysqli_fetch_assoc($result);
mysqli_close($link);
header("Content-type: image/png");
echo $row['image'];
}
?>
On running the code i just get an image frame with an 'unloaded image'(am i saying it correct?).I am pretty sure something is wrong in the man.php file, maybe in echo $row['image']. I am not sure how to go about making it right. Any help with this would be great.
The function mysqli_close should be called after the image data is echoed. This is because it destroys the result sets.
Also please fix the SQL Injection vulnerability:
$imgId = (int)$_GET['id'];
If you want to retrieve the image which is stored as BLOB type in phpmyadmin you have to echo it as follows.
echo '<img src="data:image/jpeg;base64,'.base64_encode( $rows['image'] ).'"/>'
Example:
To Retrieve the BLOB image from the DB you have to do like this.
<?php
$db = mysqli_connect("localhost","root","","dbname"); //keep your db name
$query = "SELECT * FROM image WHERE id = $id";
$sth = $db->query($query);
$fetch=$sth->fetch_assoc();
echo '<img src="data:image/jpeg;base64,'.base64_encode( $fetch['image'] ).'"/>';
?>
For Inserting the image you need to follow the procedure like this So that if you encode it as base 64 you can retrieve the image perfectly without any error.
<?php
$conn = mysqli_connect("localhost","root","","DbName"); //keep your db name
$single_image = addslashes(file_get_contents($_FILES['images']['tmp_name']));
//U have to keep your DB table column name for insertion. I keep image type Blob.
$query = "INSERT INTO image (image) VALUES('".$single_image."')";
$SQL = mysqli_query($conn, $query);
?>
This is my php file.
$con=mysql_connect("localhost","root","");
mysql_select_db("food",$con);
$SQL = "SELECT image FROM info WHERE Type='Fruit Pizza' ";
$RESULT = mysql_query("$SQL");
$ROW = mysql_fetch_assoc($RESULT);
header("Content-type: image/png");
echo $ROW['image'];
?>
This is how i add the image into database.
$data = addslashes(file_get_contents('dinner2.png'));
mysql_query("INSERT INTO info(image, Name, Price, type) VALUES('$data','Fruit Pizza','8', 'png')");
Is there anything wrong with my code?? The problem is everytime i try to retrieve the image it show a small grey box instead of the picture.Anyone can help??
SOLVED!
so ive created an image upload page which saves the image to a folder and sends the file name to the DB, ive checked to see if they are actually being added to the DB and folder which they are, but when i call the data to another page to display the images i get broken images.
below is my code to call the images, its some code ive scraped together from various tutorials as they gave me the same problem as im having now.
UPDATE:
ive managed to get the images showing but now im faced with being shown the same image for each row of data called, the id and img_name and right for each row but the image is always the same as the first listed.
UPDATED CODE:
<?php
//connect to database
include ('connect.php');
//save the name of image in table
$query = mysql_query("select * from tbl_img") or die(mysql_error());
//retrieve all image from database and store them in a variable
while ($row = mysql_fetch_assoc($query))
{
$img_name = $row['img'];
}
?>
<?php
include ('connect.php');
$img_id = mysql_query("SELECT * FROM tbl_img");
while ($row = mysql_fetch_assoc($img_id))
{
$id = $row['img_id'];
echo "
$id<br>
$img_name<br>
<img src='http://localhost/testarea/include/site_images/$img_name' />
";
echo "<br><br><br></p>
";
}
?>
If you start the path of image with / you mean an absolute path where / is the DocumentRoot folder (or the directory of virtualhost)
With src ="includes/xxx/image.png" you mean that includes is in the same folder with the php script. If it is not you can use relative path like
src="../includes/xxx/image.png" for example
<?php
include ('connect.php');
$img_id = mysql_query("SELECT * FROM tbl_img");
while ($row = mysql_fetch_assoc($img_id))
{
$img_name = $row['img'];
$id = $row['img_id'];
echo "
$id<br>
$img_name<br>
<img src='http://localhost/testarea/include/site_images/$img_name' />
";
echo "<br><br><br></p>
";
}
How can i use php to get an image url from database (based on user id) and display it out as an image.
http://mysite.com/img.php?id=338576
The code below shows a php script goes to a database , check whether the specific user(using the id in the link above) exist then get the image url out of that user's row.
<?php
//connect to database
include ("connect.php");
//Get the values
$id = mysql_real_escape_string($_GET['id']);
//get the image url
$result = mysql_query("SELECT * FROM users WHERE id='$id'")
or die(mysql_error());
$row = mysql_fetch_array($result);
if($row)
{
$img_url = row['imgurl'];
mysql_close($connect);
}
else{
}
?>
The $Img_url is an actual image link www.asite.com/image.jpg
The problem is how can i use php to make an image from the image link($img_url)? By which http://mysite.com/img.php?id=338576 will turn into an image.
I hope someone could guide me
Thanks!
The easiest way:
header('Location: url/to/image');
You could also proxy the request, which uses your bandwidth:
echo(file_get_contents('url/to/image'));
This is pretty basic stuff. Am I understanding you correctly? I would just do this:
<?php
$img_html = '<img src="' . $img_url . '" />';
echo $img_html;
?>
Or check this answer:
How do I script a php file to display an image like <img src="/img.php?imageID=32" />?
I am trying to get the image which is stored in the blob format in Mysql Database.I am using the following code.
index.php
<img src='image.php?questionid=questionid&question=question1&answer=answer1&author=myname' />
image.php
<?php
require_once('dbconfiguration.php');
$sql="SELECT image FROM tablename WHERE questionid='{$_GET['questionid']}' and question='{$_GET['question']}' and answer='{$_GET['answer']}' and author='{$_GET['author']}'";
$STH = $DBH->query($sql);
$STH->setFetchMode(PDO::FETCH_ASSOC);
$row = $STH->fetch();
ob_clean();
header("content-type: image/jpg") ;
echo $row['image'] ;
?>
In index.php I didn't get any image.So When I enter the URL for image.php I am getting the following error in forefox.
The image “URL” cannot be displayed because it contains errors.
I am using php5.3 and mysql 5.1.36 .
What I did wrong.I went through almost all forums and no clues.Please help me on this.
It should be:
<?php
require_once('dbconfiguration.php');
$sql="SELECT image FROM tablename WHERE questionid='{$_GET['questionid']}' and question='{$_GET['question']}' and answer='{$_GET['answer']}' and author='{$_GET['author']}'";
$STH = $DBH->query($sql);
$STH->setFetchMode(PDO::FETCH_ASSOC);
$row = $STH->fetch();
ob_clean();
header("content-type: image/jpg") ;
echo $row['image'] ;
?>
As your database field is called image not Efaq_image