WordPress.org

Make WordPress Core

Ticket #18543: gd-imagejpeg-wrapper-ob.2.diff

File gd-imagejpeg-wrapper-ob.2.diff, 5.5 KB (added by adambackstrom, 7 years ago)
  • 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                $filename = null;
     1035                ob_start();
     1036        }
     1037
     1038        $result = call_user_func( $function, $image, $filename, $quality, $filters );
     1039
     1040        if( $result && $stream ) {
     1041                $contents = ob_get_contents();
     1042
     1043                $fp = fopen( $dst_file, 'w' );
     1044
     1045                if( ! $fp )
     1046                        return false;
     1047
     1048                fwrite( $fp, $contents );
     1049                fclose( $fp );
     1050
     1051                ob_end_clean();
     1052        }
     1053
     1054        return $result;
     1055}
     1056
     1057/**
     1058 * Stream resource-aware wrapper for imagejpeg.
     1059 *
     1060 * @uses _wp_imageany
     1061 * @link http://www.php.net/manual/en/function.imagejpeg.php
     1062 *
     1063 * @param resource $image The image resource to write to a file
     1064 * @param string $filename The destinaion file, which may be a stream URL
     1065 * @param int $quality Image quality
     1066 * @return bool True if the image write operation succeeded.
     1067 */
     1068function wp_imagejpeg( $image, $filename, $quality = -1 ) {
     1069        return _wp_imageany( 'imagejpeg', $image, $filename, $quality );
     1070}
     1071
     1072/**
     1073 * Stream resource-aware wrapper for imagepng.
     1074 *
     1075 * @uses _wp_imageany
     1076 * @link http://www.php.net/manual/en/function.imagejpeg.php
     1077 *
     1078 * @param resource $image The image resource to write to a file
     1079 * @param string $filename The destinaion file, which may be a stream URL
     1080 * @param int $quality Image quality
     1081 * @param int $filters Filters to apply to image
     1082 */
     1083function wp_imagepng( $image, $filename, $quality = -1, $filters = null ) {
     1084        return _wp_imageany( 'imagepng', $image, $filename, $quality, $filters );
     1085}
     1086
     1087/**
     1088 * Stream resource-aware wrapper for imagegif.
     1089 *
     1090 * @uses _wp_imageany
     1091 * @link http://www.php.net/manual/en/function.imagejpeg.php
     1092 *
     1093 * @param resource $image The image resource to write to a file
     1094 * @param string $filename The destinaion file, which may be a stream URL
     1095 */
     1096function wp_imagegif( $image, $filename ) {
     1097        return _wp_imageany( 'imagegif', $image, $filename );
     1098}
     1099
     1100/**
    10151101 * API for easily embedding rich media such as videos and images into content.
    10161102 *
    10171103 * @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;