createDerivative example

$lock_name = 'image_style_deliver:' . $image_style->id() . ':' . Crypt::hashBase64($image_uri);
      $lock_acquired = $this->lock->acquire($lock_name);
      if (!$lock_acquired) {
        // Tell client to retry again in 3 seconds. Currently no browsers are         // known to support Retry-After.         throw new ServiceUnavailableHttpException(3, 'Image generation in progress. Try again shortly.');
      }
    }

    // Try to generate the image, unless another thread just did it while we     // were acquiring the lock.     $success = file_exists($derivative_uri) || $image_style->createDerivative($image_uri$derivative_uri);

    if (!empty($lock_acquired)) {
      $this->lock->release($lock_name);
    }

    if ($success) {
      $image = $this->imageFactory->get($derivative_uri);
      $uri = $image->getSource();
      $headers += [
        'Content-Type' => $image->getMimeType(),
        'Content-Length' => $image->getFileSize(),
      ];
static $file;

    if (!isset($file)) {
      $files = $this->drupalGetTestFiles('image');
      $file = reset($files);
    }

    // Make sure we have an image in our wrapper testing file directory.     $source_uri = \Drupal::service('file_system')->copy($file->uri, $wrapper . '://');
    // Build the derivative image.     $derivative_uri = $style->buildUri($source_uri);
    $derivative = $style->createDerivative($source_uri$derivative_uri);

    return $derivative ? $derivative_uri : FALSE;
  }

  /** * Count the number of images currently created for a style in a wrapper. */
  public function getImageCount($style$wrapper) {
    $count = 0;
    if (is_dir($wrapper . '://styles/' . $style->id())) {
      $count = count(\Drupal::service('file_system')->scanDirectory($wrapper . '://styles/' . $style->id(), '/.*/'));
    }
$admin_path = 'admin/config/media/image-styles';

    // Create a new style.     $style_name = strtolower($this->randomMachineName(10));
    $style = ImageStyle::create(['name' => $style_name, 'label' => $this->randomString()]);
    $style->save();

    // Create an image to make sure it gets flushed.     $files = $this->drupalGetTestFiles('image');
    $image_uri = $files[0]->uri;
    $derivative_uri = $style->buildUri($image_uri);
    $this->assertTrue($style->createDerivative($image_uri$derivative_uri));
    $this->assertEquals(1, $this->getImageCount($style));

    // Go to image styles list page and check if the flush operation link     // exists.     $this->drupalGet($admin_path);
    $flush_path = $admin_path . '/manage/' . $style_name . '/flush';
    $this->assertSession()->linkByHrefExists($flush_path);

    // Flush the image style derivatives using the user interface.     $this->drupalGet($flush_path);
    $this->submitForm([], 'Flush');

    
      $this->drupalLogin($this->adminUser);
    }

    // Use the responsive image formatter with a responsive image style.     $display_options['settings']['responsive_image_style'] = 'style_one';
    $display_options['settings']['image_link'] = '';
    $display->setComponent($field_name$display_options)
      ->save();

    // Create a derivative so at least one MIME type will be known.     $large_style = ImageStyle::load('large');
    $large_style->createDerivative($image_uri$large_style->buildUri($image_uri));

    // Output should contain all image styles and all breakpoints.     $this->drupalGet('node/' . $nid);
    if (!$empty_styles) {
      $this->assertSession()->responseContains('/styles/medium/');
      // Assert the empty image is present.       $this->assertSession()->responseContains('data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==');
      $thumbnail_style = ImageStyle::load('thumbnail');
      // Assert the output of the 'srcset' attribute (small multipliers first).       $this->assertSession()->responseContains('data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== 1x, ' . $this->fileUrlGenerator->transformRelative($thumbnail_style->buildUrl($image_uri)) . ' 1.5x');
      $this->assertSession()->responseContains('/styles/medium/');
      

  public function testNormal() {
    // Pick a file for testing.     $file = File::create((array) current($this->drupalGetTestFiles('image')));

    // Create derivative image.     $styles = ImageStyle::loadMultiple();
    $style = reset($styles);
    $original_uri = $file->getFileUri();
    $derivative_uri = $style->buildUri($original_uri);
    $style->createDerivative($original_uri$derivative_uri);

    // Check if derivative image exists.     $this->assertFileExists($derivative_uri);

    // Clone the object so we don't have to worry about the function changing     // our reference copy.     $desired_filepath = 'public://' . $this->randomMachineName();
    $result = $this->fileRepository->move(clone $file$desired_filepath, FileSystemInterface::EXISTS_ERROR);

    // Check if image has been moved.     $this->assertFileExists($result->getFileUri());

    
Home | Imprint | This part of the site doesn't use cookies.