如何使用wordpress中的rest api生成不同大小的多个图像

时间:2018-05-16 06:16:13

标签: wordpress wordpress-rest-api

我希望在通过rest api传递图像网址时生成多个图像。 并序列化' _ wp_attachment_metadata' postmeta 表中的数据,如a:5:{s:5:"width";i:1920;s:6:"height";i:1200;s:4:"file";s:29:"2018/05/Haute-Panoramic-1.jpg";s:5:"sizes";a:8:{s:9:"thumbnail";a:4:{s:4:"file";s:29:"Haute-Panoramic-1-150x150.jpg";s:5:"width";i:150;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";}s:6:"medium";a:4:{s:4:"file";s:29:"Haute-Panoramic-1-300x188.jpg";s:5:"width";i:300;s:6:"height";i:188;s:9:"mime-type";s:10:"image/jpeg";}s:12:"medium_large";a:4:{s:4:"file";s:29:"Haute-Panoramic-1-768x480.jpg";s:5:"width";i:768;s:6:"height";i:480;s:9:"mime-type";s:10:"image/jpeg";}s:5:"large";a:4:{s:4:"file";s:30:"Haute-Panoramic-1-1024x640.jpg";s:5:"width";i:1024;s:6:"height";i:640;s:9:"mime-type";s:10:"image/jpeg";}s:6:"slider";a:4:{s:4:"file";s:30:"Haute-Panoramic-1-1440x550.jpg";s:5:"width";i:1440;s:6:"height";i:550;s:9:"mime-type";s:10:"image/jpeg";}s:25:"real_estate_lite_property";a:4:{s:4:"file";s:29:"Haute-Panoramic-1-400x300.jpg";s:5:"width";i:400;s:6:"height";i:300;s:9:"mime-type";s:10:"image/jpeg";}s:32:"real_estate_lite_property_slider";a:4:{s:4:"file";s:29:"Haute-Panoramic-1-800x600.jpg";s:5:"width";i:800;s:6:"height";i:600;s:9:"mime-type";s:10:"image/jpeg";}s:27:"real_estate_lite_page_thumb";a:4:{s:4:"file";s:29:"Haute-Panoramic-1-400x220.jpg";s:5:"width";i:400;s:6:"height";i:220;s:9:"mime-type";s:10:"image/jpeg";}}s:10:"image_meta";a:12:{s:8:"aperture";s:1:"0";s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";s:1:"0";s:9:"copyright";s:0:"";s:12:"focal_length";s:1:"0";s:3:"iso";s:1:"0";s:13:"shutter_speed";s:1:"0";s:5:"title";s:0:"";s:11:"orientation";s:1:"0";s:8:"keywords";a:0:{}}}

这是我的代码: -

 $fetured_array=["https://media.architecturaldigest.com/photos/585811cfdcb583e908275f46/4:3/w_384/buildings-with-trees-001.jpg","https://media.architecturaldigest.com/photos/585811cfdcb583e908275f46/4:3/w_384/buildings-with-trees-001.jpg"]
  $count_img=0;
  foreach($feature_img as $url)
  {
  //$url = $feature_img;
  $path = parse_url($url, PHP_URL_PATH);
  $filename = mt_rand().basename($path);

  $uploaddir = wp_upload_dir();
  $uploadfile = $uploaddir['path'] . '/' . $filename;

  $contents= file_get_contents($url);
  $savefile = fopen($uploadfile, 'w');
  chmod($uploadfile, 0777);
  fwrite($savefile, $contents);
  fclose($savefile);

  $wp_filetype = wp_check_filetype(basename($filename), null );

  $attachment = array(
      'ID'=>$page_id,
      'post_mime_type' => $wp_filetype['type'],
      'post_title' => $filename,
      'post_content' => '',
      'post_status' => 'inherit',
      'post_parent'=>$new_post_id,
  ); 



$attach_id = wp_insert_attachment( $attachment, $uploadfile );
          if($count_img==0){
             if ($attach_id) {
               set_post_thumbnail( $new_post_id, $attach_id );
             }
          }

$newwidth = '250'; 
 foreach ($attachment as $attached) {
    $id = $attach_id;
    $metadata = wp_get_attachment_metadata($id); 
    $metadata['width'] = $newwidth;
    wp_update_attachment_metadata($id,$metadata);
  }
      array_push($fetured_array,$attach_id);

        $count_img++;  
  } 
      $format_fetured_img=implode('|',$fetured_array);
      update_post_meta($new_post_id, 'real_estate_property_images', $format_fetured_img);
  

目前正在运行,但只将图片上传到上传/ 2018/5   具有图像原始大小的文件夹。我该如何上传图片   动态生成大小就像在媒体库中上传图像时一样   wordpress admin

请帮助我找到上述解决方案。

提前致谢

2 个答案:

答案 0 :(得分:0)

这是解决方案。

首先确保您在 $ fetured_array 之前附上了image.php文件

之后

require ( ABSPATH . 'wp-admin/includes/image.php' );添加thumbsize add_image_size( 'custom-size', 110, 100 );

现在$attach_id = wp_insert_attachment( $attachment, $uploadfile );之后添加以下两行

$attach_data = wp_generate_attachment_metadata( $attach_id, $uploadfile);
wp_update_attachment_metadata( $attach_id,  $attach_data );

现在您的代码将正常运行。此代码经过测试,工作正常。

答案 1 :(得分:0)

我找到了创建它的功能!!它将自动创建所有图像。

https://gist.github.com/m1r0/f22d5237ee93bcccb0d9

function crb_insert_attachment_from_url($url, $parent_post_id = null) {

if( !class_exists( 'WP_Http' ) )
    include_once( ABSPATH . WPINC . '/class-http.php' );

$http = new WP_Http();
$response = $http->request( $url );
if( $response['response']['code'] != 200 ) {
    return false;
}

$upload = wp_upload_bits( basename($url), null, $response['body'] );
if( !empty( $upload['error'] ) ) {
    return false;
}

$file_path = $upload['file'];
$file_name = basename( $file_path );
$file_type = wp_check_filetype( $file_name, null );
$attachment_title = sanitize_file_name( pathinfo( $file_name, PATHINFO_FILENAME ) );
$wp_upload_dir = wp_upload_dir();

$post_info = array(
    'guid'           => $wp_upload_dir['url'] . '/' . $file_name,
    'post_mime_type' => $file_type['type'],
    'post_title'     => $attachment_title,
    'post_content'   => '',
    'post_status'    => 'inherit',
);

// Create the attachment
$attach_id = wp_insert_attachment( $post_info, $file_path, $parent_post_id );

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

// Define attachment metadata
$attach_data = wp_generate_attachment_metadata( $attach_id, $file_path );

// Assign metadata to attachment
wp_update_attachment_metadata( $attach_id,  $attach_data );

return $attach_id;


}

并调用此功能,使用它。

$xxx = crb_insert_attachment_from_url('http://ronaldoguedes.com.br/img/hello_word_2.png', null);