php Wordpress - 修复因使用iPhone拍摄的肖像照片在媒体库中横向显示的问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Wordpress - 修复因使用iPhone拍摄的肖像照片在媒体库中横向显示的问题相关的知识,希望对你有一定的参考价值。

<?php 
global $orientation_fixed;
$orientation_fixed = array();
        
add_filter( 'wp_handle_upload', 'filter_wp_handle_upload' );

function filter_wp_handle_upload( $file ) {
    fixImageOrientation( $file['file'], $file['type'] );
    return $file;
}

add_filter( 'wp_handle_upload_prefilter', 'filter_wp_handle_upload_prefilter' );

function filter_wp_handle_upload_prefilter( $file ) {
    $suffix = substr( $file['name'], strrpos( $file['name'], '.' ) + 1 );
    switch ( strtolower($suffix) ) {
        case 'jpg':
        case 'jpeg':
            $type = 'image/jpeg';
            break;
        case 'png':
            $type = 'image/png';
            break;
        case 'gif':
            $type = 'image/gif';
            break;
    }
    if ( isset( $type ) ) {
        fixImageOrientation( $file['tmp_name'], $type );
    }
    return $file;
}

function fixImageOrientation( $file, $type ) {
    if ( is_callable('exif_read_data') && !isset( $oreintation_fixed[$file] ) ) {
        $exif = @exif_read_data( $file );
        if ( isset($exif) && isset($exif['Orientation']) && $exif['Orientation'] > 1 ) { 
            include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
            switch ( $exif['Orientation'] ) {
                case 3:
                    $orientation = -180;
                    break;
                case 6:
                    $orientation = -90;
                    break;
                case 8:
                case 9:
                    $orientation = -270;
                    break;
                default:
                    $orientation = 0;
                    break;
            }
            switch ( $type ) {
                case 'image/jpeg':
                    $image = imagecreatefromjpeg( $file );
                    break;
                case 'image/png':
                    $image = imagecreatefrompng( $file );
                    break;
                case 'image/gif':
                    $image = imagecreatefromgif( $file );
                    break;
                default:
                    $image = false;
                    break;
            }
            if ($image) { 
                $image = _rotate_image_resource( $image, $orientation );
                switch ( $type ) {
                    case 'image/jpeg':
                        imagejpeg( $image, $file, apply_filters( 'jpeg_quality', 90, 'edit_image' ) );
                        break;
                    case 'image/png':
                        imagepng($image, $file );
                        break;
                    case 'image/gif':
                        imagegif($image, $file );
                        break;
                }
            }
        }
    }
    $orientation_fixed[$file] = true;
}

function upload_user_file( $file = array() ) {
    require_once( ABSPATH . 'wp-admin/includes/admin.php' );
    $file_return = wp_handle_upload( $file, array('test_form' => false ) );
    if ( isset( $file_return['error'] ) || isset( $file_return['upload_error_handler'] ) ) {
        return false;
    } else {
        $filename = $file_return['file'];
        $attachment = array(
            'post_mime_type' => $file_return['type'],
            'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
            'post_content' => '',
            'post_status' => 'inherit',
            'guid' => $file_return['url']
        );
        $attachment_id = wp_insert_attachment( $attachment, $file_return['url'] );
        require_once(ABSPATH . 'wp-admin/includes/image.php');
        $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
        wp_update_attachment_metadata( $attachment_id, $attachment_data );
        if ( 0 < intval( $attachment_id ) ) {
            return $attachment_id;
        }
    }
    return false;
}
?>

以上是关于php Wordpress - 修复因使用iPhone拍摄的肖像照片在媒体库中横向显示的问题的主要内容,如果未能解决你的问题,请参考以下文章

php Wordpress自动P标签修复

php 修复Wordpress错误

PHP Wordpress 2.7+图库样式修复

PHP 修复Wordpress编辑器问题

PHP Wordpress MU timthumb修复

PHP 管理员修复WordPress