Fetch specific images from a directory with php - php

$dirname = "media/images/iconized/";
$images = glob($dirname."*.jpg");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}
With the above code I can fetch all images in a folder.But, I am not able to fetch particular images from a folder.
For example:
I have images in a folder as:
1. low_0.jpg
2. low_1.jpg
3. low_2.jpg
4. med_0.jpg
5 med_1.jpg
6. med_2.jpg
From these images how to grab only the images that starts with med? Thanks for your time

Change the glob() command from:
$images = glob($dirname."*.jpg");
To:
$images = glob($dirname."med_*.jpg");
The * is a wildcard; it'll fill in the gap (in this case, numbers).
Full code:
$dirname = "media/images/iconized/";
$images = glob($dirname."med_*.jpg");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}

Related

PHP: Pull all images from a specified directory using relative image path

I am trying to create a gallery where images are automatically pulled from a images directory.
When I foreach() each image it returns however it pulls the entire root path as the image src
/home/dev/public_html/assets/images/
this causes the images not to show and shows dead linked images on screen. I need the relative path like this:
/assets/images/imagename.jpg
How would I set a base path/URL?
$dirname = "/home/dev/public_html/assets/images/";
$images = glob($dirname."*.jpg");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}
I tried setting $dirname to this
$dirname = "/assets/images/";
But I get no result and nothing pulls through at all.
The file i am creating this in, is located outside the public_html folder, i believe this could be casing the issue but the file cannot be relocated.
I would edit the loop in order to remove unnecessary string from path:
foreach($images as $image) {
$src = str_replace('/home/dev/public_html' ,'', $image) ;
echo '<img src="'.$src.'" /><br />';
}
You could may make use of scandir() http://php.net/manual/en/function.scandir.php
Somethink like:
Edit: Changed the code a bit, so scandir only take files, otherwise the first two placed in the array are folders for the parent folders.
$dir = "assets/images/";
$images = scandir($dir);
foreach($images as $image) {
if($image != '.' && $image != '..') {
echo ' <img src="assets/images/'.$image.'"><br>';
}
}
Hope this helped. If not, let me know, so I can change my answer. But I think it should be fine, as far as I understood what you're trying.

Get all images in a directory except retina

I have a php script which grabs all of the images in a set directory. The script works without fault. My issue is that I want it to only get none retina images. All of the retina images are named in the format 'image1#2x.jpg' whereas all of the regular images are named in the format 'image1.jpg'. With this in mind I want my script to discount any image that contains '#2x' in it's name.
My script is:
<?php
$directory = 'images/';
$dh = opendir($directory);
while (false !== ($filename = readdir($dh)))
{
$files[] = $filename;
};
$images = preg_grep ('/(\.gif|\.GIF|\.jpg|\.jpeg|\.JPG|\.JPEG|\.png|\.PNG)$/i', $files);
foreach ($images as $image)
{
echo '<img src="'.$directory.$image.'" alt="">';
}
?>
The problem is I don't have the vocabulary to know what to search for in Google so if anyone could point me in the right direction I would appreciate it.
Thanks
I would use stripos put this conditional in your foreach loop
if(stripos($image,'#2x') === false){
echo '<img src="'.$directory.$image.'" alt="">';
}
You can make your script a little shorter by using:
$directory = 'images/';
$allImages = glob($directory.'*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}', GLOB_BRACE);
$nonRetinaImages = preg_grep('/#2x/i', $allImages, PREG_GREP_INVERT);
foreach ($nonRetinaImages as $image)
{
echo '<img src="'.$image.'" alt="">';
}

What is wrong with my code on printing a random image in a website?

this is the code in order to display a random image on my website,
but for some reason the images are not popping up and there is an error on the line where $rand_image was declared. The error says undefined index. I have 7 images in the directory.
$imagesDir = '\socimages\Badminton'; // my image directory
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE); // glob
$rand_image = $images[array_rand($images)]; // applying the random function
...
...
<img src="<?php echo $rand_image[0];?>"< alt="" height="246" width="246"></p></div>
$rand_image is the image already you don't need $rand_image[0];
<img src="<?php echo htmlspecialchars($rand_image);?>"< alt="" height="246" width="246"></p></div>
Also, $rand_image will probably be the path on disk, not the web accessible path so you'll need to map it.
I'd do something like this.
$imagesDir = '/socimages/Badminton/'; // my image directory
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE); // glob
if ($images !== FALSE) {
$rand_image = $images[array_rand($images)]; // applying the random function
} else {
die("This thang ain't working, yo.");
}
...
...
<img src="<?php echo $rand_image;?>"< alt="" height="246" width="246"></p></div>

PHP different locations for images and thumbs

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>';
}

populate an HTML <ul> with images from a folder automatically

I wanted to ask how can I populate an HTML list with Images located in a folder automatically?
lets say I have 4 pages with 4 different lists.
lets say the lists are as follows: Cars, Motorcycles, Airplanes, Ships
each list entry has a src as a thumbnail and a href as a link to full size image.
my folder contain images with specific titles:
C=Cars
M=Motorcycles
A=Airplanes
S=Ships
then I have a serial number
and the ending is either
T=Thumbnail
F=Full Image
so a complete file name would be C20T.jpg or C20F.jpg.
I am looking for a script that will update the 4 lists according to their subject and populate them automatically with related file names.
from my search here I have found this piece of code but I believe it needs some tweaking and I am not sure how to do this :(
$dir = '/images';
$files = scandir($dir);
$images = array();
foreach($files as $file) {
if(fnmatch('*.jpg',$file)) {
$images[] = $file;
}
}
foreach($images as $image) {
echo '<img src="images/'.$image.'" />';
}
I hope someone could help me with this!
Thank you for your time reading this.
$type = 'c';
$imagesDir = 'images/';
$images = glob($imagesDir . $type . '*[TYPE].{jpg,jpeg,png,gif}', GLOB_BRACE);
This should get all your full images.
Making a ul is then pretty easy...
<ul>
<?php foreach($images as $image): ?>
<li><img src="<?php echo str_replace('[TYPE]', 'T', $image); ?>" alt="" /></li>
<?php endforeach; ?>
</ul>

Categories