如何修复WordPress上传中的图片旋转?

时间:2019-02-06 09:43:53

标签: wordpress upload exif image-rotation

我前面有一个上传表单,该表单通过ajax上传图像,我有一个问题,当用手机捕获图像时,图像会自动旋转! 我该如何解决? 如何使用read_exif_data和更改方向项目?

php代码是:

if( $_SERVER['REQUEST_METHOD'] == "POST" ){
        foreach ( $_FILES['files']['name'] as $f => $name ) {
            $extension = pathinfo( $name, PATHINFO_EXTENSION );
            // Generate a randon code for each file name
            $new_filename = cvf_td_generate_random_code( 20 )  . '.' . $extension;

            if ( $_FILES['files']['error'][$f] == 4 ) {
                continue; 
            }

            if ( $_FILES['files']['error'][$f] == 0 ) {
                // Check if image size is larger than the allowed file size
                if ( $_FILES['files']['size'][$f] > $max_file_size ) {
                    $upload_message[] = "حجم تصویر $name بسیار زیاد است";
                    continue;

                // Check if the file being uploaded is in the allowed file types
                } elseif( ! in_array( strtolower( $extension ), $valid_formats ) ){
                    $upload_message[] = "تصویر $name فرمت درستی ندارد.";
                    continue; 

                } else{ 
                    if( move_uploaded_file( $_FILES["files"]["tmp_name"][$f], $path.$new_filename ) ) {
                        $filename = $path.$new_filename;
                        $filetype = wp_check_filetype( basename( $filename ), null );
                        $wp_upload_dir = wp_upload_dir();
                        $attachment = array(
                            'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ), 
                            'post_mime_type' => $filetype['type'],
                            'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
                            'post_content'   => '',
                            'post_status'    => 'inherit'
                        );
                        // Insert attachment to the database
                        $attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );

                        require_once( ABSPATH . 'wp-admin/includes/image.php' );

                        // Generate meta data
                        $attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); 
                        wp_update_attachment_metadata( $attach_id, $attach_data );

                    }
                }
            }
        }

}

1 个答案:

答案 0 :(得分:0)

如果可以接受插件,则对我来说效果很好 https://de.wordpress.org/plugins/fix-image-rotation/

它修复了上传后的旋转。也许您也可以使用它来窥视其代码并自行实现。