Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Save a base64 jpeg image on the server in Media library.
- */
- function save_image($base64_image_string, $output_file_name_without_extension) {
- global $wp_filesystem;
- // Initialize the WP filesystem, no more using 'file-put-contents' function
- if (empty($wp_filesystem)) {
- require_once (ABSPATH . '/wp-admin/includes/file.php');
- WP_Filesystem();
- }
- // get uploads dir
- $upload_dir = wp_upload_dir();
- $upload_basedir = $upload_dir['basedir'];
- $upload_baseurl = $upload_dir['baseurl'];
- $screens_dir = $upload_basedir . '/flo_forms_screens/'; // custom folder where the screen shots will be kept
- // if folder that will hold the screens does not exist yet, create it
- if(!$wp_filesystem->is_dir($screens_dir)) {
- $wp_filesystem->mkdir($screens_dir);
- }
- // folder exists, push image to it
- $file = false;
- if($wp_filesystem->is_dir($screens_dir)) {
- $img = str_replace( 'data:image/jpeg;base64,', '', $base64_image_string );
- $img = str_replace( ' ', '+', $img );
- $decoded = base64_decode( $img );
- $hashed_filename = md5( $output_file_name_without_extension . microtime() ) . '_' . $output_file_name_without_extension;
- $filePath = $screens_dir . $hashed_filename . ".jpeg";
- $fileCallback = $wp_filesystem->put_contents(
- $filePath,
- $decoded,
- FS_CHMOD_FILE // predefined mode settings for WP files
- );
- $publicFilePath = str_replace($upload_basedir, $upload_baseurl, $filePath);
- if($fileCallback) $file = $publicFilePath;
- }
- return $file;
- }
Add Comment
Please, Sign In to add comment