Opened 5 months ago
Last modified 8 weeks ago
#62729 assigned defect (bug)
stream_preview_image() should stream with right mime type, if filter _load_image_to_edit_path change file with different mime type
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | 6.7.1 |
Component: | Post Thumbnails | Keywords: | has-patch has-unit-tests |
Focuses: | Cc: |
Description
Hi, im working on allowing image editor for PDF thumnbails https://core.trac.wordpress.org/ticket/62712.
in functiion stream_preview_image() in wp-admin/includes/image-edit.php - line 766
is function with filter
<?php _load_image_to_edit_path( $post_id )
we can change filepath, but for example if we change to
metadata['sizes']['full']
which has mimetype image/jpg, function stream_preview_image() dont send image to browser. Bug is in
<?php return wp_stream_image( $img, $post->post_mime_type, $post_id );
should be
<?php return wp_stream_image( $img, $img['mime-type'], $attachment_id );
Change History (12)
This ticket was mentioned in PR #8041 on WordPress/wordpress-develop by @sukhendu2002.
5 months ago
#1
- Keywords has-patch added
This ticket was mentioned in PR #8117 on WordPress/wordpress-develop by @pbearne.
4 months ago
#2
- Keywords has-unit-tests added
Introduced detailed PHPUnit tests for AJAX image cropping preview functionality. Made get_mime_type
a public method and adjusted MIME type handling in stream_preview_image
for better compatibility with image editors. These changes ensure more robustness in image handling and testing.
Trac ticket: [](https://core.trac.wordpress.org/ticket/62729)
#3
@
4 months ago
- Owner set to pbearne
- Status changed from new to assigned
I have added some test but I was not able to test the header change in this ticket as the test suit does not seem to allow this
maybe someone else can help testing
Paul
#4
@
4 months ago
I have nasty fix
<?php // bug in wp_stream_image in image-edit.php, bad mime type add_filter( 'image_editor_save_pre', 'core_thumbnail_generator_image_editor_save_pre', 10, 2 ); function core_thumbnail_generator_image_editor_save_pre( $image, $attachment_id ) { if ( did_action( 'wp_ajax_imgedit-preview' ) ) { if ( is_wp_error( $image->stream() ) ) { wp_die( -1 ); } else { wp_die(); } } }
#5
follow-up:
↓ 6
@
4 months ago
Same bug is in
<?php function wp_save_image( $post_id ) { $post = get_post( $post_id ); $img = wp_get_image_editor( _load_image_to_edit_path( $post_id, 'full' ) ); $saved_image = wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id );
Cannot change mime_type.
#6
in reply to:
↑ 5
@
4 months ago
Replying to Hrohh:
Same bug is in
<?php function wp_save_image( $post_id ) { $post = get_post( $post_id ); $img = wp_get_image_editor( _load_image_to_edit_path( $post_id, 'full' ) ); $saved_image = wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id );Cannot change mime_type.
OK we will need to add a filter where in the function should put this?
#7
@
4 months ago
I have moved the get_mine_type to after the filter returns so this should solve the problem stream_preview_image
#8
@
3 months ago
Hi sir, sorry for long delay.
function wp_save_image is here =>
https://github.com/WordPress/wordpress-develop/blob/626d9af280566d008ccf501952f119fb0ce61662/src/wp-admin/includes/image-edit.php#L904
relevant code is
<?php $post = get_post( $post_id ); $img = wp_get_image_editor( _load_image_to_edit_path( $post_id, 'full' ) ); $saved_image = wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id );
so with your patch is should be
<?php $post = get_post( $post_id ); $img = wp_get_image_editor( _load_image_to_edit_path( $post_id, 'full' ) ); $mime_type = $post->post_mime_type; if ( method_exists( $img, 'get_mime_type' ) ) { $mime_type = $img->get_mime_type(); } $saved_image = wp_save_image_file( $new_path, $img, $mime_type, $post_id );
Thank you.
#9
@
3 months ago
@Hrohh the patch doesn't match the current code so I am confused as what you are trying to do
I can't see the filter that would change the mine type
can provide more info?
Paul
#10
@
2 months ago
@pbearne If I change target image in filter in function _load_image_to_edit_path from jpg to avif for example
in wp_save_image_file this is not taken into account
$saved_image = wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id )
Thank you.
#11
@
2 months ago
@Hrohh can you provide an example of the filter that breaks the mine type
I am having problems recreating
Paul
#12
@
8 weeks ago
@pbearne Hi Paul, like this
<?php add_filter( 'load_image_to_edit_path', 'avif_load_image_to_edit_path', 10, 3 ); function avif_load_image_to_edit_path( $filepath, $attachment_id, $size ) { return str_replace( '.jpg', '.avif', $filepath ); }
wp_stream_image function will send image/jpeg instead image/avif.
Thank you.
Trac ticket: https://core.trac.wordpress.org/ticket/62729