web-dev-qa-db-fra.com

Comment obtenir la date/heure EXIF ​​de l’image et l’utiliser pour la WP poster date/heure

J'ai actuellement la fonction de travail suivante.

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

Cette fonction consiste essentiellement à créer des publications pour chaque image téléchargée vers la médiathèque et à incorporer cette image dans le contenu de la publication. Il le définit ensuite comme un brouillon que je peux relire et publier.

Comment la modifier pour que, pour chaque image téléchargée, les données EXIF ​​incorporées soient récupérées et que la date/l'heure de la capture de l'image soient capturées, puis automatiquement définies comme date/heure de la publication WP est créé?

5
Arkuen

PHP a une fonction à cet effet: exif_read_data
J'ai utilisé cette image pour les tests.
Essayez ce code pour votre but:

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
            ) );
        }
    }
}
8
Serg