I'm trying to generate images on my website by pulling a thumbnail from one folder and the actual image from another. I have no idea what I am doing. This is what I have so far:
<?php
$thumbdirname = "images/thumbs/";
$imgdirname = "images/";
$mainimages = glob($imgdirname."*.{jpg,png,gif}", GLOB_BRACE);
$imagethumbs = glob($thumbdirname."*.{jpg,png,gif}", GLOB_BRACE);
foreach($imagethumbs as $image) {
echo '<a class="imageLink" href="$mainimages" data-lightbox="logo" data-title="Vivid Logo"><img src="'.$image.'"</a> ';
}
?>
I know that it will not work with just "$mainimages" for href, but I haven't been able to figure out what to put there. As is, it will pull up the right thumbnail, but not link to the full associated image.
I tried to put another foreach statement in, but I get four results from my two pictures (and their thumbnails). Which makes sense sense it is showing one of each combination:
(img1,thumb1),(img1,thumb2),(img2,thumb1),(img2,thumb2)
How should I change this to get it playing nicely?
You don't seem to be outputting from $mainimages so I'm going to ignore this for now.
You PHP code seems to be OK from initial glance. Only thing that is a bit iffy is that you are not closing you image tag.
If that does not help try counting the image array/object.
If that returns 0 try the below code.
// Find all files in that folder
$files = glob('images/gallery1/*');
// Display images
foreach($files as $file) {
echo '<img src="' . $file . '" />';
}
Hope that helps
Related
I have been told to rewrite my question by Stackoverflow
I upload images mostly to a directory on my sever where my website is hosted. Via a program on my PC and a app on phone and that works a treat.
I have come across PHP scripts that shows all the images in the above folder and returns a basic gallery, and they all work to a point.
What I am hoping to achieve (it might not be possible) is list the images by the date they were added to the server. Not sorting from the EXIF modified date.
If it cant be done, so be it.
Best regards,
RT
PS At the moment I'm use PHP Gallery, its OK but does not quite achive the sorting requirement I would like.
Gallery here. https://www.sidingstudios.com/pix4web/index.php
Is it possible to display the contents of server folder (images) but sort by Last Modified ? using PHP
My very basic script:
<?php
$images = glob('img/*');
foreach ($images as $image) {
echo '<img src="'.$image.'"><br>';
}
?>
Link its used on. https://www.sidingstudios.com/pix4web2/
This will sort the images by last modified time
$files = glob('img/*');
$output = [];
foreach ($files as $f)
{
$output[filemtime($f)] = $f;
}
ksort($output);
$images = array_reverse($output);
foreach ($images as $image)
{
echo '<img src="' . $image . '"><br />';
}
So I have a few images in the server (public_html/img/profile_pictures/).
This is how I currently set the image:
echo "<img src='img/profile_pictures/main_photo.png'/>";
The main_photo can change each day, but if it changes to main_photo.jpg insted, it wont show (because the extension is hardcoded on that line(.png)). Is it possible to display the photo without knowing the extension for the image file?
If you want a PHP code, then try this. This code will look for main_photo.* inside your folder and automatically set the extension upon finding one.
Remember to set the path properly
<?php
$yourPhotoPath = "img/profile_pictures/";
foreach (glob($yourPhotoPath.'main_photo.*') as $filename) {
$pathInfo = pathinfo($filename);
$extension = $pathInfo['extension'];
$fileName = chop($pathInfo['basename'], $extension);
echo "<img src='".$yourPhotoPath.$fileName.$extension."'/>";
}
?>
if a Photo isn't loaded, it's width and size is null.
Although I would advise you to write a class that checks and loads images, I get a feeling you want a simple solution. so, given by the premise that the photo is either
<img src='img/profile_pictures/main_photo.png'/>
or
<img src='img/profile_pictures/main_photo.jpg'/>
and that neither this path nor this filename ever changes and in the folder is only one picture,
you could simply echo both.
The img of the one that is empty will not be shown.
A better way was to write a class that loads your photo and checks if the photo is really there, like
$path = 'img/profile_pictures/main_photo.png';
if(!file_exists('img/profile_pictures/main_photo.png'))
{
//use the jpg path
$path = 'img/profile_pictures/main_photo.jpg';
}
You can ofc just inline this if case, but it's bad practise to intermix buisinesslogic and format logic, so I advice you to write a class for it.
I'm basically a designer who hand codes HTML, but am a hack when it comes to PHP. I've been asked to add image icons within a product search results table on a PHP/MySQL site that has had many programmers over a decade, causing a sloppy mess of code.
I have the following code at the top of a search results page that calls out the name of the image:
$image2 = $row['item'] . ".jpg";
$imagefile2 = $_SERVER['DOCUMENT_ROOT'] . "/product_images/$imagefile2";
if(file_exists($imagefile2)){
Within the table itself I hacked this to get the correct image to show:
print "<td><center><img src=/product_images/$image2 width=80><br>
Of course, if there is no image, there is a broken image link. I dod not know the proper syntax to tell the server that IF there is no image, THEN show noimage.jpg (located in same folder). This is probably a couple lines of added code at best, but after a couple hours of searches and attempts I surrender.
Check to see if the file exists and if it does, set the image2 variable to point to it. If it doesn't exist, set the image2 variable to point to the "no image" image.
if(file_exists($_SERVER['DOCUMENT_ROOT'] . "/product_images/".$row['item'].".jpg")){
$image2=$row['item'].".jpg";
}else{
$image2="/product_images/noimage.png";
}
<?php
if(file_exists($_SERVER['DOCUMENT_ROOT'] . "/product_images/".$row['item'].".jpg")){
$image2=$row['item'].".jpg";
}else{
// Specify No Image File Path to Variable
$image2="/product_images/noimage.png";
}
?>
<td><center><img src="<?php echo $image2; ?>" width="80"><br>
Im having trouble finding some piece of code for showing me the small number of how many images is in a folder in my website. Im using foldergallery.
I need to show the number above my folders, so one would know how many images are in a folder.
Can someone please point me there or write some kind of PHP code for this.
Thank you
R
You can use this:
$directory = "/path/to/directory/";
if(is_dir($directory)) {
$images = glob($directory . "{*.jpg,*.JPG,*.jpeg,*.png,*.PNG,*.gif,*.GIF}");
$numberOfImages = count($images);
}
After that, you just print the $numberOfImages where you want.
I've been struggling with this for a while now.
I've got an image gallery running using jQuery cycle plugin and the files are pulled from a folder using PHP glob(). Problem is, when I navigate to another page the gallery breaks due to the url of the new page being tacked on at the beginning of the file path.
Example:
Front Page url: http://localhost/project/image-display-images/image.jpg
Other Page url: http://localhost/**NEWPAGE**/project/image-display-images/image.jpg
Here's my code:
$files = glob('image-display-images/*.*');
for ($i=1; $i<count($files); $i++)
{
$num = $files[$i];
echo '<img src="'.$num.'"'.' alt="Campus Images" width="362" height="246"/>';
}
This would generate a list of images for jQuery cycle to scroll through. It only works on the front page though.
Any ideas?
SOLVED!
Here is my new code:
$files = glob(ABSPATH.'/image-display-images/*.*');
foreach ($files as $f) {
echo '<image src="'.home_url(str_replace(ABSPATH,'',$f)).'"alt="Campus Images" width="362" height="246"/>';
}
This works on all pages.
Thank you!
Use an absolute path:
$files = glob(ABSPATH.'image-display-images/*.*');
The WordPress Core sets the ABSPATH constant so it should be fairly reliable.
glob deals with filesystem paths, but you are trying to load URLs. To display the files the way you are trying to, you will need to convert the results to URLs. Here is a bare-bones example.
$files = glob(ABSPATH."*.*");
foreach ($files as $f) {
echo home_url(str_replace(ABSPATH,'',$f));
}
You may better off writing you own function to grab your file names, rather than depending on glob which does come with a warning about not being available on some systems. See: http://codex.wordpress.org/Filesystem_API
Define the full path of your gallery instead of 'image-display-images/*.*'
For example glob('/var/etc/www/image-display-images/*.*')