Changeset 10192 for trunk/src/bp-core/bp-core-attachments.php
- Timestamp:
- 10/05/2015 11:56:42 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/bp-core-attachments.php
r10191 r10192 888 888 889 889 /** 890 * Generate the cover image file. 891 * 892 * @since 2.4.0 893 * 894 * @param array $args { 895 * @type string $file The absolute path to the image. Required. 896 * @type string $component The component for the object (eg: groups, xprofile). Required. 897 * @type string $cover_image_dir The Cover image dir to write the image into. Required. 898 * } 899 * @param BP_Attachment_Cover_Image $cover_image_class The class to use to fit the cover image. 900 * @return bool|array An array containing cover image data on success, false otherwise. 901 */ 902 function bp_attachments_cover_image_generate_file( $args = array(), $cover_image_class = null ) { 903 // Bail if an argument is missing 904 if ( empty( $args['file'] ) || empty( $args['component'] ) || empty( $args['cover_image_dir'] ) ) { 905 return false; 906 } 907 908 // Get advised dimensions for the cover image 909 $dimensions = bp_attachments_get_cover_image_dimensions( $args['component'] ); 910 911 // No dimensions or the file does not match with the cover image dir, stop! 912 if ( false === $dimensions || $args['file'] !== $args['cover_image_dir'] . '/' . wp_basename( $args['file'] ) ) { 913 return false; 914 } 915 916 if ( ! is_a( $cover_image_class, 'BP_Attachment_Cover_Image' ) ) { 917 $cover_image_class = new BP_Attachment_Cover_Image(); 918 } 919 920 // Make sure the file is inside the Cover Image Upload path. 921 if ( false === strpos( $args['file'], $cover_image_class->upload_path ) ) { 922 return false; 923 } 924 925 // Resize the image so that it fit with the cover image dimensions 926 $cover_image = $cover_image_class->fit( $args['file'], $dimensions ); 927 $is_too_small = false; 928 929 // Image is too small in width and height 930 if ( empty( $cover_image ) ) { 931 $cover_file = $cover_image_class->generate_filename( $args['file'] ); 932 @rename( $args['file'], $cover_file ); 933 934 // It's too small! 935 $is_too_small = true; 936 } elseif ( ! empty( $cover_image['path'] ) ) { 937 $cover_file = $cover_image['path']; 938 939 // Image is too small in width or height 940 if ( $cover_image['width'] < $dimensions['width'] || $cover_image['height'] < $dimensions['height'] ) { 941 $is_too_small = true; 942 } 943 } 944 945 // We were not able to generate the cover image file. 946 if ( empty( $cover_file ) ) { 947 return false; 948 } 949 950 // Do some clean up with old cover image, now a new one is set. 951 $cover_basename = wp_basename( $cover_file ); 952 953 if ( $att_dir = opendir( $args['cover_image_dir'] ) ) { 954 while ( false !== ( $attachment_file = readdir( $att_dir ) ) ) { 955 // skip directories and the new cover image 956 if ( 2 < strlen( $attachment_file ) && 0 !== strpos( $attachment_file, '.' ) && $cover_basename !== $attachment_file ) { 957 @unlink( $args['cover_image_dir'] . '/' . $attachment_file ); 958 } 959 } 960 } 961 962 // Finally return needed data. 963 return array( 964 'cover_file' => $cover_file, 965 'cover_basename' => $cover_basename, 966 'is_too_small' => $is_too_small 967 ); 968 } 969 970 /** 890 971 * Ajax Upload and set a cover image 891 972 * … … 990 1071 } 991 1072 992 // Get advised dimensions for the cover image993 $dimensions = bp_attachments_get_cover_image_dimensions( $object_data['component'] );994 995 // Resize the image so that it fit with the cover image dimensions996 $cover_image = $cover_image_attachment->fit( $uploaded['file'], $dimensions );997 $is_too_small = false;998 999 // Image is too small in width and height1000 if ( empty( $cover_image ) ) {1001 $cover_file = $cover_image_attachment->generate_filename( $uploaded['file'] );1002 @rename( $uploaded['file'], $cover_file );1003 1004 // It's too small!1005 $is_too_small = true;1006 } elseif ( ! empty( $cover_image['path'] ) ) {1007 $cover_file = $cover_image['path'];1008 1009 if ( $cover_image['width'] < $dimensions['width'] || $cover_image['height'] < $dimensions['height'] ) {1010 $is_too_small = true;1011 }1012 }1013 1014 1073 // Default error message 1015 1074 $error_message = __( 'There was a problem uploading the cover image.', 'buddypress' ); 1016 1017 if ( empty( $cover_file ) ) {1018 // Upload error response1019 bp_attachments_json_response( false, $is_html4, array(1020 'type' => 'upload_error',1021 'message' => $error_message,1022 ) );1023 }1024 1025 // Set the basename for the cover file1026 $cover_basename = wp_basename( $cover_file );1027 1075 1028 1076 // Get BuddyPress Attachments Uploads Dir datas … … 1040 1088 } 1041 1089 1042 // Clean up the cover dir to only keep the uploaded cover image 1043 if ( $att_dir = opendir( $cover_dir ) ) { 1044 while ( false !== ( $attachment_file = readdir( $att_dir ) ) ) { 1045 // skip directories and the new cover image 1046 if ( 2 < strlen( $attachment_file ) && 0 !== strpos( $attachment_file, '.' ) && $cover_basename !== $attachment_file ) { 1047 @unlink( $cover_dir . '/' . $attachment_file ); 1048 } 1049 } 1090 /** 1091 * Generate the cover image so that it fit to feature's dimensions 1092 * 1093 * Unlike the Avatar, Uploading and generating the cover image is happening during 1094 * the same Ajax request, as we already instantiated the BP_Attachment_Cover_Image 1095 * class, let's use it. 1096 */ 1097 $cover = bp_attachments_cover_image_generate_file( array( 1098 'file' => $uploaded['file'], 1099 'component' => $object_data['component'], 1100 'cover_image_dir' => $cover_dir 1101 ), $cover_image_attachment ); 1102 1103 if ( ! $cover ) { 1104 // Upload error response 1105 bp_attachments_json_response( false, $is_html4, array( 1106 'type' => 'upload_error', 1107 'message' => $error_message, 1108 ) ); 1050 1109 } 1051 1110 1052 1111 // Build the url to the file 1053 $cover_url = trailingslashit( $bp_attachments_uploads_dir['baseurl'] ) . $cover_subdir . '/' . $cover _basename;1112 $cover_url = trailingslashit( $bp_attachments_uploads_dir['baseurl'] ) . $cover_subdir . '/' . $cover['cover_basename']; 1054 1113 1055 1114 // Init Feedback code, 1 is success … … 1057 1116 1058 1117 // 0 is the size warning 1059 if ( $ is_too_small) {1118 if ( $cover['is_too_small'] ) { 1060 1119 $feedback_code = 0; 1061 1120 }
Note: See TracChangeset
for help on using the changeset viewer.