addImageEffect example



    // Add an image effect.     $effect = [
      'id' => 'image_scale_and_crop',
      'data' => [
        'width' => 120,
        'height' => 121,
      ],
      'weight' => 0,
    ];
    $this->effectUuid = $camelids->addImageEffect($effect);

    $camelids->save();

    return $camelids;
  }

  /** * {@inheritdoc} */
  protected function getExpectedDocument() {
    $self_url = Url::fromUri('base:/jsonapi/image_style/image_style/' . $this->entity->uuid())->setAbsolute()->toString(TRUE)->getGeneratedUrl();
    

        ['query' => ['weight' => $form_state->getValue('weight')]]
      );
    }
    // If there's no form, immediately add the image effect.     else {
      $effect = [
        'id' => $effect['id'],
        'data' => [],
        'weight' => $form_state->getValue('weight'),
      ];
      $effect_id = $this->entity->addImageEffect($effect);
      $this->entity->save();
      if (!empty($effect_id)) {
        $this->messenger()->addStatus($this->t('The image effect was successfully applied.'));
      }
    }
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    

  protected static $modules = ['system', 'image', 'image_module_test'];

  /** * Tests importing image styles. */
  public function testImport() {
    $style = ImageStyle::create([
      'name' => 'test',
    ]);

    $style->addImageEffect(['id' => 'image_module_test_null']);
    $style->addImageEffect(['id' => 'image_module_test_null']);
    $style->save();

    $this->assertCount(2, $style->getEffects());

    $uuid = \Drupal::service('uuid')->generate();
    $style->set('effects', [
      $uuid => [
        'id' => 'image_module_test_null',
      ],
    ]);
    
/** * Tests that files stored in the root folder are converted properly. */
  public function testConvertFileInRoot() {
    // Create the test image style with a Convert effect.     $image_style = ImageStyle::create([
      'name' => 'image_effect_test',
      'label' => 'Image Effect Test',
    ]);
    $this->assertEquals(SAVED_NEW, $image_style->save());
    $image_style->addImageEffect([
      'id' => 'image_convert',
      'data' => [
        'extension' => 'jpeg',
      ],
    ]);
    $this->assertEquals(SAVED_UPDATED, $image_style->save());

    // Create a copy of a test image file in root.     $test_uri = 'public://image-test-do.png';
    \Drupal::service('file_system')->copy('core/tests/fixtures/files/image-test.png', $test_uri, FileSystemInterface::EXISTS_REPLACE);
    $this->assertFileExists($test_uri);

    
$effects = $row->getDestinationProperty('effects');
      $row->setDestinationProperty('effects', []);
    }

    /** @var \Drupal\image\Entity\ImageStyle $style */
    $style = $this->getEntity($row$old_destination_id_values);

    // Iterate the effects array so each effect plugin can be initialized.     // Catch any missing plugin exceptions.     foreach ($effects as $effect) {
      try {
        $style->addImageEffect($effect);
      }
      catch (PluginNotFoundException $e) {
        throw new MigrateException($e->getMessage(), 0, $e);
      }
    }

    $style->save();

    return [$style->id()];
  }

}
// Add an image effect.     $effect = [
      'id' => 'image_scale_and_crop',
      'data' => [
        'anchor' => 'center-center',
        'width' => 120,
        'height' => 121,
      ],
      'weight' => 0,
    ];
    $this->effectUuid = $camelids->addImageEffect($effect);

    $camelids->save();

    return $camelids;
  }

  /** * {@inheritdoc} */
  protected function getExpectedNormalizedEntity() {
    return [
      
$image_uri = File::load($node->{$field_name}->target_id)->getFileUri();
    $this->assertSession()->responseMatches('/srcset="' . preg_quote($this->fileUrlGenerator->transformRelative($thumbnail_style->buildUrl($image_uri)), '/') . ' 1x".+?media="\(min-width: 0px\)"/');
  }

  /** * Tests responsive image formatter on node display with one and two sources. */
  public function testResponsiveImageFieldFormattersMultipleSources() {
    // Setup known image style sizes so the test can assert on known sizes.     $large_style = ImageStyle::load('large');
    assert($large_style instanceof ImageStyleInterface);
    $large_style->addImageEffect([
      'id' => 'image_resize',
      'data' => [
        'width' => '480',
        'height' => '480',
      ],
    ]);
    $large_style->save();
    $medium_style = ImageStyle::load('medium');
    assert($medium_style instanceof ImageStyleInterface);
    $medium_style->addImageEffect([
      'id' => 'image_resize',
      
    $this->assertSame($effects$cached_effects);
  }

  /** * Tests that validation errors are passed from the plugin to the parent form. */
  public function testEffectFormValidationErrors() {
    $form_builder = $this->container->get('form_builder');

    /** @var \Drupal\image\ImageStyleInterface $image_style */
    $image_style = ImageStyle::create(['name' => 'foo']);
    $effect_id = $image_style->addImageEffect(['id' => 'image_scale']);
    $image_style->save();

    $form = new ImageEffectEditForm();
    $form_state = (new FormState())->setValues([
      'data' => ['width' => '', 'height' => ''],
    ]);
    $form_builder->submitForm($form$form_state$image_style$effect_id);

    $errors = $form_state->getErrors();
    $this->assertCount(1, $errors);
    $error = reset($errors);
    
// Scale an image that is wider than it is high.     $effect = [
      'id' => 'image_scale',
      'data' => [
        'width' => 120,
        'height' => 90,
        'upscale' => TRUE,
      ],
      'weight' => 0,
    ];

    $style->addImageEffect($effect);
    $style->save();
    $this->assertEquals('<img src="' . $url . '" width="120" height="60" alt="" loading="lazy" />', $this->getImageTag($variables));
    $this->assertFileDoesNotExist($generated_uri);
    $this->drupalGet($this->getAbsoluteUrl($url));
    $this->assertSession()->statusCodeEquals(200);
    $this->assertFileExists($generated_uri);
    $image_file = $image_factory->get($generated_uri);
    $this->assertEquals(120, $image_file->getWidth());
    $this->assertEquals(60, $image_file->getHeight());

    // Rotate 90 degrees anticlockwise.

  public function submitForm(array &$form, FormStateInterface $form_state) {
    $form_state->cleanValues();

    // The image effect configuration is stored in the 'data' key in the form,     // pass that through for submission.     $this->imageEffect->submitConfigurationForm($form['data'], SubformState::createForSubform($form['data']$form$form_state));

    $this->imageEffect->setWeight($form_state->getValue('weight'));
    if (!$this->imageEffect->getUuid()) {
      $this->imageStyle->addImageEffect($this->imageEffect->getConfiguration());
    }
    $this->imageStyle->save();

    $this->messenger()->addStatus($this->t('The image effect was successfully applied.'));
    $form_state->setRedirectUrl($this->imageStyle->toUrl('edit-form'));
  }

  /** * Converts an image effect ID into an object. * * @param string $image_effect * The image effect ID. * * @return \Drupal\image\ImageEffectInterface * The image effect object. */
Home | Imprint | This part of the site doesn't use cookies.