Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the pictures from folder one by one and display in the page using PHP

Tags:

php

image

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?

like image 905
Rajasekar Avatar asked Aug 01 '09 03:08

Rajasekar


2 Answers

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.

like image 133
Imran Avatar answered Oct 10 '22 01:10

Imran


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'/>";
}
?>
like image 23
Shiva Srikanth Thummidi Avatar answered Oct 10 '22 01:10

Shiva Srikanth Thummidi