尝试以编程方式生成WordPress缩略图

时间:2011-12-22 23:13:02

标签: php image wordpress

我正在尝试以编程方式添加一堆帖子,然后添加他们的服务员图片。我有一个包含所有图像文件名的数组,我已经能够将它们添加到数据库中,但我似乎无法创建正确的缩略图。

这些帖子来自位于wp-content / uploads / dirname /的csv。他们的文件名中有数字,对应于CSV中的ID,这就是我知道哪些图片需要添加到帖子ID的方式。

我已经获得了wp_insert_attachment()部分来处理他们自己的小目录中的图像,但我无法生成缩略图。我安装了重新生成缩略图插件,它能够生成它们,但我似乎无法以编程方式实现它。

我认为这可能是因为wp_generate_attachment需要将照片放入/ uploads / 2011/12 /(例如),所以我开始沿着移动图像的路径然后尝试添加它们。这是有道理的,因为我有点想制作副本而不是将5或6种不同的媒体大小添加到我的wp-content / uploads / dirname / dir中。

无论如何,它不起作用。通过PHP的副本移动图像不起作用,并且缩略图不会生成。

foreach ($frazerfiles[$stock_num] as $stock_img){
  require_once(ABSPATH . 'wp-admin/includes/image.php');
  echo "... ...Trying to add attachment metadata...";

  // copy the file to the upload dir
  $uploads = wp_upload_dir();
  $file_to_move = ABSPATH."wp-content/uploads/".$stock_img;
  if (copy($file_to_move, $uploads['path']."/")) {
    echo "Moved $file_to_move to ".$uploads['path']."/";
    $my_moved_file = $uploads['path']."/".$stock_img;

    // I think this is failing because the images aren't in the upload dir.
    $attachment = array(
       'post_mime_type' => $wp_filetype['type'],
       'post_title' => preg_replace('/\.[^.]+$/', '', basename($stock_img)),
       'post_content' => '',
       'post_status' => 'inherit'
    );
    if ($attach_id = wp_insert_attachment( $attachment, $my_moved_file, $newpostid )) {
      if ($attach_data = wp_generate_attachment_metadata( $attach_id, $my_moved_file)) {
        echo "...success for $my_moved_file: ID:$attach_id<br />\n";
      } else {
        echo "...FAILED for $my_moved_file ID:$attach_id<br />\n";
        print_r($attach_data);
      }
    } else { // inserting attachment failed
      echo "Insert attachment failed for $my_moved_file to $newpostid<br />\n"; 
    }
  } else {
    echo "Failed moving $file_to_move to ".$uploads['path']."/";
  }


}// images foreach

1 个答案:

答案 0 :(得分:3)

在使用WordPress核心文件进行交叉检查后,在wp_update_attachment_metadata之后,唯一似乎缺失的是wp_generate_attachment_metadata

尝试添加

wp_update_attachment_metadata($attach_id, $attach_data);

echo "...success for $my_moved_file: ID:$attach_id<br />\n";

所以if块看起来像

 if ($attach_data = wp_generate_attachment_metadata( $attach_id, $my_moved_file)) {
    echo "...success for $my_moved_file: ID:$attach_id<br />\n";
    wp_update_attachment_metadata($attach_id, $attach_data);
  } else {
    echo "...FAILED for $my_moved_file ID:$attach_id<br />\n";
    print_r($attach_data);
  }

建议:(与问题无关)

移动线

require_once(ABSPATH . 'wp-admin/includes/image.php');

在for循环之外(上方)。

更新1: 添加建议以修复副本问题。 您错过了“检查文件类型”行。 (如果目标文件($my_moved_file)已存在,则PHP复制功能将失败)

更改此代码

// copy the file to the upload dir
$uploads = wp_upload_dir();
$file_to_move = ABSPATH."wp-content/uploads/".$stock_img;
if (copy($file_to_move, $uploads['path']."/")) {
echo "Moved $file_to_move to ".$uploads['path']."/";
$my_moved_file = $uploads['path']."/".$stock_img;

// I think this is failing because the images aren't in the upload dir.
$attachment = array(
   'post_mime_type' => $wp_filetype['type'],
   'post_title' => preg_replace('/\.[^.]+$/', '', basename($stock_img)),
   'post_content' => '',
   'post_status' => 'inherit'
);

// copy the file to the upload dir
$uploads = wp_upload_dir();
$file_to_move = ABSPATH."wp-content/uploads/".$stock_img;
$my_moved_file = $uploads['path']."/".$stock_img;
if (copy($file_to_move, $my_moved_file)) {
    echo "Moved $file_to_move to ".$my_moved_file;

    // Check the file type
    $wp_filetype = wp_check_filetype(basename($my_moved_file), null );

    $attachment = array(
       'post_mime_type' => $wp_filetype['type'],
       'post_title' => preg_replace('/\.[^.]+$/', '', basename($my_moved_file)),
       'post_content' => '',
       'post_status' => 'inherit'
    );