<?php
//遍历文件夹的文件名称
function foreach_file_name($file_pacth = './img/')
{
    if(!is_dir($file_pacth))
    {
        return false;
    }
    $handle = opendir($file_pacth);
    $file_name_list = array();
    while (false !== ($file = readdir($handle)))
    {
        if (!is_dir($file_pacth.$file))
        {
            $file_name_list[] = "$file";
        }
    }
    return $file_name_list;
}
//判断是否是图片
function isImage($filename){
$types = '.gif|.jpeg|.png|.bmp';//定义检查的图片类型
if(file_exists($filename)){
    $info = getimagesize($filename);
    $ext = image_type_to_extension($info['2']);
    return stripos($types,$ext);
}else{
    return false;
}
}
$tu = foreach_file_name();
//获取文件夹里面的所有图片
function get_folder_img_list($path = './img/',$width ='',$order_by = 'DESC')
{
    $img_name_list = foreach_file_name($path);
    $img_path_list = array();
    foreach($img_name_list as $img_name)
    {
        $img_path = $path.$img_name ;
        if(isImage($img_path)!==false)
        {
            $temp_data = array();
            $temp_data['path'] = $img_path;
            $temp_data['time'] = filectime($img_path);
            $temp_data['name'] = $img_name;
            $image_size = getimagesize($img_path);
            $temp_data['width'] = $image_size[0];
            $temp_data['height'] = $image_size[1];
            $temp_data['type'] = $image_size[2];
            if(!empty($width))
            {
                $temp_data['height'] = floor( $temp_data['height'] * ($width / $temp_data['width']) ) ;
                $temp_data['width'] = $width ;
            }
            $img_path_list[] = $temp_data;
        }
    }
    return $img_path_list ;
}
//获取一个路径下的所有文件夹名称
function foreach_folder_name($path)
{
    $handle = opendir($path);
    $file_list = array();
    while (false !== ($file = readdir($handle)))
    {
        if( $file == '.' || $file == '..' )
        {
            continue;
        }
        if (is_dir($path.$file))
        {
            $file_list[] = $file;
        }
    }
    return $file_list;
}
//获取一个路径下面的所有文件里面的图片
function get_path_folder_img_list($path,$width = '')
{
    $file_list = foreach_folder_name($path);
    $img_list = array();
    foreach($file_list as $file)
    {
        if(empty($width))
        {
            $img_arr = get_folder_img_list($path.$file.'/');
        }else
        {
            $img_arr = get_folder_img_list($path.$file.'/',$width);
        }
        foreach( $img_arr as &$img )
        {
            $img['class'] = $file ;
            $img_list[] = $img;
        }
    }
    shuffle($img_list) ;
    return $img_list ;
}
?>