Below are coding that I currently use to display images from database :
$con = mysqli_connect('localhost','root','','demo') or die('Unable To connect');
$query = mysqli_query($con,"SELECT * FROM images");
while($row=mysqli_fetch_assoc($query))
{
echo '<img src="data:image/png;base64,' . base64_encode($row['image']) . '" />';
}
Problem with those coding : It only display images with .png extension because I am using echo '<img src="data:image/png;base64,' . base64_encode($row['image']) . '" />'; to display it.
What I am trying to do : I wanted to display images with extension .jpeg .png and .gif as well.
Is there anybody that can help me with other simple way of displaying images? Thank you.
Store the format in the database as well:
$con = mysqli_connect('localhost','root','','demo') or die('Unable To connect');
$query = mysqli_query($con,"SELECT * FROM images");
while($row=mysqli_fetch_assoc($query))
{
echo '<img src="data:image/' . $row['format'] . ';base64,' . base64_encode($row['image']) . '" />';
}
Hi Just get the image extension and use it
$ext = pathinfo($row['image'], PATHINFO_EXTENSION);
echo '<img src="data:image/' . $ext . ';base64,' . base64_encode($row['image']) . '" />';
#FrankerZ already gave correct solution but if you have already stored base64 images on your database it will be problematic to add format column.
You can discover the mime type of image from base64 like that;
$img = $row['image']; // base64 of your image
$fileInfo = finfo_open(); // file info resource
$mimeType = finfo_buffer($fileInfo, $img, FILEINFO_MIME_TYPE);
If you have already existing records, do not apply this methodology on your each iteration. Simply add new column to your database, and fill/update these new colums using this methodology. After that, use #FrankerZ 's solution.
Related
Image is stored in database (as varchar(255)), but on page isn't shown.
This is code that stored image, and put's image in folder:
if (isset($_FILES["image"])) {
$title = date("dmyHms") . "_" . $_FILES["image"]["name"];
$path = "img/profile/" . $_POST["id"] . "_" . $title;
move_uploaded_file($_FILES["image"]["tmp_name"], $path);
}
And this is to display image:
foreach ($conn->results() as $conn):
$img = $_SERVER["CONTEXT_DOCUMENT_ROOT"] . $path . "img/profile" . $conn->id . "_" . $conn->image;
if (file_exists($img)) {
$image = $path . "img/profile/" . $conn->id . "_" . $conn->image;
} else {
$image = $path . "img/noimage.png";
}
<?php endforeach; ?>
When I print_r $img, it shows right path to image and still image is not displayed.
Display:
<?php echo $image; ?>
Where did you echoing your image? If it is inside foreach, you need to change your code little bit...
$image = '<img src="'.$path . "img/profile/" . $conn->id . "_" . $conn->image . '" />';
Hope, will work perfectly for you.... TQ
The following line is not correct:
foreach ($conn->results() as $conn):
Don't overwrite $conn. Change the variable to something different like $result
I am not really good in PHP but the images that will show on the webpage of mine will be the main resolution. I would like to downscale it but I have no clue how.
<?php
$images = glob('*.{gif,png,jpg,jpeg}', GLOB_BRACE); // Formats to look for
$num_of_files = 2; // Number of images to display
foreach ($images as $image) {
$num_of_files--;
if ($num_of_files > -1) // This made me laugh when I wrote it
{
echo "<b>" . $image . "</b><br>"
. "Created on " . date('D, d M y H:i:s', filemtime($image)) . "<br>"
. "<img src='" . $image . "'><br><br>";
} // Display images
else {
break;
}
}
?>
It seems like you just want to resize the image shown in the browser, right? In that case some simple styling would do the trick:
. "<img src='" . $image . "' style='width: 200px'><br><br>";
Change the 200px to whatever width you want, the height will scale proportionally.
You should also have a look on how to use CSS classes.
If you rather want to scale the actual image file (i.e. if it resides on your server), check out ImageMagick.
UPDATE.
I can only display the images inside a query e.g.
if($Result = mysqli_query($Link, $Query)){
while($row = mysqli_fetch_array($Result))
{
$img = $row['path'];
echo '<img src="'.$img.'">';
}
}
I want to display them inside a table but this code doesn't work :
<td class="tcgimagecell" colspan="5"><?php echo '<img src="'.$img.'">'; ?></td>
BELOW ISSUE RESOLVED :
I have images stored in a folder (uploads) and the path is stored in a database table in a field named path. I am trying to display the stored images.
$Link = mysqli_connect($Host, $User, $Password, $Database);
$Query = "SELECT * FROM $images";
if($Result = mysqli_query($Link, $Query)){
while($row = mysqli_fetch_array($Result))
{
$img = "uploads/" . $_FILES["file"]["name"];
echo '<img src="'.$img.'">';
}
}
Your folder permissions for "uploads/" might not be set to public read. Have you checked those already? What is the URL that is output for your image? Have you tried Copying URL and linked directly to it from your browser? If the URL is coming back blank, try $row instead of $_FILES since you are just wanting the path to the file.
This line is wrong: $img = "uploads/" . $_FILES["file"]["name"];.
Replace it with $img = "uploads/" . $row[x]; where x is the number of column where file name is
OR
with $img = "uploads/" . $row['columnname'];. Here you have to replace columnname.
I need to build a dynamic image gallery plugin for Joomla! that will go into a specific folder and pull out all images from the folder and show first of them as a large preview image and the rest in a list. Afterwards I will need to make the preview image open in a lightbox if clicked and in the lightbox I need to have the small thumbnails of listed images as well.
But know I need just php to go to the folder and pull out all the images from the folder specified. I have googled a bit and found some solution but this does not work for some reason I don't understand. Could someone tell me please, what is wrong with the code?
Thanks!
<div id="images">
<?php
$images_dir = 'images/';
$scan = scandir($images_dir);
echo '<img src="' . $images_dir . $scan[2] . '"alt="image" />';
?>
<ul id="smallimages">
<?php
for($i=0; $i<count($scan); $i++){
$ext = substr($scan[$i], strpos($scan[$i], '.'), strlen($scan[$i]-1));
$filetypes = array('.jpg', '.JPEG', '.jpeg');
if(in_array($ext, $filetypes)){
echo '<li><img src="' . $images_dir . $scan[$i] . '" alt="' . $scan[$i] . '"></li>';} }?></ul>
</div>
I think it's because you haven't defined the path correctly. Try using the following:
$images_dir = JUri::root() . 'plugins/content/plg_new/images';
JUri::root() is the root of your Joomla site so change the path from there on accordingly to where ever your images are located
Hope this helps
Does this work for you?
<?php
$image_directory="hrevert/images/";
$pictures = glob("$image_directory*.{gif,jpg,png}", GLOB_BRACE);
//displays the first image
$first_img=reset($pictures);
echo "<img src=\"".$first_img."\" width='20px' />";
//loops through other images and prints them
echo "<ul>";
foreach($pictures as $picture) {
echo "<a href='$picture'><img src=\"".$picture."\" width='20px' /></a>";
}
echo "</ul>"
?>
My PHP knowledge is kind of limited, and I was trying to search for different PHP/JavaScript solutions, but nothing helped me to make it work, so I'd just try to ask the question directly.
I have the following PHP code that loads all the images from a specific folder on server:
foreach(glob($images_path . '*.*') as $filename){
echo '<img src="' . $filename . '" class="preview" />';
}
Now, in order to give different styles to the images, based on their kind, I want to recognize the extension of the file, whether with JavaScript/jQuery or PHP, and to put it as a class (preferably, but could be some other idea)... let's say, after the class "preview" which I already have in the code. It could be similar to the extension, just without the dot, of course; e.g. class="preview png".
Thank you very much in advance!
foreach(glob($images_path . '*.*') as $filename){
$ext = pathinfo($filename, PATHINFO_EXTENSION);
echo '<img src="' . $filename . '" class="preview ' . $ext . '" />';
}
try with this.
foreach(glob($images_path . '*.*') as $filename){
$extension = str_replace('.', '', strrchr($filename, '.'));
echo '<img src="' . $filename . '" class="preview '. $extension .'" />';
}