WordPress.org

Ready to get started?Download WordPress

Forums

admin-ajax.php imgedit-preview header problem (6 posts)

  1. hugofcampos
    Member
    Posted 2 years ago #

    Hi.

    I can'n be sure that I'm in the right section of this forum. Actually I'm am here because I'm not sure about what's happening... rs

    Well, I use the "Post thumbnail Editor" in my website. After move my site to a IIS Server (I know, it sux, but my customer call for it), the page for select area for the thumbnail did not show the main image anymore.

    After debug many .php files, I found that the source of this bug was a WordPress core instruction. In the wp-admin/includes/image-edit.php you can found these lines (beginning at line 239):

    function wp_stream_image($image, $mime_type, $post_id) {
    	$image = apply_filters('image_save_pre', $image, $post_id);
    
    	switch ( $mime_type ) {
    		case 'image/jpeg':
    			header('Content-Type: image/jpeg');
    			return imagejpeg($image, null, 90);
    		case 'image/png':
    			header('Content-Type: image/png');
    			return imagepng($image);
    		case 'image/gif':
    			header('Content-Type: image/gif');
    			return imagegif($image);
    		default:
    			return false;
    	}
    }

    In my tests, I have used jpeg images and, I can't say why, the header had no effect and the image was blank.

    Searching for a solution, a have found this forum page and a instruction call my attention. And it solved the bug!

    function wp_stream_image($image, $mime_type, $post_id) {
    	$image = apply_filters('image_save_pre', $image, $post_id);
    
    	while (@ob_end_clean());
    
    	switch ( $mime_type ) {
    		case 'image/jpeg':
    			header('Content-Type: image/jpeg');
    			return imagejpeg($image, null, 90);
    		case 'image/png':
    			header('Content-Type: image/png');
    			return imagepng($image);
    		case 'image/gif':
    			header('Content-Type: image/gif');
    			return imagegif($image);
    		default:
    			return false;
    	}
    }

    I found that this ob_end_clean function "discards the contents of the topmost output buffer and turns off this output buffering". Ok, it works for me, but what I want to know is if is safe to call this function there. As I said, I'm at WordPress Core. Does anyone know a better solution?

    Many thanks!

  2. sewpafly
    Member
    Posted 1 year ago #

    I solved the same issue/behavior. It was caused by a theme with a functions.php with some empty lines at the end of the file after the ?>. (Solution: remove the empty lines). If this isn't the case I imagine a similar issue could be caused by a plugin...

  3. stakabo
    Member
    Posted 1 year ago #

    Hi,

    I just spend some time figuring out why this did not work on multiple site we have.
    We found 2 possible cause:
    1 - Any spaces left before the first <?php tag or after the last ?> tag. this could be in any plugins or in the current themes files.

    2- I also found that if you have anything using the hook image_save_pre it will cause this feature to fail.

    Hope this helps anyone.

    Have fun

  4. sewpafly
    Member
    Posted 1 year ago #

    @stakabo - Thanks!!

  5. Mediacleg
    Member
    Posted 1 year ago #

    Thanks, can u explain what means " if you have anything using the hook image_save_pre it will cause this feature to fail."

    Sorry i don't understand... :-\

  6. stakabo
    Member
    Posted 1 year ago #

    There is a WP hook called "image_save_pre".

    This hooked is expecting your function to send back an image.
    If it does not retunr an image, it will make wp_stream_image() fail to do it's job.

Topic Closed

This topic has been closed to new replies.

About this Topic