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

申し訳ありませんが、現在この解決策を試すことはできませんが、次のようなものにする必要があります。

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

ツールチップにマウスを合わせると正しい日付が表示されますが、サムネイルや画像は読み込まれません。最初の質問では、最後の 2 つの関数を省略しました。これが現在の更新コードです。

<?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 に答える