Wordpress: why after downloading the file from the front it appears crooked in the photo library?

Download images of fouzia wp_handle_upload, all loaded, everything is OK, and in your library are displayed here.

And if you upload image with media_handle_upload all the rules, but for my tricks it's not easy, sideload too. What cant be?

Here is the code responsible for loading images:

$movefile = wp_handle_upload( $file, array( 'test_form' => false ) );
 $filename = $movefile['file'];
 $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'
);

 wp_insert_attachment( $attachment, $filename );<img src="https://habrastorage.org/files/bac/4bb/d32/bac4bbd327cd41d0865ea2f090cd5455.jpg" alt="image">
July 2nd 19 at 13:42
2 answers
July 2nd 19 at 13:44
Solution
I'm not sure of the proper response, after all You are running it from another function, but I'd check whether the php extension GD
but how? what is it? - jaquelin_Litt commented on July 2nd 19 at 13:47
print_r(get_loaded_extensions());
?>
And see if the gd list
GD Graphics Library - https://ru.wikipedia.org/wiki/GD_Graphics_Library - beaulah.Gaylord commented on July 2nd 19 at 13:50
July 2nd 19 at 13:46
Solution
In General figured out, you also need these 2 functions to add the loaded image to make it visible in the media library:
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
 wp_update_attachment_metadata( $attachment_id, $attachment_data );

Find more questions by tags WordPress