미디어 모듈을 사용하여 프로그래밍 방식으로 외부 URL의 이미지를 추가하는 방법은 무엇입니까? 어떻게해야합니까? 일반적으로 새 노드를 만드는 동안 “미디어

프로그래밍 방식으로 외부 URL에서 이미지를 추가하고 모듈에 이미지의 로컬 사본을 저장하고 표시하려고합니다. 어떻게해야합니까? 일반적으로 새 노드를 만드는 동안 “미디어 선택”단추를 클릭하지만 코드를 통해 수행하려고합니다.



답변

비디오 자료를 사용하여 비슷한 작업을 시도하고 있기 때문에 귀하의 질문에 대한 부분 답변입니다.

노드를 컨텐츠 유형으로 작성하고 필요한 매체 유형을 저장하십시오 (미디어 코드를 통해 호출해야하는 관련 MIME / 유형 및 기능에 대해 살펴보십시오). 멀티미디어 자산 필드를 설정하고 필드 유형에서 미디어 파일 선택기를 사용해야합니다.

내가 문제를 겪고있는 비트는 브라우저를 생성 된 노드에 표시하는 것입니다. 현재 작동하고 있습니다.

최신 정보

조금 더있어. 미디어 API를 사용하여 미디어 파일을 저장 한 후 file_usage_add ()를 사용하여 파일 ID를 노드 ID와 연결하십시오 . 미디어 자산 필드를 만들 때 추가 된 필드에 파일을 연결해야 할 수도 있습니다.


답변

php.ini가 allow_url_fopen을 허용하는지 확인하십시오. 그런 다음 모듈에서 다음과 같은 것을 사용할 수 있습니다.

$image = file_get_contents('http://drupal.org/files/issues/druplicon_2.png'); // string
$file = file_save_data($image, 'public://druplicon.png',FILE_EXISTS_REPLACE);

PHP의 file_get_contents () 함수 사용

http://www.php.net/manual/en/function.file-get-contents.php

Drupal API의 file_save_data ()를 사용하십시오.

http://api.drupal.org/api/drupal/includes–file.inc/function/file_save_data/7

그런 다음을 사용하여 호출하고 노드 등에 저장할 수 있어야합니다.

$node = new stdClass;
$node->type = 'node_type';
node_object_prepare($node);
$node->field_image[LANGUAGE_NONE]['0']['fid'] = $file->fid;
node_save($node);

편집하다:

주석에서 지적했듯이 system_retrieve_file 함수를 사용할 수 있습니다 : https://api.drupal.org/api/drupal/modules!system!system.module/function/system_retrieve_file/7 참조


답변

여기 내 작업 예가 있습니다.

$remoteDocPath = 'http://drupal.org/files/issues/druplicon_2.png';
$doc = system_retrieve_file($remoteDocPath, NULL, FALSE, FILE_EXISTS_REPLACE);
$file = drupal_add_existing_file($doc);

$node = new stdClass;
$node->type = 'node_type';
node_object_prepare($node);
$node->field_image[LANGUAGE_NONE]['0']['fid'] = $file->fid;
node_save($node);

function drupal_add_existing_file($file_drupal_path, $uid = 1, $status = FILE_STATUS_PERMANENT) {
  $files = file_load_multiple(array(), array('uri' => $file_drupal_path));
  $file = reset($files);

  if (!$file) {
    $file = (object) array(
        'filename' => basename($file_drupal_path),
        'filepath' => $file_drupal_path,
        'filemime' => file_get_mimetype($file_drupal_path),
        'filesize' => filesize($file_drupal_path),
        'uid' => $uid,
        'status' => $status,
        'timestamp' => time(),
        'uri' => $file_drupal_path,
    );
    drupal_write_record('file_managed', $file);
  }
  return $file;
}


답변

이것은 직접적인 대답은 아니지만 일반적으로 이미지에 대해 수행하는 Filefield Sources 모듈 에 대해 알고 있는지 확인하십시오 . 그것은 당신의 필요를 스스로 충족시킬 수 있습니다. 미디어에 유용한 지 모르겠습니다.


답변

@tecjam 답변 외에도 file_get_contents () 대신 drupal_http_request () 를 사용해야 하므로 프로세스를 더 많이 제어 할 수 있습니다. 그러나 전체적으로이 방법은 예상대로 작동합니다.


답변

// This is a PHP function to get a string representation of the image file.
$image = file_get_contents($path);

// A stream wrapper path where you want this image to reside on your file system including the desired filename.
$destination = 'public://path/to/store/this/image/name.jpg';

$file = file_save_data($image, $destination, FILE_EXISTS_REPLACE);

if (is_object($file)) { // if you get back a Drupal $file object, everything went as expected so make the status permenant
  $file->status = 1;
  $file = file_save($file);
}

return $file;


답변