Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $file_type = explode('.', $image)[1];
- $data = file_get_contents($image);
- $path = 'public://images/';
- if (file_prepare_directory($path, FILE_CREATE_DIRECTORY)) {
- $file = file_save_data($data, $path . md5($image) . '.' . $file_type, FILE_EXISTS_REPLACE);
- $node = Node::create(array(
- 'type' => 'image',
- 'title' => $image,
- 'field_image' => array(
- 'target_id' => $file->id(),
- ),
- ));
- $node->save();
- }
- $original_image = 'public://images/image.jpg';
- // Load the image style configuration entity.
- use DrupalimageEntityImageStyle;
- $style = ImageStyle::load('max_2600x2600');
- $uri = $style->buildUri($original_image);
- $url = $style->buildUrl($original_image);
- use DrupalfileEntityFile;
- use DrupalimageEntityImageStyle;
- use DrupalnodeEntityNode;
- $fileName = 'example.jpg';
- $destination = "public://images/" . $fileName;
- $temporaryFile = 'temporary://' . $fileName;
- file_save_data($photoData, $temporaryFile, FILE_EXISTS_REPLACE);
- $style = ImageStyle::load('thumbnail');
- $style->createDerivative($temporaryFile, $destination);
- $file = File::create([
- 'uid' => 1,
- 'filename' => $fileName,
- 'uri' => $destination,
- 'status' => 1,
- ]);
- $file->save();
- if ($file) {
- $node->get('field_photos')->appendItem([
- 'target_id' => $file->id(),
- 'alt' => 'Alt text goes here',
- ]);
- }
- ...
- $node->save();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement