【问题标题】:how to fix image rotation in WordPress upload?如何修复 WordPress 上传中的图像旋转?
【发布时间】:2019-02-06 09:43:52
【问题描述】:

我在前端有一个通过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】:

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

    它在上传后修复了旋转。也许你也可以用它来查看它的代码并自己实现它。

    【讨论】:

    • 感谢您的重播,我尝试了所有插件,但不幸的是它们都不起作用!
    猜你喜欢
    • 2020-03-17
    • 2019-12-14
    • 1970-01-01
    • 2019-12-06
    • 2019-02-04
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多