When uploading images to D8 programmatically the setting for Maximum image resolution will be ignored and images will not be scaled down. Some of the images i have to upload are even larger then 10MB.

I am currently using this code.

$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(),


I've tried to work with ImageStyle::load('max_2600x2600'), but that will only create a style in styles directory.

It would be possible to use a custom shell command but i would love to use the Drupal 8 way. How to make sure original images will get resized?

2 Answers 2


You can Use Image style before save the node.

$original_image = 'public://images/image.jpg';

// Load the image style configuration entity.
use Drupal\image\Entity\ImageStyle;
$style = ImageStyle::load('max_2600x2600');

$uri = $style->buildUri($original_image);
$url = $style->buildUrl($original_image);

Here some links may helps you

Drupal 8 - Create an Image and ImageStyle Programmatically

Drupal 8 images with image style

Flexible image style system

  • My image does not come from public:// but from directory outside of Drupal provided for FTP uploads. You pointed me into the right direction and i've finally managed to resize images while importing them.
    – rugar
    Commented Aug 31, 2016 at 13:02
  • now your issue has been fixed right?
    Commented Aug 31, 2016 at 13:06
  • Yes thanks allot! But the code in your example is not the code that helped me, since it will not create a new image outside of public:// and i want to create a file with fid to attach to the node. I'm using $style->createDerivative($original_image, 'temporary://filename.jpg'); to create a resized image in temporary directory and attach this file to the node and have it imported correctly.
    – rugar
    Commented Aug 31, 2016 at 13:23
  • Nice to here you. Actually this code is how to resize images programmatically using image style. Great job :)
    Commented Aug 31, 2016 at 13:28

I ran into the same thing. This code ended up working for me

      use Drupal\file\Entity\File;
      use Drupal\image\Entity\ImageStyle;
      use Drupal\node\Entity\Node;

      $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,

      if ($file) {
              'target_id' => $file->id(),
              'alt' => 'Alt text goes here',



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.