6

How to fetch the images from a folder and display it in the page, whether i can resize it in php itself or i have to resize it and upload it separtely to display it as thumbnails?

Imran
  • 87,203
  • 23
  • 98
  • 131
Rajasekar
  • 18,392
  • 34
  • 106
  • 137

5 Answers5

11

Here's the basic structure for traversing a directory and doing something with the image files (given 'images' is a directory in the same directory of your script)

$image_types = array(
    'gif' => 'image/gif',
    'png' => 'image/png',
    'jpg' => 'image/jpeg',
);

foreach (scandir('images') as $entry) {
    if (!is_dir($entry)) {
        if (in_array(mime_content_type('images/'. $entry), $image_types)) {
            // do something with image
        }
    }
}

From here, you can send the images directly to browser, generate tags for HTML page or create thumbnails with GD functions and store them for displaying.

Imran
  • 87,203
  • 23
  • 98
  • 131
  • Take note that this solution uses deprecated `mime_content_type` function which needs mime_magic php modules. Also, correct me if I'm wrong, but the `for` loop doesn't seem to be valid Php and I think should be `foreach( scandir('images') as $entry)`. – Emile Bergeron Mar 12 '15 at 20:42
  • 1
    @emileb Corrected the loop syntax. – Imran Mar 13 '15 at 19:43
7

i think this may help you!

<?
$string =array();
$filePath='directorypath/';  
$dir = opendir($filePath);
while ($file = readdir($dir)) { 
   if (eregi("\.png",$file) || eregi("\.jpg",$file) || eregi("\.gif",$file) ) { 
   $string[] = $file;
   }
}
while (sizeof($string) != 0){
  $img = array_pop($string);
  echo "<img src='$filePath$img'  width='100px'/>";
}
?>
Shiva Srikanth Thummidi
  • 2,898
  • 4
  • 27
  • 36
2

eregi is deprecated now so you can use preg_match instead

<?php
$string =array();
$filePath='directorypath/';  
$dir = opendir($filePath);
while ($file = readdir($dir)) { 
   if (preg_match("/.png/",$file) || preg_match("/.jpg/",$file) || preg_match("/.gif/",$file) ) { 
   $string[] = $file;
   }
}
while (sizeof($string) != 0){
  $img = array_pop($string);
  echo "<img src='$filePath$img' >";
}
?>
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
user2477139
  • 608
  • 1
  • 6
  • 21
1

Have a look at:

To resize images directly from PHP:

deceze
  • 510,633
  • 85
  • 743
  • 889
redShadow
  • 6,687
  • 2
  • 31
  • 34
1

Here's a one-liner based on another answer to a similar question:

// this will get you full path to images file.
$data = glob("path/to/images/*.{jpg,gif,png,bmp}", GLOB_BRACE);

// this will get you only the filenames
$data= array_map('basename', $data);

Originally, I wanted to use @Imran solution, but mime_content_type wasn't available and the server (on which I have zero control) uses old versions of Apache and Php.

So I changed it a little to work with file extension instead and I'm giving it here.

$imgDir = "images_dir";

// make sure it's a directory
if (file_exists($imgDir)) {

    // select the extensions you want to take into account
    $image_ext = array(
            'gif',
            'png',
            'jpg',
            'jpeg'
    );

    foreach (scandir($imgDir) as $entry) {
        if (! is_dir($entry)) { // no need to weed out '.' and '..'
            if (in_array(
                    strtolower(pathinfo($entry, PATHINFO_EXTENSION)), 
                    $image_ext)) {

                // do something with the image file.
            }
        }
    }
}

The code is tested and is working.

Community
  • 1
  • 1
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129