I have a field that will accept an image link coming from the external source. But of course, to set that image I have to save it in Drupal first. I'm wondering how do Drupal save the image from an external link. I use the following code.
$imageUri = 'https://api.statvoo.com/favicon/?url=google.com'; // this is a favicon
$file = File::create([
'uri' => $imageUri,
'status' => 1,
]);
$file->save();
$user_profile->field_profile_image[] = [
'target_id' => $file->id(),
'alt' => 'Alt text',
'title' => 'Title',
'width' => 200,
'height' => 200,
];
$user_profile->save();
I notice that the following values and the external image URL were added in the image field, but the image is not displayed on the page. Thanks in advance!
$file_content = file_get_contents($imageUri); $directory = 'public://2018-08/'; file_prepare_directory($directory, FILE_CREATE_DIRECTORY); $imageUri = file_save_data($file_content, $directory . basename($imageUri), FILE_EXISTS_REPLACE);
but unfortunately, its still not able to display the image, though I can access the photo in the same directory where it upload/save the image by default(on manual upload of image in editting node page). This is what I want to achieve similarly drupal.stackexchange.com/a/227182/70281.