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="">';
}
Related
I try to list all images that have been uploaded to the website and embed them with a foreach loop, all I see is just a "not working" thumbnail, like a placeholder for the images, while the image url is already correct.
the code:
$scan = scandir('images');
foreach ($scan as $image){
echo "<img src=\"$image\"> ";
}
I am working with windows xampp environment, just for testing purposes, but i can't test it anywhere else yet, so i can't say if it's a windows thing or not :(
Thank you in advance!
If the images were in a folder called images then add that to the src attribute
$scan = scandir('images');
foreach ($scan as $image){
// remember to remove the `.` and `..` folders
if ( $image != '.' && $image != '..'){
echo '<img src="images/$image">';
}
}
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.
Hello there good people of stackoverflow.
I have this code that basically get any photo i place in a folder called "pictures" and displays them on my page. Which is all good, and it works, with lightbox as well.
My main question is, can there be an easy way to display the photos in some form of order? i.e newest photos first?
<?php $handle = opendir(dirname(realpath(__FILE__)).'/pictures/');
while($file = readdir($handle)){if($file !== '.' && $file !== '..'){
echo '<img src="pictures/'.$file.'" border="0" />';}}?>
I know the code is very vulgar and ancient, but its only for a sample page so it doesn't need to be massive.
You can order them using php's filemtime(): http://php.net/manual/en/function.filemtime.php
<?php
$path = dirname(realpath(__FILE__)).'/pictures/';
$handle = opendir($path);
$arrFiles = array();
while($file = readdir($handle))
{
if($file !== '.' && $file !== '..')
{
$arrFiles[filemtime($path.$file)] = '<img src="pictures/'.$file.'" border="0" />';
}
}
arsort($arrFiles);
foreach ($arrFiles as $file)
{
echo $file;
}
?>
I followed a few sites and have come to this:
<?php
$imagesDir = base_url() . 'images/eventGallery/';
$files = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
for ($i = 0; $i < count($files); $i++) {
$num = $files[$i];
echo '<img src="'.$num.'" alt="random image">'." ";
}
?>
It is not working as nothing is displaying! What am I doing wrong? There are two images in said directory, with ,jpg extension.
Thanks for any assistance!
What does your base_url() returns? Do you have slash('/') between base url and images?
case-sensitivity of glob is different on various systems, do your pictures have jpg or JPG or Jpg (etc) extensions?
when creating img tags you must transform paths from file-system to web path (relative from web root)
remove count from loop
This is working version of your code (altough I would rather use readdir :) ):
define('BASE_URL', dirname(__FILE__));
$imagesDir = BASE_URL . '/images/eventGallery/';
$files = glob($imagesDir . '*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}', GLOB_BRACE);
$len=count($files);
for ($i = 0; $i < $len; $i++)
{
$num = $files[$i];
// transform from file system path to web path - assuming images is in the web root
$num = substr($num, strlen(BASE_URL) );
echo '<img src="'.$num.'" alt="random image">'." ";
}
Just use PHP's built in readdir function
Reference: http://php.net/manual/en/function.readdir.php
<?php
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "$entry\n";
}
}
closedir($handle);
}
?>
i would probably do something like this:
foreach (glob('../_pics/about/'.'*.{jpg,jpeg,JPG,JPEG}', GLOB_BRACE) as $filename)
{
echo"$filename";
}
I have a script that goes through a directory that has 3 images
$imglist='';
$img_folder = "path to my image";
//use the directory class
$imgs = dir($img_folder);
//read all files from the directory, checks if are images and ads them to a list
while ($file = $imgs->read()) {
if (eregi("gif", $file) || eregi("jpg", $file) || eregi("png", $file))
$imglist .= "$file ";
}
closedir($imgs->handle);
//put all images into an array
$imglist = explode(" ", $imglist);
//display image
foreach($imglist as $image) {
echo '<img src="'.$img_folder.$image.'">';
}
but the problem that I am having is it display a 4th img with no image.. yet I only have 3 image in that folder.
There is no need to build a string of images and then explode that string into an array of images instead just add the images directly to an array as Radu mentioned.
Here is the corrected code:
$imglist = array();
$img_folder = "path to my image";
//use the directory class
$imgs = dir($img_folder);
//read all files from the directory, checks if are images and adds them to a list
while ($file = $imgs->read()) {
if (eregi("gif", $file) || eregi("jpg", $file) || eregi("png", $file)){
$imglist[] = $file;
}
}
closedir($imgs->handle);
//display image
foreach($imglist as $image) {
echo '<img src="'.$img_folder.$image.'">';
}
You'll have a space at the end of the $imglist string, which explode() will turn into an empty element. Trim the string:
$imglist = explode(" ", trim($imglist));
Or better yet, just add them to the $imglist array in the first place, instead of making a string and exploding it:
$imglist = array();
/* ... */
$imglist[] = $file;
ereg() is deprecated. You'd probably be better off with:
chdir($img_folder);
$imgs = glob('*.jpg *.gif *.png');
foreach ($imgs as $img) {
echo "<img src=\"{$img_folder}/{$img}\">";
}
glob() does wildcard matching pretty much the same way as most Unix shells do.
Use [glob()][1] function
<?php
define('IMAGEPATH', 'path to my image/'.$imglist.'/');
foreach(glob(IMAGEPATH.'*.jpg') as $filename){
echo '<img src="'.$filename.'" alt="'.$album.'" />';
?>
[1]: http://www.php.net/manual/en/function.glob.php