issue while outputting the image in code igniter - php

I'm new to code igniter and learning with how to display images in html page. But the images displays file but it also displays the image tags alt="" also. Here is my code.
views/admin.php
<div class="row">
<?php
//print_r($images_disp);
foreach($images_disp as $item=>$val){
?>
<div class="col-md-3">
<img src="<?php echo base_url().''.$val['imagepath'];?>" width="150px" height="150px" alt="'<?php echo $val['name']?>'" >
</div>
<?php
}
?>
</div>
controller/AddProduct_controller
public function index(){
$sql['images_disp'] = $this->addProduct_model->show_images();
/* echo "<pre>";
print_r($sql);
echo "</pre>"; */
$this->load->view('admin',$sql);
}
model/AddProduct_model
public function show_images(){
$query = $this->db->get('db_images');
$query = $query->result_array();
return $query;
}
HERE IS THE ISSUE IN BELOW IMAGE
OUTPUT IMAGE
THANKS IN ADVANCE

In your scenario name column contains double quotes(") which is causing problem in echo statement. Double quotes causing alt attribute value issue.
Replace your line of code with this line :
<img src="<?php echo base_url().''.$val['imagepath'];?>" width="150px" height="150px" alt="'<?php echo html_escape($val['name']); ?>'" >
html_escape will escape the html character from your string.
Advice :
Always avoid inserting quotes and other character directly in database, escape user input before inserting in database.
At time of insert escape user input like this.
$escaped_str = $this->db->escape($input_str);
$this->db->escape() method will escape all the quotes from your string.

Related

Escape quotes on onclick metho

I have click method generated by php echo. It does not render as it should be. It shows as in the attached image.
my code is
echo "<div class='col-sm-3' onclick='ViewItem('".$item['item_id']."')' style='cursor:pointer'>";
how can I escape the quotes to get the following
<div class='col-sm-3' onclick='ViewItem("1")' style='cursor:pointer'>
We have all been there looking at code too long.
$item['item_id'] = '6';
echo "<div class='col-sm-3' onclick='ViewItem(\"".$item['item_id']."\")' style='cursor:pointer'>";
Output:
<div class='col-sm-3' onclick='ViewItem("6")' style='cursor:pointer'>

Problem displaying images saved as blob images

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.

Getting string for database and use it as img src

I have problem looping out pictures from a database.
I have no problem getting the information(string) that I need from the database. The problems accure when I try to use the string as a img src-tag.
My code:
<?php
foreach ($console as $con):
echo '<li>', $con['brand'], ', ',$con['pic'], '</li>';
echo'<div class="item2">
<a href="xboxconsol.html">
<img src=',$con['pic'],'/>
</a>
</div>';
endforeach;
?>
Note, the li-elements put out the correct information but when I use it in the img-tag it adds a "/" at the end on the string.
Example of list output:
Playstation, ../playstation.jpg
The picutre however output the URL to the pic as:
../playstation.jpg/
Where do the last "/" come from and how do I get rid of it?
You need to quote the attribute value.
echo'<div class="item2">
<a href="xboxconsol.html">
<img src="',$con['pic'],'"/>
</a>
</div>';
Currently the browser is guessing what the attribute should contain.
Which comes out as:
<img src="value/">
but you want:
<img src="value"/>
You need to quote your concatenations, be very careful how you output HTML from PHP.
I'd rather concatenate the variables like this instead:
foreach ($console as $con)
{
echo "<li>{$con['brand']} {$con['pic']}</li>";
echo "<div class='item2'>
<a href='xboxconsol.html'>
<img src=\"{$con['pic']}\" />
</a>
</div>";
}
It's way cleaner and more readable.

Unable to Display Images from MySQL

I am unable to display images from MySQL. I've checked path etc. but still there are broken images on site. Below is my Code. Please guide me.
<div id="right_content">
<div id="headline">
<div id="headline_content"> <b>Welcome Guest!</b> <b style="color:#F56013">Shopping Cart</b> <span>-Items: -Price: </span> </div>
</div>
<div id="products_box">
<?php
$get_products ="SELECT * FROM products ORDER BY rand() LIMIT 0,6";
$run_products = mysqli_query($con,$get_products);
while($row_products=mysqli_fetch_array($run_products)){
$prod_id = $row_products['product_ID'];
$prod_title = $row_products['product_title'];
$prod_cat = $row_products['category_ID'];
$prod_brand = $row_products['brand_ID'];
$prod_price = $row_products['product_price'];
$prod_desc = $row_products['product_desc'];
$prod_img =$row_products['product_img1'];
echo "
<div id='single_product'>
<h3>$prod_title</h3>
<img src='admin_area/product_images/$prod_img' width='180' height='180' />
</div>
";
}
?>
</div>
As per comment, problem is solved. The images were not moving into the directory and i didn't check that. When i manually copied them into the specified directory the problem was gone.
When you write html please note that you should always use double and not single quotes for tag properties. So for example
src='admin_area/product_images/$prod_img'
should be
src="admin_area/product_images/$prod_img"
and so on. Since you are doing echo using double quotes you should escape double quotes with .
So your echo should look like:
echo "
<div id=\"single_product\">
<h3>$prod_title</h3>
<img src=\"admin_area/product_images/$prod_img\" width=\"180\" height=\"180\" />
</div>
";
IMHO it is better to write plain html and use php only to echo variables just to avoid this kind of problems with the generated code. So I'd write:
?>
<div id="single_product">
<h3><?php echo $prod_title; ?></h3>
<img src="admin_area/product_images/<?php echo $prod_img; ?>" width=\"180\" height=\"180\" />
</div>
<?php } ?>
This will save you a lot of headache in case of debugging if your code is not working

Error displaying blob image in php/html

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

Categories