在php中按exif数据对图像进行排序

在php中按exif数据对图像进行排序,php,html,apache,exif,Php,Html,Apache,Exif,我是php新手,我正在尝试找出如何按exif创建日期对图像进行排序。下面的代码是我遵循的本教程中的代码: 我只是做了一些修改来检索exif数据 我正在寻找一个没有数据库的方法,目标是有一个分页的画廊(现在它是分页的)排序与最新的第一 function getPictures() { global $page, $per_page, $has_previous, $has_next, $DirFoto, $DirThumb; if ( $handle = opendir($DirF

我是php新手,我正在尝试找出如何按exif创建日期对图像进行排序。下面的代码是我遵循的本教程中的代码:

我只是做了一些修改来检索exif数据 我正在寻找一个没有数据库的方法,目标是有一个分页的画廊(现在它是分页的)排序与最新的第一

function getPictures() {
    global $page, $per_page, $has_previous, $has_next, $DirFoto, $DirThumb;
    if ( $handle = opendir($DirFoto) ) {

        echo '<ul id="pictures">';

        $count = 0;
        $skip = $page * $per_page;

        if ( $skip != 0 )
            $has_previous = true;

        while ( $count < $skip && ($file = readdir($handle)) !== false ) {
            if ( !is_dir($file) && ($type = getPictureType($file)) != '' )
                $count++;
        }
        while ( $count < $per_page && ($file = readdir($handle)) !== false ) {
            if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {

                $exif = exif_read_data("$DirFoto/$file", 0, true);                  

                if ( ! is_dir($DirThumb) ) {
                    mkdir($DirThumb);
                }
                if ( ! file_exists($DirThumb.'/'.$file) ) {
                    makeThumb( $file, $type );
                }
                echo '<li><a href="'.$DirFoto.'/'.$file.'" title="Photo taken on '.date("F d Y, H:i:s", strtotime($exif['IFD0']['DateTime'])).'">';
                    echo '<img src="'.$DirThumb.'/'.$file.'" alt="'.date("F d Y, H:i:s", strtotime($exif['IFD0']['DateTime'])).'"/>';   
                echo '</a></li>';
                $count++;
            }
        }
        echo '</ul>';



        while ( ($file = readdir($handle)) !== false ) {
            if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
                $has_next = true;
                break;
            }
        }


    }
}
函数getPictures(){ 全局$page,$per_page,$has_previous,$has_next,$DirFoto,$DirThumb; 如果($handle=opendir($DirFoto)){ echo'
    ; $count=0; $skip=$page*$每页; 如果($skip!=0) $has_previous=true; 而($count<$skip&($file=readdir($handle))!==false){ 如果(!is_dir($file)&($type=getPictureType($file))!=“”) $count++; } 而($count<$per_page&&($file=readdir($handle))!==false){ 如果(!is_dir($file)&($type=getPictureType($file))!=“”){ $exif=exif_read_数据($DirFoto/$file),0,true); 如果(!is_dir($DirThumb)){ mkdir($DirThumb); } 如果(!file_存在($DirThumb./'.$file)){ makeThumb($file,$type); } 回音“
  • ”; $count++; } } 回声“
”; while(($file=readdir($handle))!==false){ 如果(!is_dir($file)&($type=getPictureType($file))!=“”){ $has_next=true; 打破 } } } }
由于代码读取目录并在运行时输出HTML,因此您必须对其进行一些更改,以执行所需操作。我建议首先将文件名读入一个数组,并为每次读取的文件调用
exif\u read\u data

如果按文件名为数组设置关键帧,且数组的值为exif创建日期,则可以调用按创建日期对数组进行排序。如果文件没有有效的exif创建日期,也许您可以使用服务器上文件的修改时间

一旦数组按正确的顺序排序,就可以更改以下while循环

while ( $count < $per_page && ($file = readdir($handle)) !== false ) {
while($count<$per_page&&($file=readdir($handle))!==false){
将来

while($count<$per\u page&&($file=array\u shift($sorted\u files))!==false){

其中,
$sorted\u files
是排序文件的数组。如果没有帮助,我可以尝试编写一个示例。

很抱歉,我现在无法尝试此解决方案,但它应该类似于:

<?php

function getPictures() {
    global $page, $per_page, $has_previous, $has_next, $DirFoto, $DirThumb;
    if ( $handle = opendir($DirFoto) ) {

        $images = array(); # empty data structure
        while(($file = readdir($handle)) !== false ) {
            if ( !is_dir($file) && ($type = getPictureType($file)) != '' )
                # only for images
                $exif = exif_read_data("$DirFoto/$file", 0, true);
                $date = $exif['IFD0']['DateTime']; # everything you like to be ordered
                $images[$file] = $date; # associate each file to its date
            }
        }
        asort($images); # sort the structure by date

        echo '<ul id="pictures">';

        $skip = $page * $per_page;

        if ( $skip != 0 )
            $has_previous = true;

        $count = 0;
        while ( $count < $skip && ($file = array_shift($images)) !== false ) {
            if ( !is_dir($file) && ($type = getPictureType($file)) != '' )
                $count++;
        }
        # $count = 0;# (???)
        while ( $count < $per_page && ($file = array_shift($images)) !== false ) {

            $exif = exif_read_data("$DirFoto/$file", 0, true); # it could be avoided                

            if ( ! is_dir($DirThumb) ) {
                mkdir($DirThumb);
            }
            if ( ! file_exists($DirThumb.'/'.$file) ) {
                makeThumb( $file, $type );
            }
            echo '<li><a href="'.$DirFoto.'/'.$file.'" title="Photo taken on '.date("F d Y, H:i:s", strtotime($exif['IFD0']['DateTime'])).'">';
            echo '<img src="'.$DirThumb.'/'.$file.'" alt="'.date("F d Y, H:i:s", strtotime($exif['IFD0']['DateTime'])).'"/>';   
            echo '</a></li>';

            $count++;
        }
        echo '</ul>';



        while ( ($file = readdir($handle)) !== false ) {
            if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
                $has_next = true;
                break;
            }
        }


    }
}
?>

如果我将鼠标悬停在工具提示上,显示正确的日期,但没有加载缩略图或图像,代码现在就可以工作了。
在我的第一个问题中,我省略了最后两个函数,这是我当前的更新代码:

<?php
    $page = $_GET['page'];
    $has_previous = false;
    $has_next = false;  
function getPictures() {
    global $page, $per_page, $has_previous, $has_next, $DirFoto, $DirThumb;
    if ( $handle = opendir($DirFoto) ) {
        $images = array();
        while(($file = readdir($handle)) !== false ) {
            if ( !is_dir($file) && ($type = getPictureType($file)) != '' )

                $exif = exif_read_data("$DirFoto/$file", 0, true);
                $date = $exif['IFD0']['DateTime'];
                $images[$file] = $date;
            }
        }
        asort($images);

        echo '<ul id="pictures">';
        $skip = $page * $per_page;
        if ( $skip != 0 )
            $has_previous = true;

        $count = 0;
        while ( $count < $skip && ($file = array_shift($images)) !== false ) {
            if ( !is_dir($file) && ($type = getPictureType($file)) != '' )
                $count++;
        }
        while ( $count < $per_page && ($file = array_shift($images)) !== false ) {
            $exif = exif_read_data("$DirFoto/$file", 0, true);              

            if ( ! is_dir($DirThumb) ) {
                mkdir($DirThumb);
            }
            if ( ! file_exists($DirThumb.'/'.$file) ) {
                makeThumb( $file, $type );
            }
            echo '<li><a href="'.$DirFoto.'/'.$file.'" title="Photo taken on '.date("F d Y, H:i:s", strtotime($exif['IFD0']['DateTime'])).'">';
            echo '<img src="'.$DirThumb.'/'.$file.'" alt="'.date("F d Y, H:i:s", strtotime($exif['IFD0']['DateTime'])).'"/>';   
            echo '</a></li>';

            $count++;
        }
        echo '</ul>';
        while ( ($file = readdir($handle)) !== false ) {
            if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
                $has_next = true;
                break;
            }
        }


    }

    function getPictureType($file) {
        $split = explode('.', $file); 
        $ext = $split[count($split) - 1];
        if ( preg_match('/jpg|jpeg/i', $ext) ) {
            return 'jpg';
        } else if ( preg_match('/png/i', $ext) ) {
            return 'png';
        } else if ( preg_match('/gif/i', $ext) ) {
            return 'gif';
        } else {
            return '';
        }
    }
    function makeThumb( $file, $type ) {
        global $max_width, $max_height;
        if ( $type == 'jpg' ) {
            $src = imagecreatefromjpeg($DirFoto.'/'.$file);
        } else if ( $type == 'png' ) {
            $src = imagecreatefrompng($DirFoto.'/'.$file);
        } else if ( $type == 'gif' ) {
            $src = imagecreatefromgif($DirFoto.'/'.$file);
        }
        if ( ($oldW = imagesx($src)) < ($oldH = imagesy($src)) ) {
            $newW = $oldW * ($max_width / $oldH);
            $newH = $max_height;
        } else {
            $newW = $max_width;
            $newH = $oldH * ($max_height / $oldW);
        }
        $new = imagecreatetruecolor($newW, $newH);
        imagecopyresampled($new, $src, 0, 0, 0, 0, $newW, $newH, $oldW, $oldH);
        if ( $type == 'jpg' ) {
            imagejpeg($new, $DirThumb.'/'.$file);
        } else if ( $type == 'png' ) {
            imagepng($new, $DirThumb.'/'.$file);
        } else if ( $type == 'gif' ) {
            imagegif($new, $DirThumb.'/'.$file);
        }
        imagedestroy($new);
        imagedestroy($src);
    }
?>

我将整个代码合理化了一点,这是我的结果:
    $ScriptsHead = '
        <link type="text/css" media="screen" rel="stylesheet" href="./stile.css"/>
        <link type="text/css" rel="stylesheet" href="./js/photoswipe.css"/>
        <script type="text/javascript" src="./js/jquery-1.8.3.js"></script>
        <script type="text/javascript" src="./js/klass.min.js"></script>
        <script type="text/javascript" src="./js/code.photoswipe-3.0.5.min.js"></script>
        <script type="text/javascript" src="./js/miophotoswipe.js"></script>            
    ';

function getPictures() {
    global $page, $per_page, $has_previous, $has_next, $DirFoto, $DirThumb;
    if ( $handle = opendir($DirFoto) ) {

        $skip = $page * $per_page;

        $images = array(); # empty data structure
        while(($file = readdir($handle)) !== false ) {
            if($file == '..' || $file == '.' || is_dir($file) || getPictureType($file) == '')
                continue;
            # only for images
            $exif = exif_read_data("$DirFoto/$file", 0, true);
            $date = $exif['IFD0']['DateTime']; # everything you like to be ordered
            $images[$file] = $date; # associate each file to its date
        }
        asort($images); # sort the structure by date

        echo '<ul id="pictures">';

        if ( $skip != 0 )
            $has_previous = true;

        $count = -1;
        foreach ($images as $file => $fileDate) {
            $count ++;
            if($count < $skip)
                continue;
            if($count >= $skip + $per_page) {
                $has_next = true;
                break;
            }

            if ( ! is_dir($DirThumb) ) {
                mkdir($DirThumb);
            }
            if ( ! file_exists($DirThumb.'/'.$file) ) {
                makeThumb( $file, $type );
            }

            echo '<li><a href="'.$DirFoto.'/'.$file.'" title="Photo taken on '.date("F d Y, H:i:s", strtotime($fileDate)).'">';
            echo '<img src="'.$DirThumb.'/'.$file.'" alt="'.date("F d Y, H:i:s", strtotime($fileDate)).'"/>';   
            echo '</a></li>';

        }
        echo '</ul>';
    }


}


    function getPictureType($file) {
        $split = explode('.', $file); 
        $ext = $split[count($split) - 1];
        if ( preg_match('/jpg|jpeg/i', $ext) ) {
            return 'jpg';
        } else if ( preg_match('/png/i', $ext) ) {
            return 'png';
        } else if ( preg_match('/gif/i', $ext) ) {
            return 'gif';
        } else {
            return '';
        }
    }

    function makeThumb( $file, $type ) {
        global $max_width, $max_height, $DirFoto, $DirThumb;
        if ( $type == 'jpg' ) {
            $src = imagecreatefromjpeg($DirFoto.'/'.$file);
        } else if ( $type == 'png' ) {
            $src = imagecreatefrompng($DirFoto.'/'.$file);
        } else if ( $type == 'gif' ) {
            $src = imagecreatefromgif($DirFoto.'/'.$file);
        }
        if ( ($oldW = imagesx($src)) < ($oldH = imagesy($src)) ) {
            $newW = $oldW * ($max_width / $oldH);
            $newH = $max_height;
        } else {
            $newW = $max_width;
            $newH = $oldH * ($max_height / $oldW);
        }
        $new = imagecreatetruecolor($newW, $newH);
        imagecopyresampled($new, $src, 0, 0, 0, 0, $newW, $newH, $oldW, $oldH);
        if ( $type == 'jpg' ) {
            imagejpeg($new, $DirThumb.'/'.$file);
        } else if ( $type == 'png' ) {
            imagepng($new, $DirThumb.'/'.$file);
        } else if ( $type == 'gif' ) {
            imagegif($new, $DirThumb.'/'.$file);
        }
        imagedestroy($new);
        imagedestroy($src);
    }
/*  echo phpinfo(); */

?>
$ScriptsHead='1!'
';
函数getPictures(){
全局$page,$per_page,$has_previous,$has_next,$DirFoto,$DirThumb;
如果($handle=opendir($DirFoto)){
$skip=$page*$每页;
$images=array()#空数据结构
while(($file=readdir($handle))!==false){
如果($file='..'| |$file='.| |是_dir($file)| | getPictureType($file)='')
继续;
#仅适用于图像
$exif=exif_read_数据($DirFoto/$file),0,true);
$date=$exif['IFD0']['DateTime'];#您想订购的一切
$images[$file]=$date;#将每个文件与其日期关联
}
asort($images)#按日期对结构排序
echo'
    ; 如果($skip!=0) $has_previous=true; $count=-1; foreach($file=>$fileDate的图像){ $count++; 如果($count<$skip) 继续; 如果($count>=$skip+$每页){ $has_next=true; 打破 } 如果(!is_dir($DirThumb)){ mkdir($DirThumb); } 如果(!file_存在($DirThumb./'.$file)){ makeThumb($file,$type); } 回音“
  • ”; } 回声“
”; } } 函数getPictureType($file){ $split=分解('.',$file); $ext=$split[计数($split)-1]; if(preg_match('/jpg | jpeg/i',$ext)){ 返回“jpg”; }else if(preg_匹配('/png/i',$ext)){ 返回‘png’; }else if(preg_匹配('/gif/i',$ext)){ 返回'gif'; }否则{ 返回“”; } } 函数makeThumb($file,$type){ 全局$max_width、$max_height、$DirFoto、$DirThumb; 如果($type=='jpg'){ $src=imagecreatefromjpeg($DirFoto./'.$file); }如果($type='png'),则为else{ $src=imagecreatefrompng($DirFoto./'.$file); }else if($type=='gif'){ $src=imagecreatefromgif($DirFoto./'.$file); } 如果($oldW=imagesx($src))<($oldH=imagesy($src))){ $newW=$oldW*($max_width/$oldH); $newH=$max_高度; }否则{ $newW=$max_宽度; $newH=$oldH*($max_height/$oldW); } $new=imagecreatetruecolor($newW,$newH); imagecopyresampled($new、$src、0、0、0、0、$newW、$newH、$oldW、$oldH); 如果($type=='jpg'){ imagejpeg($new,$DirThumb./'.$file); }如果($type='png'),则为else{ imagepng($new,$DirThumb./'.$file); }else if($type=='gif'){ imagegif($new,$DirThumb./'.$file); } (新的); (1)(src); } /*echo phpinfo()*/ ?>
php有一个图书馆,我
    $ScriptsHead = '
        <link type="text/css" media="screen" rel="stylesheet" href="./stile.css"/>
        <link type="text/css" rel="stylesheet" href="./js/photoswipe.css"/>
        <script type="text/javascript" src="./js/jquery-1.8.3.js"></script>
        <script type="text/javascript" src="./js/klass.min.js"></script>
        <script type="text/javascript" src="./js/code.photoswipe-3.0.5.min.js"></script>
        <script type="text/javascript" src="./js/miophotoswipe.js"></script>            
    ';

function getPictures() {
    global $page, $per_page, $has_previous, $has_next, $DirFoto, $DirThumb;
    if ( $handle = opendir($DirFoto) ) {

        $skip = $page * $per_page;

        $images = array(); # empty data structure
        while(($file = readdir($handle)) !== false ) {
            if($file == '..' || $file == '.' || is_dir($file) || getPictureType($file) == '')
                continue;
            # only for images
            $exif = exif_read_data("$DirFoto/$file", 0, true);
            $date = $exif['IFD0']['DateTime']; # everything you like to be ordered
            $images[$file] = $date; # associate each file to its date
        }
        asort($images); # sort the structure by date

        echo '<ul id="pictures">';

        if ( $skip != 0 )
            $has_previous = true;

        $count = -1;
        foreach ($images as $file => $fileDate) {
            $count ++;
            if($count < $skip)
                continue;
            if($count >= $skip + $per_page) {
                $has_next = true;
                break;
            }

            if ( ! is_dir($DirThumb) ) {
                mkdir($DirThumb);
            }
            if ( ! file_exists($DirThumb.'/'.$file) ) {
                makeThumb( $file, $type );
            }

            echo '<li><a href="'.$DirFoto.'/'.$file.'" title="Photo taken on '.date("F d Y, H:i:s", strtotime($fileDate)).'">';
            echo '<img src="'.$DirThumb.'/'.$file.'" alt="'.date("F d Y, H:i:s", strtotime($fileDate)).'"/>';   
            echo '</a></li>';

        }
        echo '</ul>';
    }


}


    function getPictureType($file) {
        $split = explode('.', $file); 
        $ext = $split[count($split) - 1];
        if ( preg_match('/jpg|jpeg/i', $ext) ) {
            return 'jpg';
        } else if ( preg_match('/png/i', $ext) ) {
            return 'png';
        } else if ( preg_match('/gif/i', $ext) ) {
            return 'gif';
        } else {
            return '';
        }
    }

    function makeThumb( $file, $type ) {
        global $max_width, $max_height, $DirFoto, $DirThumb;
        if ( $type == 'jpg' ) {
            $src = imagecreatefromjpeg($DirFoto.'/'.$file);
        } else if ( $type == 'png' ) {
            $src = imagecreatefrompng($DirFoto.'/'.$file);
        } else if ( $type == 'gif' ) {
            $src = imagecreatefromgif($DirFoto.'/'.$file);
        }
        if ( ($oldW = imagesx($src)) < ($oldH = imagesy($src)) ) {
            $newW = $oldW * ($max_width / $oldH);
            $newH = $max_height;
        } else {
            $newW = $max_width;
            $newH = $oldH * ($max_height / $oldW);
        }
        $new = imagecreatetruecolor($newW, $newH);
        imagecopyresampled($new, $src, 0, 0, 0, 0, $newW, $newH, $oldW, $oldH);
        if ( $type == 'jpg' ) {
            imagejpeg($new, $DirThumb.'/'.$file);
        } else if ( $type == 'png' ) {
            imagepng($new, $DirThumb.'/'.$file);
        } else if ( $type == 'gif' ) {
            imagegif($new, $DirThumb.'/'.$file);
        }
        imagedestroy($new);
        imagedestroy($src);
    }
/*  echo phpinfo(); */

?>