4

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

我只是修改了一点以检索 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;
            }
        }


    }
}
4

4 回答 4

2

我对整个代码进行了一些合理化,这是我的结果:

    $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(); */

?>
于 2013-05-14T07:01:45.403 回答
1

由于代码会读取目录并在运行时输出 HTML,因此您必须对其进行一些更改才能执行您想要的操作。我建议首先将文件名读入一个数组并调用exif_read_data每个文件读取。

如果您按文件名键入数组并且数组的值是 exif 创建日期,则可以调用asort()以按创建日期对数组进行排序。如果文件没有有效的 exif 创建日期,也许您可​​以只使用服务器上文件的修改时间。

数组按正确顺序排序后,您可以更改以下 while 循环

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

成为

while ( $count < $per_page && ($file = array_shift($sorted_files)) !== false ) {

$sorted_files排序文件的数组在哪里。希望有帮助,如果没有,我可以尝试写一个例子。

于 2013-04-26T00:13:07.417 回答
1

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

<?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;
            }
        }


    }
}
?>

请给我一个反馈。干杯,里卡多

于 2013-05-09T22:23:29.037 回答
0

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

<?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);
    }
?>
于 2013-05-09T22:46:28.607 回答