| | 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 | */ |
| | 1030 | function _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 | |
| | 1052 | if( $stream ) { |
| | 1053 | ob_end_clean(); |
| | 1054 | } |
| | 1055 | |
| | 1056 | return $result; |
| | 1057 | } |
| | 1058 | |
| | 1059 | /** |
| | 1060 | * Stream resource-aware wrapper for imagejpeg. |
| | 1061 | * |
| | 1062 | * @uses _wp_imageany |
| | 1063 | * @link http://www.php.net/manual/en/function.imagejpeg.php |
| | 1064 | * |
| | 1065 | * @param resource $image The image resource to write to a file |
| | 1066 | * @param string $filename The destinaion file, which may be a stream URL |
| | 1067 | * @param int $quality Image quality |
| | 1068 | * @return bool True if the image write operation succeeded. |
| | 1069 | */ |
| | 1070 | function wp_imagejpeg( $image, $filename, $quality = -1 ) { |
| | 1071 | return _wp_imageany( 'imagejpeg', $image, $filename, $quality ); |
| | 1072 | } |
| | 1073 | |
| | 1074 | /** |
| | 1075 | * Stream resource-aware wrapper for imagepng. |
| | 1076 | * |
| | 1077 | * @uses _wp_imageany |
| | 1078 | * @link http://www.php.net/manual/en/function.imagejpeg.php |
| | 1079 | * |
| | 1080 | * @param resource $image The image resource to write to a file |
| | 1081 | * @param string $filename The destinaion file, which may be a stream URL |
| | 1082 | * @param int $quality Image quality |
| | 1083 | * @param int $filters Filters to apply to image |
| | 1084 | */ |
| | 1085 | function wp_imagepng( $image, $filename, $quality = -1, $filters = null ) { |
| | 1086 | return _wp_imageany( 'imagepng', $image, $filename, $quality, $filters ); |
| | 1087 | } |
| | 1088 | |
| | 1089 | /** |
| | 1090 | * Stream resource-aware wrapper for imagegif. |
| | 1091 | * |
| | 1092 | * @uses _wp_imageany |
| | 1093 | * @link http://www.php.net/manual/en/function.imagejpeg.php |
| | 1094 | * |
| | 1095 | * @param resource $image The image resource to write to a file |
| | 1096 | * @param string $filename The destinaion file, which may be a stream URL |
| | 1097 | */ |
| | 1098 | function wp_imagegif( $image, $filename ) { |
| | 1099 | return _wp_imageany( 'imagegif', $image, $filename ); |
| | 1100 | } |
| | 1101 | |
| | 1102 | /** |