Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在wordpress中使用RESTAPI生成不同大小的多幅图像_Wordpress_Wordpress Rest Api - Fatal编程技术网

如何在wordpress中使用RESTAPI生成不同大小的多幅图像

如何在wordpress中使用RESTAPI生成不同大小的多幅图像,wordpress,wordpress-rest-api,Wordpress,Wordpress Rest Api,我想在通过RESTAPI传递图像url时生成多个图像。 并将“wp\u attachment\u metadata”数据序列化到postemta表中,如下a:5:{s:5:“宽度”;i:1920;s:6:“高度”;i:1200;s:4:“文件”;s:29:“2018/05/Haute-panimal-1.jpg”;s:5:“大小”;a:8:{s:9:“缩略图”;a:4:{s:4:“文件”;s:29:“Haute-panimal-1-150x150.jpg”;s:5:“宽度”;i:150;s:15

我想在通过RESTAPI传递图像url时生成多个图像。 并将“wp\u attachment\u metadata”数据序列化到postemta表中,如下
a:5:{s:5:“宽度”;i:1920;s:6:“高度”;i:1200;s:4:“文件”;s:29:“2018/05/Haute-panimal-1.jpg”;s:5:“大小”;a:8:{s:9:“缩略图”;a:4:{s:4:“文件”;s:29:“Haute-panimal-1-150x150.jpg”;s:5:“宽度”;i:150;s:150;mime:10:s:10:10“image/jpeg”;}s:6:“中等”;a:4:{s:4:“文件”;s:29:“高级全景-1-300x188.jpg”;s:5:“宽度”;i:300;s:6:“高度”;i:188;s:9:“mime类型”;s:10:“图像/jpeg”}s:12:“中等大小”;a:4:{s:4:“文件”;s:29:“高级全景-1-768x480.jpg”;s:5:“宽度”;i:768;s:6:“高度”;i:480;s:9:“图像类型”;jpeg:10:“大型”;a:30:{上全景1-1024x640.jpg;s:5:“宽度”i:1024;s:6:“高度”i:640;s:9:“mime类型”;s:10:“图像/jpeg”}s:6:“滑块”a:4:{s:4:“文件”s:30:“上全景-1-0x550.jpg”;s:5:“宽度”i:1440;s:6:“高度”i:550;s:9:“mime类型”;s:10:“图像/jpeg”}s:25:“房产”a:4:“文件”上全景-1-0x550.jpg:“宽度”“i:400;s:6:”高度“;i:300;s:9:”mime类型“;s:10:”图像/jpeg”;}s:32:”不动产光碟属性滑块“;a:4:{s:4:”文件“;s:29:”上全景-1-800x600.jpg“;s:5:”宽度“;i:800;s:6:”高度“;i:600;s:9:”mime类型“;s:10:”图像/jpeg”}s:27:”不动产光碟页面“;a:4:{:s:4:”上全景-20-x200.jpg:”高度:“上全景-6:40010:10:“图像”元)a:9:)a:a:a:a:12:)a:a:a:a:12:{s:8:“光圈”s:1:“0”s:1:;s:s:s:6:::s:s:s:6:“信用”s:s:s:6:“信用”s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:6:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s:s}}

以下是我的代码:-

 $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);
目前正在工作,但仅将图像上传至uploads/2018/5 具有图像原始大小的文件夹。我如何上传图像 通过在媒体库中上载图像时动态生成大小 wordpress管理员

请帮我找到解决上述问题的方法

提前感谢

这是解决方案

首先,请确保在$fetured\u数组前面附加image.php文件,如下所示

require(ABSPATH.'wp admin/includes/image.php')之后添加拇指大小
添加图像大小('自定义大小',110100)

现在在
$attach\u id=wp\u insert\u attachment($attachment,$uploadfile)之后添加以下两行

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

现在你的代码可以工作了。此代码经过测试,工作正常

我找到了创建它的函数!!它将自动创建所有图像

要调用此函数,请使用它

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

感谢dipmala现在代码运行良好,但是_wp_附件_元数据我没有得到最后一个键值响应链接这是:10:“image_meta”;a:13:{s:8:“光圈”s:1:“0”s:6:“信用”s:0:“相机”s:0:“s:7:“说明”s:0:“s:17:“创建时间戳”s:1:“0”s:9:“版权”s:0:“s:12:“焦距”s:1:“0”s:3:“iso”s:1:“0”s:13:“快门速度”s:1:“0”s:5:“标题”s:0:“s:11:“方向”s:1:“0”s:8:“a:0:{s:14:“已调整大小的_图像”;a:2:{i:0;s:7:“870x420”;i:1;s:7:“250x130”;}}}}}以及未使用870x420生成的图像;以及“250x130”“使用_wp_attachment_元数据确定代码大小,从何处获得响应?”?因为根据上面的代码,我找不到任何你回电的回应。但您主要关心的是创建生成的拇指,我只添加了add_image_size('custom size',870,420);添加图像大小(“自定义大小”,250130);很抱歉,我在metapost tableAli中找到了元数据响应,因为您的问题答案已经给出,现在您正在谈论不同的事情,所以对于该问题,请询问其他问题或对此问题进行一些修改。
$xxx = crb_insert_attachment_from_url('http://ronaldoguedes.com.br/img/hello_word_2.png', null);