【发布时间】:2021-01-29 08:42:17
【问题描述】:
我找到了一种插入base64图像作为附件的方法,它插入成功但不显示图像的缩略图和大小。
这是我的代码:
$imgpath = "data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAA34AAAK8C...";
$upload_dir = wp_upload_dir();
$upload_path = str_replace( '/', DIRECTORY_SEPARATOR, $upload_dir['path'] ) . DIRECTORY_SEPARATOR;
$img = str_replace('data:image/jpeg;base64,','',$imgpath);
$img = str_replace( ' ', '+', $img );
$decoded = base64_decode( $img );
$filename = "preview.jpeg";
$hashed_filename = md5( $filename . microtime() ) . '_' . $filename;
$image_upload = file_put_contents( $upload_path . $hashed_filename, $decoded );
$attachment = array(
'post_mime_type' => "image/jpeg",
'post_title' => preg_replace('/\.[^.]+$/', '', basename($hashed_filename)),
'post_content' => '',
'post_status' => 'inherit',
'guid' => $wp_upload_dir['url'] . '/' . basename($hashed_filename)
);
$attach_id = wp_insert_attachment( $attachment, $upload_dir['path'] . '/' . $hashed_filename );
我不知道哪里出了问题或遗漏了什么,请帮忙!
谢谢
【问题讨论】:
标签: wordpress image base64 attachment