Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. $file_type = explode('.', $image)[1];
  2. $data = file_get_contents($image);
  3. $path = 'public://images/';
  4.  
  5. if (file_prepare_directory($path, FILE_CREATE_DIRECTORY)) {
  6.  
  7. $file = file_save_data($data, $path . md5($image) . '.' . $file_type, FILE_EXISTS_REPLACE);
  8.  
  9. $node = Node::create(array(
  10. 'type' => 'image',
  11. 'title' => $image,
  12. 'field_image' => array(
  13. 'target_id' => $file->id(),
  14. ),
  15. ));
  16. $node->save();
  17.  
  18. }
  19.  
  20. $original_image = 'public://images/image.jpg';
  21.  
  22. // Load the image style configuration entity.
  23. use DrupalimageEntityImageStyle;
  24. $style = ImageStyle::load('max_2600x2600');
  25.  
  26. $uri = $style->buildUri($original_image);
  27. $url = $style->buildUrl($original_image);
  28.  
  29. use DrupalfileEntityFile;
  30. use DrupalimageEntityImageStyle;
  31. use DrupalnodeEntityNode;
  32.  
  33. $fileName = 'example.jpg';
  34. $destination = "public://images/" . $fileName;
  35. $temporaryFile = 'temporary://' . $fileName;
  36. file_save_data($photoData, $temporaryFile, FILE_EXISTS_REPLACE);
  37.  
  38. $style = ImageStyle::load('thumbnail');
  39. $style->createDerivative($temporaryFile, $destination);
  40.  
  41. $file = File::create([
  42. 'uid' => 1,
  43. 'filename' => $fileName,
  44. 'uri' => $destination,
  45. 'status' => 1,
  46. ]);
  47. $file->save();
  48.  
  49. if ($file) {
  50. $node->get('field_photos')->appendItem([
  51. 'target_id' => $file->id(),
  52. 'alt' => 'Alt text goes here',
  53. ]);
  54. }
  55.  
  56. ...
  57.  
  58. $node->save();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement