Reputation: 369
I'm converting a canvas in a base64 png image and now I would like to add this image as a post attachment. This is my image in server-side:
$image =
base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $data['pdf_thumbnail_'.$item_id]));
I would like to use wp_insert_attachment()
to add this image as an attachment. How can I do this?
Upvotes: 3
Views: 10587
Reputation: 244
I faced the same problem, my solution.
function upload_base64_file($base64_image, $post_id)
{
if (empty($base64_image) || empty($post_id)) return false;
// Base64-encoded image data
// Decode the Base64 image data
$image_data = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $base64_image));
// Determine the upload directory
$upload_dir = wp_upload_dir();
$image_name = '/avatar' . time() . '.jpg';
$image_path = $upload_dir['path'] . $image_name;
// Save the image to the specified location
if (file_put_contents($image_path, $image_data)) {
// Изображение успешно сохранено
// Add the image to the WordPress Media Library
$attachment = array(
'post_mime_type' => 'image/jpeg', // Замените на соответствующий MIME-тип
'post_title' => $image_name, // Замените на желаемый заголовок
'post_content' => '',
'post_status' => 'inherit',
);
$attach_id = wp_insert_attachment($attachment, $image_path, $post_id);
if (!is_wp_error($attach_id)) {
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attachment_data = wp_generate_attachment_metadata($attach_id, $image_path);
wp_update_attachment_metadata($attach_id, $attachment_data);
return [
"status" => ($attach_id !== 0) ? "success" : "error",
'name_file' => $image_name,
'attach_id' => $attach_id
];
}
}
}
Upvotes: 0
Reputation: 41
** I have used this code, so this working properly Use this code:**
//HANDLE UPLOADED FILE
$upload_dir = wp_upload_dir();
$upload_path = str_replace( '/', DIRECTORY_SEPARATOR, $upload_dir['path'] ) . DIRECTORY_SEPARATOR;
$image_parts = explode(";base64,",$_POST['image']);
$decoded = base64_decode($image_parts[1]);
$filename = 'wigo.png';
$hashed_filename = md5( $filename . microtime() ) . '_' . $filename;
$image_upload = file_put_contents( $upload_path . $hashed_filename, $decoded );
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
// Without that I'm getting a debug error!?
$file = array();
$file['error'] = '';
$file['tmp_name'] = $upload_path . $hashed_filename;
$file['name'] = $hashed_filename;
$file['type'] = 'image/png';
$file['size'] = filesize( $upload_path . $hashed_filename );
// upload file to server
// use $file instead of $image_upload
$file_return = wp_handle_sideload( $file, array( 'test_form' => false ) );
$filename = $file_return['file'];
$attachment = array(
'post_mime_type' => $file_return['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit',
'guid' => $wp_upload_dir['url'] . '/' . basename($filename)
);
$attach_id = wp_insert_attachment( $attachment, $filename );
/// generate thumbnails of newly uploaded image
$attachment_meta = wp_generate_attachment_metadata($attach_id, $filename );
wp_update_attachment_metadata($attach_id,$attachment_meta);
set_post_thumbnail($post_id,$attach_id);
Upvotes: 4
Reputation: 1300
Use this code:
$upload_dir = wp_upload_dir();
// @new
$upload_path = str_replace( '/', DIRECTORY_SEPARATOR, $upload_dir['path'] ) . DIRECTORY_SEPARATOR;
$decoded = $image;
$filename = 'my-base64-image.png';
$hashed_filename = md5( $filename . microtime() ) . '_' . $filename;
// @new
$image_upload = file_put_contents( $upload_path . $hashed_filename, $decoded );
//HANDLE UPLOADED FILE
if( !function_exists( 'wp_handle_sideload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
}
// Without that I'm getting a debug error!?
if( !function_exists( 'wp_get_current_user' ) ) {
require_once( ABSPATH . 'wp-includes/pluggable.php' );
}
// @new
$file = array();
$file['error'] = '';
$file['tmp_name'] = $upload_path . $hashed_filename;
$file['name'] = $hashed_filename;
$file['type'] = 'image/png';
$file['size'] = filesize( $upload_path . $hashed_filename );
// upload file to server
// @new use $file instead of $image_upload
$file_return = wp_handle_sideload( $file, array( 'test_form' => false ) );
$filename = $file_return['file'];
$attachment = array(
'post_mime_type' => $file_return['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit',
'guid' => $wp_upload_dir['url'] . '/' . basename($filename)
);
$attach_id = wp_insert_attachment( $attachment, $filename );
Upvotes: 3