如何获取图像EXIF日期/时间并将其用于WP发布日期/时间

时间:2015-10-13 作者:Arkuen

我目前有以下工作职能。

add_action(\'add_attachment\', \'create_post_from_image\');
function create_post_from_image($id) {
    if (wp_attachment_is_image($id)) {
        $image = get_post($id);
        // get image height/width for auto inserting the image later
        @list($width, $height) = getimagesize(get_attached_file($id));
        $post = array(
            // Set image title as post title
            \'post_title\' => $image->post_title,
            // Set post to draft for details
            \'post_status\' => \'draft\',
            // "Fake" WordPress insert image code
            \'post_content\' => \'<a href="\'.$image->guid.\'"><img class="alignnone size-full wp-image-\'.$image->ID.\'" src="\'.$image->guid.\'" alt="\'.$image->post_name.\'" width="\'.$width.\'" height="\'.$height.\'" /></a>\'
        );
        $postid = wp_insert_post($post);
        if ($postid) {
            // Set image as post featured image
            set_post_thumbnail($postid, $image->ID);
            // Attach image to the post
            wp_update_post(array(
                \'ID\' => $id,
                \'post_parent\' => $postid
                )
            );
        }
    }
}
这个函数的作用本质上是为上传到媒体库的每个图像创建帖子,并将上传的图像嵌入帖子内容。然后将其设置为草稿,供我审阅和发布。

我如何修改它,以便对于上载的每个图像,它将获取嵌入的EXIF数据并获取图像捕获的日期/时间,然后自动将其设置为创建的WP帖子的日期/时间?

1 个回复
最合适的回答,由SO网友:Serg 整理而成

PHP有一个用于此目的的函数:exif_read_data
我用过this image 用于测试
请尝试以下代码:

add_action( \'add_attachment\', \'create_post_from_image\' );
function create_post_from_image( $id ) {
    if ( wp_attachment_is_image( $id ) ) {
        $image = get_post( $id );
        // Get image height/width for auto inserting the image later
        @list( $width, $height ) = getimagesize( get_attached_file( $id ) );

        $post = array(
            // Set image title as post title
            \'post_title\'   => $image->post_title,
            // Set post to draft for details
            \'post_status\'  => \'draft\',
            // "Fake" WordPress insert image code
            \'post_content\' => \'<a href="\' . $image->guid . 
                \'"><img class="alignnone size-full wp-image-\' . $image->ID . 
                \'" src="\' . $image->guid . \'" alt="\' . $image->post_name . 
                \'" width="\' . $width . \'" height="\' . $height . \'" /></a>\'
        );

        // Take image date 
        if ( function_exists( \'exif_read_data\' ) ) {
            $exif = exif_read_data( $image->guid );
            if ( ! empty( $exif[\'DateTime\'] ) ) {
                //var_dump( $exif[\'DateTime\'] );
                $post[\'post_date\'] = $exif[\'DateTime\'];
            }
        }

        $postid = wp_insert_post( $post );
        if ( $postid ) {
            // Set image as post featured image
            set_post_thumbnail( $postid, $image->ID );
            // Attach image to the post
            wp_update_post( array(
                \'ID\'          => $id,
                \'post_parent\' => $postid
            ) );
        }
    }
}

相关推荐

Functions.php上未定义$_GET和&_REQUEST索引

我最近尝试学习为我的自定义主题创建一个主题选项页面,这是按照stackoverflow和其他资源的教程进行的。但脚本显示错误if ($_GET[\'page\'] == basename(__FILE__)) { if (\'save\' == $_REQUEST[\'formaction\']) { foreach ($options as $value) { if( isset( $_REQUEST[ $value[\'id\']