This is my PHP script which displays a radio station's schedule:
<div class="divider"></div>
<div class="main" style="width:552px;">
<img src="$image" width=115 height=60>
<div class="time">$airtime</div>
<div class="show"><h3><a href="$link><b>$presenter</b></a></h3>
<p>$showdesc</p></div>
<div class="footer"></div>
</div>
</div>
<div class="footer"></div>
<div class="bottom"></div>
</div>
The values with the dollar sign represent the field names in my database, which is radiopresenters.
How would I get this to work as a PHP script, and display the values from the database?
All values in the fields are stored in TEXT format, apart from the image field which is stored in BLOB format.
Airtime is stored in a separate database entitled as radioschedule which has all 4 fields ib, and I intend to link these together via some relational means.
What's the best way to get it to display as the above, especially the BLOB part?
What you want to do is set up an show_image.php script, Example
show_image.php
<?php
$id = (isset($_GET['blid']) && is_numeric($_GET['blid'])) (int)$_GET['blid'] : false;
if($id)
{
if(false !==($res = mysql_query('SELECT blob_data FROM table WHERE blob_id = ' . $id))
{
$data = mysql_fetch_assoc($res);
header("Content-type: image/jpg"); //Send the content Type here.
print $data['blob_data'];
exit;
}
}
?>
Then within your html files you would do the follwing.
<div class="divider"></div>
<div class="main" style="width:552px;">
<img src="show_image.php?id=<?php echo (int)$id?>" width=115 height=60>
<div class="time">$airtime</div>
<div class="show">
<h3><a href="$link><b>$presenter</b></a></h3>
<p>$showdesc</p></div>
<div class="footer"></div>
</div>
</div>
<div class="footer"></div>
<div class="bottom"></div>
That's roughly how its done, another pointer is when your selecting your data via your main page, you should not select the blob data as it will slow your application down, and show_image.php may require more work as its for example purposes only
Peace.
You could write the image stored as a blob in the database out to a file. You could then use a url as your image source.
Assuming $presenter doesn't have any file/url reserved characters in it, and $image is stored as an accurate binary jpg (or png or gif etc.) , you could do:
<?php
if($fh = fopen("/webroot/images/{$presenter}.jpg", "wb")) {
fwrite($fh, $image) ;
fclose($fh) ;
}
?>
<img src="/images/<? echo $presenter ; ?>.jpg" width="115" height="60">
I would suggest that you work out some way of caching the file, so it doesn't have to be written out for every page load though.
Are you asking how to put those values into your file?
As for the text ones you could just use
<?=$airtime?>
or if your server setup doesn't support short tags just use
<?php echo $airtime?>
I would just do this inline with your php.
Example:
<div class="time"><?=$airtime?></div>
As for the BLOB, I would advise not doing this and just storing the image on your server and store the image file's name in the db. BUT if you are intent on doing this I think the only way you can do this is by having a separate script that returns an image file. Something like this:
<?php
// Do all your db stuff here, grab the blob file. Probably from a $_GET paramater
$image = $row['image'];
header("Content-type: image/jpeg"); // or gif, etc...
echo $image;
die();
?>
Then in your other file you would do something like:
<img src="imagescript.php?person_id=<?=$person_id?>" width=115 height=60>
Related
am having an issue displaying images stored as blob in my database, as the images doesn't display rather is shows crazy characters and symbols, i tried to change the tags for the php but with no avail. Help and here is my code: Note that i want all types of images to be saved as blob (jpg, jpeg, gif, svg, png) - thanks in advance
<?php
include 'include/connect.php';
$sql = "SELECT * FROM room_details ORDER BY id ASC LIMIT 2, 1;";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
?>
<div class="room-thumb"><?php echo '<img alt="room 3" class="img-responsive" src="data:image;base64,'.$row['image'].'">'; ?>
<div class="mask">
<div class="main">
<h5><?= $row['room_type']; ?></h5>
<div class="price"><?= $row['room_name']; ?><span>a night</span></div>
</div>
Read More
</div>
</div>
} ?>
Since the blob url is decoded with base64, you need to encode it first. You can use either JavaScript or PHP to do this.
JavaScript:
With JavaScript, you can just use the atob() function to encode your base64 url like this:
<script>
var x = <?php echo $row['image'] ?>
document.getELementById("code").innerHTML = atob(x);
</script>
<div class="room-thumb"><?php echo '<img alt="room 3" class="img-responsive" src="data:image;base64,' ?><span id="code"></span><?php echo '">'; ?>
<!-- DIV CONTENT HERE -->
</div>
However, as stated in this other SO thread, it would be a better approach if you use PHP itself or other JavaScript approaches (mentioned in that same thread) to retrieve and encode the base64 url as the above approach is not secure.
PHP:
With PHP, you can either encode the base64 url before outputting it into your HTML like this:
<?php echo '<img alt="room 3" class="img-responsive" src="data:image;base64,' . base64_encode(.$row['image']).'">'; ?>
Or you can create a new php file, encode the PHP in that file and then echo the encoded base64 url to your HTML like this:
<!-- PHP file -->
<?php
$id = (isset($_GET['id']) && is_numeric($_GET['id'])) ? intval($_GET['id']) : 0;
$image = base64_encode(getImageFromDatabase($id)); // your code to fetch the image
header('Content-Type: image/jpeg');
echo $image;
?>
<!-- HTML -->
<img src="image.php?id=<?php echo $image_id; ?>" />
Check out the accepted answer in this other SO thread for a more in-depth explanation of the above two PHP approaches.
I have this problem when I read a picture from the database.
It shows like this:
This my code:
<?php
include 'Header.php';
include 'yzd.php';
?>
<div class="container">
<h1> All Project </h1>
<?php
$posts = mysqli_query($db_connect,"select * from Project order by id desc");
while($post = mysqli_fetch_array($posts)){
echo "
<article>
<a href='Project.php?id=$post[id]'>
<h1>$post[NameProject]</h1>
</a>
<p class='text-muted']>
<h3>$post[TypeProject]</h3>
</p>
<h4>$post[Description]</h4>
$post[Pic];
<p>-----------------------------------------------------------------------------</p>
</article>";
}
?>
[![enter image description here][2]][2]
Try converting the raw image output into a base 64 data URI (see here)
you may be able to do this by changing
$post[Pic]
into
<img src='".'data:image/png;base64,' . base64_encode($post["Pic"])."'/>
Given all of the images in your databases are stroed as png(s) The type may need to be varied dependent on image type.
Please in the future use code snippets and not screenshots.
In this little snippet of code ,i show how i take the "foto1" column of my database and transfer the value of it to a variable in c# named $foto.
The $foto contains the path of the image corresponding to the product that is showing up. Ive tried to copy and paste the path and ditch out the php part and it works. But when i put it in img src it gives me like the broken image thing.And i cant figure out why it does that.
All help is aprecciated .
Have a nice day :)
<div class="row shop_box-top">
<?php
$ligaBD=odbc_connect('basededadospap','','');
$sql="SELECT * FROM produto WHERE nome_produto LIKE '%ADIDAS%'";
$resultado=odbc_exec($ligaBD,$sql);
?>
<div class="row shop_box-top">
<?php
while (odbc_fetch_row($resultado))
{
$nome = odbc_result($resultado,2);
$preco= odbc_result($resultado,4);
$foto = odbc_result($resultado,9);
?>
<div class="col-md-3 shop_box"><a href="stansmithflatwhite.html">
<img src="<?php echo $foto; ?>" class="img-responsive" alt=""/>
<span class="new-box">
<span class="new-label">Novo</span>
</span>
<div class="shop_desc">
<h3><?php echo $nome; ?></h3>
<span class="actual"><?php echo $preco; ?></span><br>
</div>
</a></div>
<?php }?>
depends of what path contains the $foto var. If is the absolute path, you have to retrive the relative path.
Try also to append an / or an http[s] in front of the path
<img src="/<?php echo $foto;?>">
So it would be : //path/to/photo
As I can see it in your comment, your image paths contain spaces, so a possible solution can be to use urlencode() before echoing them.
Try passing full path to img tag like http://localhost/xyz/images/Cal�ado/Adidas/Homem/Stan Smith/ADIDAS STAN SMITH - RED/ch-adidas-stan-smith-red-5.jpg.
Replace "localhost/xyz" with your website directory path.
I have this code :
http://jsfiddle.net/S4rD9/
(i know the preview is messed up, it's just to show you the PHP code part I have)
Basicly, this is connected to my databse, and it takes the informations inside the text fields of each columns.
As you can see in the code, elements are fetched like this :
$row['img2']
Means it displays the information in the text field of the "img2" column.
The "img2" column has image path in it.
The part of the code that I'm having trouble with looks like this :
<div id="slides" class="col-md-7" >
<img src="'.$row['img2'].'" >
</div>
And I need it to use the "explode" function (I think that's the one I need to use), to get each values in that img2 column textbox in the database, seperated by a comma.
Right now in the database, the text field of the first row (for example) of the img2 column is :
img/img-araignee-big.jpg, img/img-bug-big.jpg
But it only gets the "img/img-araignee-big.jpg" part.
Someone sent me this :
<div id="slides" class="col-md-7" >
<?php
$images = explode(',', $row['img2']);
foreach($images as $image) {
?>
<img src="<?php echo $image; ?>" >
<?php } ?>
</div>
He said it should work, but I cannot get this to work correctly.
<div id="slides" class="col-md-7" >
<?php
$images = explode(',', $row['img2']);
foreach($images as $image) {
print '<img src="' . $image . '">';
} ?>
</div>
I am trying to display image from a blob field of a MySQL table. Looks like I have some sort of error in the following line. As soon as I put "header("Content-type: image/jpeg")" things get messed up and instead of displaying webpage, all source code of the page is displayed.
Please let me know how to correct.
<div class="image" align="left">
<a href="<?php header("Content-type: image/jpeg"); echo $rec['image']; ?>">
<img src="<?php echo $rec['image']; ?>" width="150" border="0"/>
</a>
</div><!-- image -->
You normally don't put the actual image contents in the src= attribute of the image tag. Instead, you point to the URL of an image file.
(There are ways to include the image source directly in the HTML, but it doesn't work consistantly with all browsers, and you still won't have your <a> link working properly.
Instead, the best way to do this is to create a separate PHP file to serve the image.
Your HTML:
<div class="image" align="left">
<img src="myimage.php?key=<?php echo($key) ?>" width="150" border="0"/>
</div><!-- image -->
myimage.php:
<?php
header("Content-type: image/jpeg");
$key = $_GET['key'];
// todo: load image for $key from database
echo $rec['image'];
You're trying to put the image data inline inside the content. The only feasible way to do this is via a Data URI data URI. Something like:
<img src="data:image/jpeg;base64,<?= base64_encode($rec['image']) ?>" width="150" border="0" />
However, what you probably want to do is put it into a separate script. So your HTML would be:
<img src="showimage.php?id=XXX" width="150" border="0" />
And your showimage.php script would be:
<?php
// Get $rec from database based on the $_GET['id']
header('Content-Type: image/jpeg');
echo $rec['image'];
?>
I've done something like that retrieving blob from my database in another way that you may find useful, here is the code example.. see if it suits your needs and if you needed anymore help let me know.
while ($row = mysql_fetch_array($hc_query2)) {
$title = $row['title'];
$text = $row['text'];
$image = $row ['image'];
$output ='<div class="HCInstance"><img src="data:image/jpeg;base64,' . base64_encode($image) . '" alt="High Council" width="100px" height="100px"/>
<div class="HCHeader"><h2>'.$title.'</h2></div><br/><div class="HCDetails"><p>'.$text.'</p></div></div>';
echo $output;
}