I have a form that uploads data to the DB and this includes the path to the directory where images are uploaded. Everything works, except for the fact that the image won't display.
Viewing the source in my browser tells me that the image is found but I keep getting the broken image icon.
Here's my code:
$dir = "../uploaded_images/";
$filePath = $row['images_path'];
$fileArray = explode("*", $filePath);
if (count($fileArray) > 0) {
$image = $fileArray[0];
echo "<img src='$image' width='300px'>";
}
In the form you can upload multiple files. In the DB, the files get a random prefix then file name, like 3456456745654_imageName.jpg.
If multiple files are uploaded, they are split with an asterisk (*), which is why I'm exploding.
Then, to print only one image, I'm checking for the number of images relevant to a specific record then displaying only the first one.
PS. This code works for displaying all the images relevant to a selected image:
$dir = "uploaded_images/";
$filePath = $row['images_path'];
$fileArray = explode("*", $filePath);
foreach ($fileArray as $file) {
if (file_exists($dir . $file)) {
echo "<img class='images' src='$dir/$file' width='300px;'>";
}
}
But that's for a different page that displays a selected vehicle's information, including all images.
I need to show only one image per vehicle on the landing page that lists all vehicles.
Had to use the directory:
$dir = "../uploaded_images/";
$filePath = $row['images_path'];
$fileArray = explode("*", $filePath);
if (count($fileArray) > 0) {
$image = $fileArray[0];
if (file_exists($dir . $image)) {
echo "<img class='images' src='$dir/$image' width='300px;'>";
}
}
Related
I'm using PHP to put all the images in a given folder into a slideshow.
This works fine in the following code:
<?php
//get all image files with a .jpg extension.
$images = glob("" . $directory . "*.jpg");
$imgs = array();
// create array
foreach($images as $image){ $imgs[] = "$image"; }
/****** Display Images ******/
foreach ($imgs as $img) {
//I would like to get the image title here, to put in the echo below
echo "<div><img src='$img' border='0' width=\"100%\" height=\"100%\"/ /></div>";
}
?>
This all goes quite easy, but since I would now also like to add the Titles of the pictures as captions, I need to extract/get this information from the image.
I'm thinking I can get it with something along the lines of the function exif_read_data , but I'm not quite sure how to get the title and not all the meta data . . .
With a little help from the smart people using stackoverflow, this is the final and functional result, as seen in my answer below as well and again, made with bits and pieces from several answers.
<?php
/*
This script will print the image files in a given folder, along with their image description (title in windows file explorer).
*/
// Set the directory where the files reside
$directory = $GLOBALS['directory'];
//get all image files with a .jpg extension.
$imagenpath = glob("" . $directory . "*.jpg");
$imgs = array();
// create array
foreach($imagenpath as $image){ $imgs[] = "$image"; }
// Print each image file with the ImageDescription (the title/caption) in the ALT field
foreach ($imgs as $img) {
$exif_data = exif_read_data($img, 'IFD0');
$exif_description = "";
if (!empty($exif_data['ImageDescription']))
$exif_description = $exif_data['ImageDescription'];
echo "<div><img src='$img' alt='$exif_description' border='0' width=\"100%\" height=\"100%\"/ /></div>";
}
?>
thats everything that can be done, because most pictures are not supported with this function, though, you are doing it wrong because the error that you are getting is because the file is not found, the error you get when the file is found and is not supported is this:
Warning: exif_read_data(file.png): File not supported in
/path/to/file/file.php on line x
and its because you single quoted the variable in
$exif_data = exif_read_data($img, 'IFD0');
the code could be less characters, this is my solution:
<?php
/*
This script will print the image files in a given folder, along with their image description (title in windows file explorer).
*/
// Set the directory where the files reside
$directory = $GLOBALS['directory'];
//get all image files with a .jpg extension.
$imgs = glob($directory . "*.jpg");
// Print each image file with the ImageDescription (the title/caption) in the ALT field
foreach ($imgs as $img) {
$exif_data = exif_read_data($img, 'IFD0');
if (!empty($exif_data['ImageDescription']))
$exif_description = $exif_data['ImageDescription'];
echo "<div><img src=\"$img\" alt=\"$exif_description\" border=\"0\" width=\"100%\" height=\"100%\"/ /></div>";
}
?>
Try this :-
foreach (glob("*.jpg") as $filename) {
$name = basename($filename, '.jpg');
echo "<div><img src='$filename' title='$name' alt='$name' border='0' width=\"100%\" height=\"100%\"/ /></div>";
}
To solve the problem, I basically used the Windows file browser, selected the image file and entered the text in the document comments section.
Then I printed the $exif_data as suggested above by #RamRaider and found out this text ends up in the ImageDescription of the file.
Once I nailed down the errors in my script, along with help from #Abdallah Samman, there wasn't much to it anymore!
Thank you everyone...
The following works beautifully:
<?php
/*
This script will print the image files in a given folder, along with their image description.
*/
// Set the directory where the files reside
$directory = $GLOBALS['directory'];
//get all image files with a .jpg extension.
$imagenpath = glob("" . $directory . "*.jpg");
$imgs = array();
// create array
foreach($imagenpath as $image){ $imgs[] = "$image"; }
// Print each image file with the ImageDescription (the caption) in the ALT field
foreach ($imgs as $img) {
$exif_data = exif_read_data($img, 'IFD0');
$exif_description = "";
if (!empty($exif_data['ImageDescription']))
$exif_description = $exif_data['ImageDescription'];
echo "<div><img src='$img' alt='$exif_description' border='0' width=\"100%\" height=\"100%\"/ /></div>";
}
?>
I know just image name I don't know image extension (jpg, gif, etc) (code auto select). Directory Name "Upload"
I have many images in upload folder like (Image1, Image2, Image3), but I want to select only one image (image1 . (jpg)(gif)(png)). Please give me PHP code for select image from directory with any extension.
$img_detail = '11_detail'; // i wanna select this image
$dir = 'upload/advertisement/';
$files = scandir($dir);
foreach($files as $file){
echo $file.'<br>';
}
// Code showing result like thi
// 11.swf
// 13.gif
// 14.gif
// 15.gif
// 16.jpg
// 16_detail.gif // i wanna select this image
// index.php
All image extension not same
Enjoy!
$ad_id = '11';
$addata = ad_data($conn, $ad_id, 'ad_img'); // Function
$addetail = $ad_id.'_detail'; // this is my file name
$dir = 'upload/advertisement/';
$files = scandir($dir);
foreach($files as $file){
$file = explode('.', $file);
$currentname = current($file); // i have select current name
$ext = end($file); // for final result
// echo $firstname. '-'.$ext.'<br>'; // test
if($currentname == $addetail){ // matching image name with required name
$real_file = $currentname.'.'.$ext; // after matching use real extension
}
}
if(empty($real_file)){ // if file not found
echo $ad_id.'.'.$addata['ad_img'];
}else{ // enjoy
echo $real_file;
}
I have found some solution that work mostly but none that do the trick 100% what I am looking for is code that will look in all my albums in one directory and pull the images and also pull my thumbs that are in another folder and echo out the a string like this.
echo '<img src="',$img,'" />';
The problem is that my href image is in my images folder and my src (thumbnails) are in another directory. I am using shadowbox and want the script to echo out a stirng that will allow both the directories to be scanned at the same time.
Below is what I am using that does scan the directorys but will not return the coresponding enter code herethumbnails that reside in another directory
//path to directory to scan. i have included a wildcard for a subdirectory
$directory = "gallery_uploads/*/";
//get all image files with a .jpg extension.
$images = glob("" . $directory . "*.jpg");
$imgs = '';
// create array
foreach($images as $image){ $imgs[] = "$image"; }
//display images
foreach ($imgs as $img) {
"<a href='$img' rel='shadowbox'><img src='$img' /> ";
echo'<img src="',$img,'" />';
This works for me. You would have to make sure your images have the same name in each directory though.
It is very simple to have your uploader save the original, then save a resized image of the same name in the thumbnail directory.
$gallery_up_dir = "gallery_uploads";
$gallery_thumb_dir = "gallery_thumbs";
$directory = "$gallery_up_dir/*/";
// or get all image files with a .jpg, .JPG, .png, .PNG extension.
$extensions_array = array('jpg', 'JPG', 'png', 'PNG');
$imageArray = array();
foreach($extensionsArray as $ext){
$images = glob("" . $directory . "*.$ext");
// fill up the array
foreach($images as $image){
$imageArray[] = "$image";
}
}
//display images
foreach ($imageArray as $img) {
echo '<a href="',$img,'" rel="shadowbox"><img src="',
str_replace($gallery_up_dir,$gallery_thumb_dir,$img) ,'" /></a>';
}
How can I display images from directory and get a corresponding description with each image, give the description exists.
in Directory //
01.png
01.txt
02.png
03.png
03.txt
etc.
to display as //
<img src="01.png"><br>This is the description from the text file named 01.txt
<img src="02.png"><br>
<img src="03.png"><br>This is the description from the text file named 03.txt
I've been searching and searching, but can't find anything, so if someone could point me in the right direction it would be greatly appreciated. Also this is a very useful thing to be able to do for people wanting to create very simple galleries or lists of images and names.
Thanks in advance!
This is what you're looking for, as the description must be dynamically captured from a corresponding .txt file:
$dir = './';
$files = glob( $dir . '*.png');
foreach( $files as $file) {
$filename = pathinfo( $file, PATHINFO_FILENAME) . '.txt';
$description = file_exists( $filename) ? file_get_contents( $filename) : '';
echo '<img src="' . $file . '"><br>' . $description;
}
What it does is grabs an array of *.png files using glob() from a given directory ($dir). Then, for each image, it gets the filename of the image (so 01.png would be 01), and appends .txt to get the name of the description file. Then, it loads the description file into the $description variable using file_get_contents() if the description file exists. It then outputs the desired HTML.
I assume you have the .php file in the same directory as the pictures and text files are.
You could use function glob() to read in all image-files as array from the directory, cut off the file extension (so '01.png' becomes '01') and append the file extension with string concatentation.
A working code example may look like this:
<?php
$path_to_directory = './';
$pics = glob($path_to_directory . '*.png');
foreach($pics as $pic)
{
$pic = basename($pic, '.png'); // remove file extension
echo '<img src=\"{$pic}.png\"><br>';
if(file_exists($pic . '.txt'))
{
echo file_get_contents("{$pic}.txt");
}
}
?>
So definitely have a look on these functions:
http://www.php.net/glob
http://www.php.net/basename
http://www.php.net/file_get_contents
Happy coding.
Your question is a bit confusing.
make an array with all information.
$pics = array('img' => '01.png', 'text' => 'This is the description');
foreach($pics as $pic) {
echo '<img src="'.$pic['name'].'" alt="">' . $pic['text'];
}
So you have to put your information in an array or a database otherwise you cannot map the desciption to your image.
When you want to read dynamicly the folder its a bit difficult.
You can look at readdir or glob then you can read all images get the name and load the textfile with file_get_contents but i think its not a really performant way.
Code Modified from here : http://php.net/manual/en/function.readdir.php
//path to directory to scan
$directory = "../images/team/harry/";
//get all image files with a .jpg extension.
$images = glob($directory . "*.jpg");
//print each file name
foreach($images as $image)
{
print "<img src=\"$image\"><br>This is the description from the text file named $image";
}
ok, so this won't print the contents of the text file, but I'm sure you can further modify the code above to figure that out
YEP
Updated version of ZnArKs code, as he missed that you wanted the content of the files
//path to directory to scan
$directory = "../images/team/harry/";
//get all image files with a .jpg extension.
$images = glob($directory . "*.png");
//print each file name
foreach($images as $image)
{
$textfile = substr($image, 0, -3) . "txt";
echo "<img src='{$image}'><br/>";
if(file_exists($textfile))
{
echo file_get_contents($textfile) . "<br/>";
}
}
I am using this script to display random images from a folder. I have hundreds of images with significant filenames, So I’m looking for a way to display each image file name as an image caption. Is this possible?
this is the script Im using:
<?php
/*
By Matt Mullenweg > http://photomatt.net
Inspired by Dan Benjamin > http://hiveware.com/imagerotator.php
Latest version always at:
http://photomatt.net/scripts/randomimage
*/// Make this the relative path to the images, like "../img" or "random/images/".
// If the images are in the same directory, leave it blank.
$folder = '';
// Space seperated list of extensions, you probably won't have to change this.
$exts = 'jpg jpeg png gif';
$files = array(); $i = -1; // Initialize some variables
if ('' == $folder) $folder = './';
$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false !== ($file = readdir($handle))) {
foreach($exts as $ext) { // for each extension check the extension
if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive
$files[] = $file; // it's good
++$i;
}
}
}
closedir($handle); // We're not using it anymore
mt_srand((double)microtime()*1000000); // seed for PHP < 4.2
$rand = mt_rand(0, $i); // $i was incremented as we went along
header('Location: '.$folder.$files[$rand]); // Voila!
?>
If all you want to do is continue to display a single random image then remove (or comment out so you can restore it if you later want to) the header line and at the end of the file add the following lines underneath it:
//header('Location: '.$folder.$files[$rand]); // Voila!
echo $files[$rand];
?>
<br />
<img src="<?php echo $folder.$files[$rand] ?>" />
You might want to add some css styling to set the display size of the image, and to format the caption above it.