I'm trying to use PHP to bring in every image from a specific directory and turn them into HTML objects by wrapping some markup around them.
Right now I am just trying to get the images on the page. In the future I would also like to bring in a text file that has the same file name as the image(slide#4.jpg and slide#4.txt).
This is what I have so far.
<?php
$dir = "includes/pages/products/*.jpg";
$images = glob( $dir );
foreach( $images as $image ):
$file = basename($image);
$file = basename($image, ".jpg");
echo "<div class='"prod-cont"'>" . "<h4>" . $file . "</h4>" . "<img src'" . $image "' />" . "</div>";
endforeach;
?>
I'm not having much luck getting this going, any help is appreciated.
Remove the second $file = line, and make sure you're generating correct HTML.
echo "<div class='prod-cont'><h4>".$file."</h4><img src='".$image."' /></div>";
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.
I have a text file with 10 paths to files saved on my server, that looks like this:
C:\pictures\123.jpg
C:\pictures\124.jpg
C:\pictures\125.jpg
I'd like to show the pictures from the text file on a website. I can't directly put the links into a php script because the file is dynamically generated and has different paths everytime.
Any ideas on how to do this?
This should work for you:
(Here i use file() to get all lines of the file in an array. After this i simple loop through them and print it as an image path)
<?php
$lines = file("test.txt");
foreach($lines as $line)
echo "<img src='" . trim($line) . "'>";
?>
You can try and draw a neat table of images from your file .
NOTE: I am assuming all your files in the folder are images.
$root = 'C:\pictures';
$files = scandir( $directory );
echo '<table>';
foreach( $files as $file )
{
if(is_file($file))
{
echo '<tr><td><img src="' . $root . $file . '" /></td><td>' . $file . '</td></tr>';
}
}
echo '</table>
Let me know if I helped :)
I don't have opportunity to test this. But I hope this help.
$handle = fopen("images.txt", "r");
if ($handle) {
while (($pathToImg = fgets($handle)) !== false) {
echo "<img src='".$pathToImg."'/>";
}
fclose($handle);
} else {
// error opening the file.
}
I'm trying to get all images from a directory and print them. I know my URL is correct because I am echoing the the $dirname along with a .jpg image I have saved in the directory. What am I doing wrong?
<?php
$dirname = "uploads/{$site_country}_{$site_state}_{$site_name}/{$record_id}/";
$images = glob($dirname . "*.jpg");
foreach($images as $image) {
echo '<img src="'.$image.'"/>';
}
//check to see if $dirname is correct
echo '<img src="' . $dirname . "IMG_0002.JPG" . '"/>';
?>
//edit
I have solved my problem, by removing the jpg extention and just pulling "*" for everything in the folder. Although it works for me now, its not best practice. It must be something to do with different .jpg file formats?
You used lowercase file extension here.
$images = glob($dirname . "*.jpg");
And uppercase JPG in your test output.
echo '<img src="' . $dirname . "IMG_0002.JPG" . '"/>';
If you're using a linux system then file extensions are case sensitive.
You need to prepend the dirname to glob's result to get the full paths.
So something like:
$dirname = "uploads/{$site_country}_{$site_state}_{$site_name}/{$record_id}/";
$images = glob($dirname . "*.jpg");
foreach($images as $image) {
echo '<img src="'.$dirname.$image.'"/>';
}
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>"
?>