Image Search In Folder PHP - php

I am getting an image from user and storing it in a folder. Now using the image name i have to search the image in that same folder.The image is stored correctly in the folder.For search i am using some thing like this :
echo '<img src="upload/'.$thumb.'" " />';
the variable $thumb is having the image name.It is getting the image name like that :
$thumb=$_POST['thumbnailPic'];

if(isset($_REQUEST['thumbnailPic'])) {
$thumb=$_REQUEST['thumbnailPic'];
echo '<img src="upload/'.$thumb.'" />';
}
else {
die("no thumbnailPic is found!");
}
I think you have extra quotes in the image src :
echo '<img src="upload/'.$thumb.'" " />';
^---- this is extra quotes
Should be:
echo '<img src="upload/'.$thumb.'" />';
That quotes makes your img tag inavlid. Also check images permission

Related

Can't display image in PHP

I am using this code to display an image.
echo "<img src=\"".$row["image"]."\">";
Printing $row["image"] gives:
https://www.lesechos.fr/medias/2017/04/19/2080617_hopital-les-candidats-a-la-presidentielle-divergent-sur-les-effectifs-web-0211985257133_300x160.jpg
Which is a valid source when I type it in my browser.
However, no image is displayed... What is wrong?
if you want to show an image, you need to use a img tag. An URL won't be enough. What about :
<?php
echo'<img src=" '.$row["image"].' " alt="my image" />';
/* after edit, I saw that you are using code below */
// echo "<img src=\"".$row["image"]."\">";
// seems fine though
// what about this ->
$path_to_img = $row['image'];
echo "<img src=\"$path_to_img\" alt=\"my image\" />"; /* just added `alt` and last slash to be fully compliant */
?>
what does source inspector show as html output ?
I just tested and hotlinking is fine, imgshows up (kind of an hospital hallway).
you have to print the image url in html src attribute:
echo "<img src='images/." $row['image']. "'/>";

php image database blob

need your help. I am able to upload image to database on a blob field. Now the question is I am not able to display it back, it just puts out a box with out the real image, what is that I am missing.
Here is the code. images is the field that is declared as blob;
$statement->bind_result($notes, $images, $image_type);
$statement->fetch();
echo $images;
echo '<img rc="data:image/jpeg;base64,'.base64_encode($images).'"/>';
you missed src from img tag. use src in place of rc
echo '<img src="data:image/jpeg;base64,'.base64_encode($images).'"/>';
you missing 'r' in your attribute src <img> tag :
echo '<img src="data:image/jpeg;base64,'.base64_encode($images).'"/>';

Load Images in Directory in Sequence, Loading a Filler Image in Place of Missing Images

So i have a set of 20 images in a set, all labeled as dog. So dog01, dog02, dog03, etc. I'm using this code to pull those out of a directory and display them 5 to a row, in 4 rows like so.
dog01 dog02 dog03 dog04 dog05
dog06 dog07 dog08 dog09 dog10 (etc.)
I"m using this code to load the images from a directory, and it is loading them in order.
<?php
$dirname = "images/";
$images = glob($dirname."dog*.png");
foreach ($images as $i=>$image) {
$title = pathinfo($image);
echo '<img class="deck" src="'.$image.'" alt="'. $title['filename'].'" title="'.$title['filename'].'">';
if(($i+1)%5 == 0) echo '<br />';
}
?>
However, I want to see if there is a way to get it so that if one of the dogs is missing from the directory, it instead loads a filler in its place, and continues to load the others in sequence. So if dog03 is missing, it would look like this:
dog01 dog02 filler dog04 dog05
It would show filler, and continue on the sequence. I'm genuinely not sure if I can achieve this in php. If someone knows what approach I need to take here, that would be appreciated. I should note that the filler image is in another directory called "fillers/".
Thanks in advance.
You have two ways of approaching this I think. The first is by not worrying about it in php, and loading the filler image as background image. Normally the image will be loaded over the filler image, but if the image does not load, the filler image stays visible.
The other way is by testing if the file exists.
As background image:
.deck {
//whatever you had here
background-image: url( "/filler/filler.png" );
}
You might need to make container divs around your images, and put the css on that instead. Please note that if no explicit width or height is set, this will not work, as the background-image does not assign any width or height to the element it is attached to.
By testing beforehand:
<?php
$dirname = "images/";
$images = glob($dirname."dog*.png");
foreach ($images as $i=>$image) {
if( file_exists( $image ) ) {
$title = pathinfo($image);
echo '<img class="deck" src="'.$image.'" alt="'. $title['filename'].'" title="'.$title['filename'].'">';
} else {
echo '<img class="deck filler" src="/filler/filler.png" alt="This image does not exist." title="This image does not exist.">';
}
if(($i+1)%5 == 0) echo '<br />';
}
?>
This method might not work if you are running php in safe mode.

Using URL as HTML <img src />

I'm trying to use a URL which just contains an image to use the image on my site. I am using PHP, this is my code:
Echo '<img src= "$link["Image_Link"]" />';`
$link["Image_Link"] points to this link.
I also have many similar links which contain different images, and are called like this one. When I call this on my site I just get a broken image symbol where the real image should be.
Single and double quotes are not interchangable in PHP.
This should work:
Echo "<img src='$link[Image_Link]' />";
or
Echo '<img src="' . $link["Image_Link"] . '" />';
You need to concatenate your variable with your string:
Echo '<img src= "'.$link["Image_Link"].'" />';
Without it your HTML code in final result will look like:
<img src="$link[" Image_Link"]" />
And this is invalid HTML.

PHP images from function

I am a php newbie so please go easy.
I created queries for imgFld and imageFldName.
I am trying to find why my images from my db are not being displayed.
I have the below code:
image_show(stripslashes($row['imgFld']),stripslashes($row['imageFldName']));
echo ' '.$records_num;
function image_show($name_image, $alt_tag) {
if (file_exists("mywebsite.co.uk/images/'$name_image'")) {
$img = getimagesize('mywebsite.co.uk/images/'.$name_image);
echo '<img src="mywebsite.co.uk/images/'.$name_image.'" alt = '.$alt_tag.' border=0 align="bottom"';
echo 'width = '. $img[0] .' height = ' .$img[1] . ' />';
} else {
echo 'Add an image here';
}
}
Im getting the image names from a column inside my db and each column has an 'image.jpg', connecting it with the img src script from HTML so that I can display the images from the db.
However no images are being displayed and I cant find the error. Doesnt seem like anything is wrong.
When I echo $name_image nothing is produced.
In that case it means that there is nothing populating the $name_image variable.
Assuming that website directory is a local one, the cause of this is most likely your arguments when you call the image_show function. They do not match the order you have specified.
The first argument should be the name and the second the alt text, as defined:
function image_show($name_image, $alt_tag)
However you are passing the id to $name_image and the name as $alt_tag.
That should be it.
The problem is file_exists is only for local files.
For example:
if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/path/to/files/image.jpg')) {
...
}

Categories