在本地保存URL中的图像

时间:2016-06-23 17:29:54

标签: php wordpress function

我目前有一个插件可以使用RSS Feed将我在博客上创建的帖子拉到WordPress。问题是它的链接我的图像。我希望WordPress在本地保存图像,然后显示它们。显示我没有问题的图像。这是我能找到的最好的,并根据自己的喜好调整它,但它只会崩溃我的网站:

$data = my_get_remote_content('get_post_meta( get_the_ID(), 'enclosure', true )');
savePhoto(my_get_remote_content($data['body']), $post->ID); 

function my_get_remote_content($url) {
  $response = wp_remote_get($url, 
    array(
      'headers' => array(
        'user-agent' => 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2)'
      )
    )
  );
  if( is_wp_error( $response ) ) {
    throw new Exception('Error fetching remote content');
  } else {
    $data = wp_remote_retrieve_body($response);
    return $data;
  }  
}

function savePhoto($fileContents, $isbn) {
  if (DIRECTORY_SEPARATOR=='/'){
    $absolute_path = dirname(__FILE__).'/'; 
  } else { 
    $absolute_path = str_replace('\\', '/', dirname(__FILE__)).'/'; 
  }
  $newImg = imagecreatefromstring($fileContents);
  return imagejpeg($newImg, $absolute_path ."{$isbn}.jpg",100);
}

get_post_meta( get_the_ID(), 'enclosure', true )从我的帖子元数据(自定义字段)中获取实际链接,但我似乎无法使其正常工作,任何人都可以帮助我吗? 感谢

0 个答案:

没有答案