如何解决在WordPress图像旋转上传?

问题描述 投票:0回答:1

我有一个上传表单中主音,通过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 );

                    }
                }
            }
        }

}
wordpress upload exif image-rotation
1个回答
0
投票

如果一个插件是可以接受的,这很适合我https://de.wordpress.org/plugins/fix-image-rotation/

它上传后确定的旋转。也许你也可以用它来窥视它的代码,并在自己实现它。

© www.soinside.com 2019 - 2024. All rights reserved.