WordPress的:使用PHP上传图像 - 无法创建不同的图像大小

问题描述:

我想模拟WordPress的默认上传过程。 何去何从图像:WordPress的:使用PHP上传图像 - 无法创建不同的图像大小

<input type="file" name="imgsupload1[]" id="imgsupload1[]" multiple=""> 

的代码应该上传:未创建

$post_imgs = $_FILES['imgsupload1'];  
foreach ($post_imgs['name'] as $key => $value) { 
     if ($post_imgs['name'][$key]) { 
     $file = array(
      'name'  => $post_imgs['name'][$key], 
      'type'  => $post_imgs['type'][$key], 
      'tmp_name' => $post_imgs['tmp_name'][$key], 
      'error' => $post_imgs['error'][$key], 
      'size'  => $post_imgs['size'][$key] 
     ); 
     $img_caption = $img_captions[urlencode(basename($file['name']))]; 
     $new_file = wp_handle_upload($file, array('test_form' => false)); 

    $filename = $new_file['url']; 
    $filetype = wp_check_filetype(basename($filename), null); 
    $wp_upload_dir = wp_upload_dir(); 

    // Prepare an array of post data for the attachment. 
    $attachment = array(
     'guid'   => $wp_upload_dir['url'].'/'.basename($filename), 
     'post_mime_type' => $filetype['type'], 
     'post_title'  => preg_replace('/\.[^.]+$/', '', basename($filename)), 
     'post_content' => '', 
     'post_status' => 'inherit', 
     'post_excerpt' => $img_caption 
    ); 

    require_once(ABSPATH.'wp-admin/includes/image.php'); 
    $img_id = wp_insert_attachment($attachment, $filename, $post_id); 
    $img_data = wp_generate_attachment_metadata($img_id, $wp_upload_dir['basedir'].'/'.basename($filename)); 
    wp_update_attachment_metadata($img_id, $img_data); 

     } // IF IMAGE[KEY] EXISTS 
    } // FOREACH UPLOADED IMAGE 

默认定义图像尺寸。 看来问题是wp_generate_attachment_metadata

的问题是在wp_generate_attachment_metadata,用于传递正确的绝对路径,你需要使用wp_upload_dir阵“路径”元素:

$img_data = wp_generate_attachment_metadata($img_id, $wp_upload_dir['path'].'/'.basename($filename)); 

这是从什么不同以Codex编写。希望它能帮助别人。