問題描述
我終於得到這件事我已經嘗試了 12 次製作和 12 種不同的方法,但終於得到它的工作,… 有一些。
我做了一個自定義的 metabox 上傳和附加圖像到帖子,它不需要你使用內置在 WP 中的可怕的 thickbox 媒體上傳器。我討厭那件事不,我所做的只是一組輸入 (標題,描述,文件),您也可以複製這些輸入,以便添加附加附件。所以你填寫字段,選擇要上傳的圖像,並保存草稿或發佈帖子。一旦將附件添加到帖子中,metabox 將顯示輸入字段,以及您添加的每個附件的附件圖像的預覽圖像。標題和描述字段用於生成文件元數據,沒有什麼可以保存為我知道的 post_meta 。目前為止,我已經開始工作了。
我需要這樣做,以便當您保存/發佈帖子時,內容上傳/創建附件文件,它將創建三個圖像大小作為默認的 wp 上傳者將,縮略圖,中等,大,並保持完整大小的圖像。如果這樣可能的話。如果沒有,我想以其他方式使用 add_image_size()創建/定義新的自定義尺寸,並在上傳時以其生成方式。
我不知道在這種情況下最適合使用哪種功能,也許 image_make_intermediate_size()功能會更好,或者 wp_create_thumbnail()或 wp_crop_image() … 誰知道!
我不知道如何去做,如果我需要為每一個運行 wp_handle_upload()功能,或者也許涉及 wp_generate_attachment_metadata()功能的東西。這對我來説很困惑,因為 3 個圖像大小要作為相同附件的變體相關聯,以及如何去做。
我已經瀏覽網頁,閲讀每個 wp 媒體/上傳/圖片相關文件的來源,並播放與媒體上傳的所有功能,無法找到 WP 如何創建 3 個圖像大小在任何地方,或如何做它自己
在 wp-includes /media.php 中,image_resize()功能看起來像是最好的,因為它應該是最好的。我只是無法想出我的生活,我失蹤了或曾經嘗試做過,但做錯圖像的縮略圖。
這是我的工作功能,wp_handle_upload()的東西和東西,但它也需要創建 100px 的拇指,並使一個像 500 像素 max-width 的圖像的大小版本,並保存為上傳的新文件。
function update_attachment(){
global $post;
wp_update_attachment_metadata( $post->ID, $_POST['a_image'] );
if( !empty( $_FILES['a_image']['name'] )) { //New upload
require_once( ABSPATH . 'wp-admin/includes/file.php' );
$override['action'] = 'editpost';
$url = wp_handle_upload( $_FILES['a_image'], $override );
// $medium = image_make_intermediate_size( $uploaded_file['url'], 500, 400, true );
// $thumb = = image_make_intermediate_size( $uploaded_file['url'], 100, 100, true );
if ( isset( $file['error'] )) {
return new WP_Error( 'upload_error', $file['error'] );
}
$array_type = wp_check_filetype
$allowed_file_types = array('image/jpg','image/jpeg','image/gif','image/png');
$name_parts = pathinfo( $name );
$name = trim( substr( $name, 0, - ( 1 + strlen( $name_parts['extension'] )) ));
$type = $file['type'];
$file = $file['file'];
$title = $_POST['a_title'] ? $_POST['a_title'] : $name;
$content = $_POST['a_desc']
$post_id = $post->ID;
$attachment = array(
'post_title' => $title,
'post_type' => 'attachment',
'post_content' => $content,
'post_parent' => $post_id,
'post_mime_type' => $type,
'guid' => $url['url']
);
// Save the data
$id = wp_insert_attachment( $attachment, $_FILES['a_image'][ 'file' ]/*, $post_id - for post_thumbnails*/);
if ( !is_wp_error( $id )) {
$attach_meta = wp_generate_attachment_metadata( $id, $uploaded_file['url'] );
wp_update_attachment_metadata( $attach_id, $attach_meta );
}
update_post_meta( $post->ID, 'a_image', $uploaded_file['url'] );
}
}
任何人能夠幫助我終於解決這個問題,所以它的工作正常將被愛。我花了這麼多可笑的無數小時多次不同的時間試圖開發這個東西,文件很爛,而且沒有任何關於如何做的好帖子。
謝謝
最佳解決方案
你好 @jaredwilli:
好傢伙!偉大的努力,和好的工作。 All-in-all 它可能是 WordPress 的一個很好的補充。
你是如此接近,但是你有 5 到 10 個小失敗的假設或代碼,看起來像你開始,但沒有回到它,因為它不工作。我修改了你的功能,只要我需要糾正它。解決方案如下,我將把 side-by-side 與您或更少的 burned-out 進行比較。 🙂
function update_attachment() {
global $post;
wp_update_attachment_metadata( $post->ID, $_POST['a_image'] );
if( !empty( $_FILES['a_image']['name'] )) { //New upload
require_once( ABSPATH . 'wp-admin/includes/file.php' );
$override['action'] = 'editpost';
$file = wp_handle_upload( $_FILES['a_image'], $override );
if ( isset( $file['error'] )) {
return new WP_Error( 'upload_error', $file['error'] );
}
$file_type = wp_check_filetype($_FILES['a_image']['name'], array(
'jpg|jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png',
));
if ($file_type['type']) {
$name_parts = pathinfo( $file['file'] );
$name = $file['filename'];
$type = $file['type'];
$title = $_POST['a_title'] ? $_POST['a_title'] : $name;
$content = $_POST['a_desc'];
$post_id = $post->ID;
$attachment = array(
'post_title' => $title,
'post_type' => 'attachment',
'post_content' => $content,
'post_parent' => $post_id,
'post_mime_type' => $type,
'guid' => $file['url'],
);
foreach( get_intermediate_image_sizes() as $s ) {
$sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => true );
$sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options
$sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
$sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
}
$sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );
foreach( $sizes as $size => $size_data ) {
$resized = image_make_intermediate_size( $file['file'], $size_data['width'], $size_data['height'], $size_data['crop'] );
if ( $resized )
$metadata['sizes'][$size] = $resized;
}
$attach_id = wp_insert_attachment( $attachment, $file['file'] /*, $post_id - for post_thumbnails*/);
if ( !is_wp_error( $id )) {
$attach_meta = wp_generate_attachment_metadata( $attach_id, $file['file'] );
wp_update_attachment_metadata( $attach_id, $attach_meta );
}
update_post_meta( $post->ID, 'a_image', $file['url'] );
}
}
}
嘿,為什麼不爆發一下 PhpStorm 的副本?你可以很容易地解決這個問題,你很親密,如果你可以跟蹤我現在可以做的代碼。如果你這樣做,不要浪費你的時間在非常錯誤的 XDEBUG,而是下載 Zend Debugger 。
附:這是我以前的答案。我發現這一點,我才意識到 Jared 正在問什麼。這是正確的,但與他的問題無關。 🙂
我想你正在尋找的是 add_image_size():
add_image_size( $size_id, $width, $height, $crop );
例如:
add_image_size('headshot', 130, 150);
add_image_size('large-headshot', 260, 300);
通過設置這個 WordPress 將自動創建這些大小。你需要什麼?
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。