Edited and started over to better describe my issue.
I have this code that displays an image from a database using blob data.
function getContent($db) {
$query = "SELECT name, animalid, image, thumb FROM images ";
$sql = $db->prepare($query);
$sql->execute();
return $sql->fetchAll();
}
$data = getContent($db);
foreach($data as $row) {
$id = $row['id'];
$image = $row['image'];
$thumb = $row['thumb'];
echo '<img src="data:thumb/jpg;charset=utf8;base64,'.$image.'">';
}
This code displays a image on the page, when viewing the source code i see the blob data that the image is made from, what i need is to actually make an image that i can name properly.
How do i make a ******.jpg from the blob data?
Use base64_encode. You should use image/jpeg MIME type without charset.
function getContent($db) {
$query = "SELECT name, animalid, image, thumb FROM images ";
$sql = $db->prepare($query);
$sql->execute();
return $sql->fetchAll();
}
$data = getContent($db);
foreach($data as $row) {
$id = $row['id'];
$image = $row['image'];
$thumb = $row['thumb'];
// $image is the BLOB
echo '<img src="data:image/jpeg;base64,'.base64_encode($image).'">';
}
Related
Im trying to show images that i uploaded to the database using longblob and PDO but i always get a black screen with a small white square in the center.
This is the code:
<?php
include('snippets/startDB.php');
include('snippets/Session.php');
$PicNum = $_GET["PicNum"]; echo($PicNum);
$id = $_SESSION['id'];
$stmt = $dbh->prepare('SELECT imagem FROM aluno WHERE id= ?');
$stmt->execute(array($PicNum));
$row = $stmt->fetch();
Header( 'Content-type: image/gif');
echo $row['imagem'];
?>
I think the save file is working correctly because a file appears in the database when i upload the image.
My save file:
<?php
$imagem = $_FILES["imagem"];
include('snippets/startDB.php');
include('snippets/Session.php');
if($imagem != NULL) {
$nomeFinal = time().'.jpg';
if (move_uploaded_file($imagem['tmp_name'], $nomeFinal)) {
$tamanhoImg = filesize($nomeFinal);
$mysqlImg = addslashes(fread(fopen($nomeFinal, "r"), $tamanhoImg));
if($_SESSION['tipo']== 'Aluno'){
$stmt = $dbh->prepare('UPDATE aluno SET imagem = ? WHERE id = ?');
$stmt->execute(array($mysqlImg, $_SESSION["id"]));
}else if($_SESSION['tipo']== 'Professor'){
$stmt = $dbh->prepare('UPDATE professor SET imagem = ? WHERE id = ?') ;
$stmt->execute(array($mysqlImg, $_SESSION["id"])) ;
}
header("location:perfil_aluno.php");
}
}
else {
echo"Erro!";
}
?>
I'm storing images in a folder, and saving their path encoded to base64 in database. The problem is, my images don't display, only their 'broken icons'. I don't know what I'm doing wrong there. Here's a stored image:
data:image/jpg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAQCAwMDAgQDAwMEBAQEBQkGBQUFBQsICAYJDQsNDQ0LDAwOEBQRDg8TDwwMEhgSExUWFxcXDhEZGxkWGhQWFxb/2wBDAQQEBAUFBQoGBgoWDwwPFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhb/wAARCAQ4B4ADASIAAhEBA
and my code:
<?php
require_once "db_connect.php";
if( isset( $_GET['id'])) {
$id = $_GET['id'];
}
if ($stmt = $conn->prepare("SELECT image FROM cardimages WHERE id=?")) {
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$image = $row['image'];
$image_src = "uploads/" . $image;
echo $image_src;
}
$stmt->close();
}
$conn->close();
?>
and in the html:
<img class="card-img-top" src="imageView.php?id=<?php echo $row["id"]; ?>" /
Why can't my php display the image?
<?php
include("sql.php");
//$sql = "SELECT * FROM filesdb WHERE fileid = 5";
$id = $_GET['id'];
// do some validation here to ensure id is safe
$sql = "SELECT * FROM filesdb WHERE fileid =$id";
echo $sql."<br>";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
//header('content-type: image/jpeg');
echo "<br>Fileid:".$row['fileid'];
echo "<br>FileName:".$row['filename'];
header("Content-Type: image/jpeg");
echo "<br>".$row['dbforfile'];
}
it can display fileid and filename, but not dbfofile, which is a BLOB.
If you're downloading the file contents, you must not put HTML elements around it.
You can also only send one image at a time, so there's no point in looping.
<?php
include("sql.php");
//$sql = "SELECT * FROM filesdb WHERE fileid = 5";
$id = $_GET['id'];
// do some validation here to ensure id is safe
$sql = "SELECT dbforfile FROM filesdb WHERE fileid =$id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
header('content-type: image/jpeg');
$row = $result->fetch_assoc();
echo $row['dbforfile'];
}
You are only doing one image, so you don't need a loop. But if you want to do multiples; you need to use an img tag and call another PHP script as if it were an image in the src and have that script output the image (with headers). Or you can use base64 encoded data as the image source in the tag:
while($row = $result->fetch_assoc()) {
echo "<br>Fileid:".$row['fileid'];
echo "<br>FileName:".$row['filename'];
echo "<br>";
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['dbforfile']).'"/>';
}
How can I get the base64 value properly in PHP from a submitted data?
The data being passed on is
data:image/png;base64,LongBase64ValueHereOfAnImage
Right now, I can only get it by
$data = $_POST['image'];
list($type, $data) = explode(';', $data);
list($data) = explode(',', $data);
$data = base64_decode($data);
Is there a proper way to get the base64 value?
This could be done in one line with regex
$data = $_POST['image'];
$data = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $data));
try this: uploading image code.
if (count($_FILES) > 0) {
if (is_uploaded_file($_FILES['image']['tmp_name'])) {
$conn = mysqli_connect('localhost', 'username', 'password', 'databasename');
$imgData = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$imageProperties = getimageSize($_FILES['image']['tmp_name']);
$sql = "INSERT INTO upload(image) VALUES('$imgData')";
$current_id = mysqli_query($conn, $sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysqli_error($conn));
if (isset($current_id)) {
echo 'done';
}
}
}
And then get data using base64
$sql = "SELECT * FROM upload WHERE id = 113";
$sth = $conn->query($sql);
$result = mysqli_fetch_array($sth);
echo '<img src="data:image/jpeg;base64,' . base64_encode($result['image']) . '"/>';
I'm storing in DB only profile and profile heading images. [BLOB]
If i change my profile picture or my header picture its works fine, its storing in my DB.
The funny thing is that when i go back one page the last uploaded image appears again.
I recorded it here in this video
Watch it so you will understand whats going on.
I want to know why this happens?
this are my codes.
function profile_pic(){
//uploads imgs
global $conn;
if(isset($_FILES['proImage']['tmp_name'])){
$img = file_get_contents($_FILES['proImage']['tmp_name']);
$imgName = addslashes($_FILES['proImage']['name']);
$imgSize = getimagesize($_FILES['proImage']['tmp_name']);
if ($imgSize == false){
$_SESSION['empError'] = 'Error';
}else{
$stmt = $conn->prepare("UPDATE users SET ProfileImage=? , ProfileImageName=? WHERE email=?");
$stmt->bind_param("sss",$img, $imgn, $em);
$img=$img;
$imgn=$imgName;
$em = escape_string($_SESSION['useremail']);
$stmt->execute();
$lastId=$stmt->insert_id;
$_SESSION['empError']= "Updated.";
//return $lastId;
}
}
}
display imgs
function display_pic(){
global $conn;
$stmt = $conn->prepare("SELECT * FROM users WHERE email=?");
$stmt->bind_param("s", $em);
$em = $_SESSION['useremail'];
$stmt->execute();
$result = $stmt->get_result();
$rows = $result->fetch_assoc();
$url =$rows['ProfileImage']; //blob row
$b64 = base64_encode($url);
$uri = 'data:image/jpeg;base64,' . $b64;
echo '<img class="avatar border-white" src="' . $uri . '" />';
}