WordPress.org

Make WordPress Core

Ticket #18543: gd-imagejpeg-wrapper-tempnam.diff

File gd-imagejpeg-wrapper-tempnam.diff, 5.5 KB (added by adambackstrom, 4 years ago)

Alternate using temp file rather than output buffering (recommended by dd32)

  • wp-includes/functions.php

     
    45494561        @header( 'X-Frame-Options: SAMEORIGIN' ); 
    45504562} 
    45514563 
     4564/** 
     4565 * Test if a given path is a stream URL 
     4566 *  
     4567 * @param string $path The resource path or URL 
     4568 * @return bool True if the path is a stream URL 
     4569 */ 
     4570function wp_is_stream( $path ) { 
     4571        $wrappers = stream_get_wrappers(); 
     4572        $wrappers_re = '(' . join('|', $wrappers) . ')'; 
     4573 
     4574        return preg_match( "!^$wrappers_re://!", $path ) === 1; 
     4575} 
     4576 
    45524577?> 
  • wp-includes/media.php

     
    444444        $destfilename = "{$dir}/{$name}-{$suffix}.{$ext}"; 
    445445 
    446446        if ( IMAGETYPE_GIF == $orig_type ) { 
    447                 if ( !imagegif( $newimage, $destfilename ) ) 
     447                if ( !wp_imagegif( $newimage, $destfilename ) ) 
    448448                        return new WP_Error('resize_path_invalid', __( 'Resize path invalid' )); 
    449449        } elseif ( IMAGETYPE_PNG == $orig_type ) { 
    450                 if ( !imagepng( $newimage, $destfilename ) ) 
     450                if ( !wp_imagepng( $newimage, $destfilename ) ) 
    451451                        return new WP_Error('resize_path_invalid', __( 'Resize path invalid' )); 
    452452        } else { 
    453453                // all other formats are converted to jpg 
    454454                $destfilename = "{$dir}/{$name}-{$suffix}.jpg"; 
    455                 if ( !imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) ) ) 
     455                if ( !wp_imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) ) ) 
    456456                        return new WP_Error('resize_path_invalid', __( 'Resize path invalid' )); 
    457457        } 
    458458 
     
    10121012} 
    10131013 
    10141014/** 
     1015 * Wrap the GD image* functions (imagejpeg, imagepng, imagegif) so that images can be 
     1016 * written to stream wrappers. 
     1017 * 
     1018 * @uses wp_is_stream() 
     1019 * @see imagejpeg 
     1020 * @see imagepng 
     1021 * @see imagegif 
     1022 * 
     1023 * @param string $function The GD function to call 
     1024 * @param resource $image The image resource to write to a file 
     1025 * @param string $filename The destinaion file, which may be a stream URL 
     1026 * @param int $quality Image quality (imagejpeg, imagepng) 
     1027 * @param int $filters Image filters (imagepng) 
     1028 * @return bool True if the image write operation succeeded. 
     1029 */ 
     1030function _wp_imageany( $function, $image, $filename, $quality = -1, $filters = null ) { 
     1031        $dst_file = $filename; 
     1032 
     1033        if ( $stream = wp_is_stream($filename) ) { 
     1034                if ( !function_exists('wp_tempnam') ) 
     1035                        include ABSPATH . '/wp-admin/includes/file.php'; 
     1036 
     1037                $filename = wp_tempnam('imageany'); 
     1038        } 
     1039 
     1040        $result = call_user_func( $function, $image, $filename, $quality, $filters ); 
     1041 
     1042        if( $stream ) { 
     1043                if( $result ) 
     1044                        $fp = copy( $filename, $dst_file ); 
     1045 
     1046                unlink( $filename ); 
     1047        } 
     1048 
     1049        return $result; 
     1050} 
     1051 
     1052/** 
     1053 * Stream resource-aware wrapper for imagejpeg. 
     1054 * 
     1055 * @uses _wp_imageany 
     1056 * @link http://www.php.net/manual/en/function.imagejpeg.php 
     1057 * 
     1058 * @param resource $image The image resource to write to a file 
     1059 * @param string $filename The destinaion file, which may be a stream URL 
     1060 * @param int $quality Image quality 
     1061 * @return bool True if the image write operation succeeded. 
     1062 */ 
     1063function wp_imagejpeg( $image, $filename, $quality = -1 ) { 
     1064        return _wp_imageany( 'imagejpeg', $image, $filename, $quality ); 
     1065} 
     1066 
     1067/** 
     1068 * Stream resource-aware wrapper for imagepng. 
     1069 * 
     1070 * @uses _wp_imageany 
     1071 * @link http://www.php.net/manual/en/function.imagejpeg.php 
     1072 * 
     1073 * @param resource $image The image resource to write to a file 
     1074 * @param string $filename The destinaion file, which may be a stream URL 
     1075 * @param int $quality Image quality 
     1076 * @param int $filters Filters to apply to image 
     1077 */ 
     1078function wp_imagepng( $image, $filename, $quality = -1, $filters = null ) { 
     1079        return _wp_imageany( 'imagepng', $image, $filename, $quality, $filters ); 
     1080} 
     1081 
     1082/** 
     1083 * Stream resource-aware wrapper for imagegif. 
     1084 * 
     1085 * @uses _wp_imageany 
     1086 * @link http://www.php.net/manual/en/function.imagejpeg.php 
     1087 * 
     1088 * @param resource $image The image resource to write to a file 
     1089 * @param string $filename The destinaion file, which may be a stream URL 
     1090 */ 
     1091function wp_imagegif( $image, $filename ) { 
     1092        return _wp_imageany( 'imagegif', $image, $filename ); 
     1093} 
     1094 
     1095/** 
    10151096 * API for easily embedding rich media such as videos and images into content. 
    10161097 * 
    10171098 * @package WordPress 
  • wp-admin/includes/image-edit.php

     
    262262 
    263263        switch ( $mime_type ) { 
    264264                case 'image/jpeg': 
    265                         return imagejpeg( $image, $filename, apply_filters( 'jpeg_quality', 90, 'edit_image' ) ); 
     265                        return wp_imagejpeg( $image, $filename, apply_filters( 'jpeg_quality', 90, 'edit_image' ) ); 
    266266                case 'image/png': 
    267                         return imagepng($image, $filename); 
     267                        return wp_imagepng($image, $filename); 
    268268                case 'image/gif': 
    269                         return imagegif($image, $filename); 
     269                        return wp_imagegif($image, $filename); 
    270270                default: 
    271271                        return false; 
    272272        } 
  • wp-admin/includes/image.php

     
    7171 
    7272        $dst_file = preg_replace( '/\\.[^\\.]+$/', '.jpg', $dst_file ); 
    7373 
    74         if ( imagejpeg( $dst, $dst_file, apply_filters( 'jpeg_quality', 90, 'wp_crop_image' ) ) ) 
     74        if ( wp_imagejpeg( $dst, $dst_file, apply_filters( 'jpeg_quality', 90, 'wp_crop_image' ) ) ) 
    7575                return $dst_file; 
    7676        else 
    7777                return false;