【问题标题】:Sort images by exif data in php在php中按exif数据对图像进行排序
【发布时间】:2013-04-19 23:53:41
【问题描述】:

我是 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;
            }
        }


    }
}

【问题讨论】:

  • php 有一个exif
  • 我在 $exif var 中使用它,但如果可能的话,如何按该值对图像进行排序?

标签: php html apache exif


【解决方案1】:

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

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

?>

【讨论】:

    【解决方案2】:

    由于代码会读取目录并在运行时输出 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 是已排序文件的数组。希望有帮助,如果没有我可以尝试写一个例子。

    【讨论】:

    • 我不是很熟悉 :'( 你能给我更多的提示吗?非常感谢
    【解决方案3】:

    对不起,我现在不能尝试这个解决方案,但它应该是这样的:

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

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 2016-03-25
        • 2014-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-22
        • 2016-11-27
        • 1970-01-01
        相关资源
        最近更新 更多