本文整理汇总了PHP中Drupal\image\Entity\ImageStyle类的典型用法代码示例。如果您正苦于以下问题:PHP ImageStyle类的具体用法?PHP ImageStyle怎么用?PHP ImageStyle使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ImageStyle类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: viewElements
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode)
{
$elements = [];
$thumb_image_style = $this->getSetting('thumbnail_image_style');
$popup_image_style = $this->getSetting('popup_image_style');
$gallery_type = $this->getSetting('gallery_type');
$files = $this->getEntitiesToView($items, $langcode);
foreach ($files as $delta => $file) {
$image_uri = $file->getFileUri();
$popup_image_path = !empty($popup_image_style) ? ImageStyle::load($popup_image_style)->buildUrl($image_uri) : $image_uri;
// Depending on the outcome of https://www.drupal.org/node/2622586,
// Either a class will need to be added to the $url object,
// Or a custom theme function might be needed to do so.
// For the time being, 'a' is used as the delegate in magnific-popup.js.
$url = Url::fromUri(file_create_url($popup_image_path));
$item = $file->_referringItem;
$item_attributes = $file->_attributes;
unset($file->_attributes);
$item_attributes['class'][] = 'mfp-thumbnail';
if ($gallery_type === 'first_item' && $delta > 0) {
$elements[$delta] = ['#theme' => 'image_formatter', '#url' => $url, '#attached' => ['library' => ['magnific_popup/magnific_popup']]];
} else {
$elements[$delta] = ['#theme' => 'image_formatter', '#item' => $item, '#item_attributes' => $item_attributes, '#image_style' => $thumb_image_style, '#url' => $url, '#attached' => ['library' => ['magnific_popup/magnific_popup']]];
}
}
return $elements;
}
示例2: transformDimensions
/**
* {@inheritdoc}
*/
public function transformDimensions(array &$dimensions, $uri)
{
if (!isset($dimensions['width']) || !isset($dimensions['height'])) {
// We cannot know which preset would be executed and thus cannot know the
// resulting dimensions, unless both styles return the same dimensions:
$landscape_dimensions = $portrait_dimensions = $dimensions;
/* @var ImageStyle $landscape_style */
$landscape_style = ImageStyle::load($this->configuration['landscape']);
$landscape_style->transformDimensions($landscape_dimensions, $uri);
/* @var ImageStyle $portrait_style */
$portrait_style = ImageStyle::load($this->configuration['portrait']);
$portrait_style->transformDimensions($portrait_dimensions, $uri);
if ($landscape_dimensions == $portrait_dimensions) {
$dimensions = $landscape_dimensions;
} else {
$dimensions['width'] = $dimensions['height'] = NULL;
}
} else {
$ratio_adjustment = isset($this->configuration['ratio_adjustment']) ? floatval($this->configuration['ratio_adjustment']) : 1;
$aspect = $dimensions['width'] / $dimensions['height'];
$style_name = $aspect * $ratio_adjustment > 1 ? $this->configuration['landscape'] : $this->configuration['portrait'];
/* @var ImageStyle $style */
$style = ImageStyle::load($style_name);
$style->transformDimensions($dimensions, $uri);
}
}
示例3: doTestAutoOrientOperations
/**
* Auto Orientation operations test.
*/
public function doTestAutoOrientOperations()
{
$image_factory = $this->container->get('image.factory');
$test_data = [['test_file' => drupal_get_path('module', 'image_effects') . '/misc/portrait-painting.jpg', 'original_width' => 640, 'original_height' => 480, 'derivative_width' => 200, 'derivative_height' => 267], ['test_file' => drupal_get_path('module', 'simpletest') . '/files/image-test.jpg', 'original_width' => 40, 'original_height' => 20, 'derivative_width' => 200, 'derivative_height' => 100], ['test_file' => drupal_get_path('module', 'simpletest') . '/files/image-1.png', 'original_width' => 360, 'original_height' => 240, 'derivative_width' => 200, 'derivative_height' => 133]];
foreach ($test_data as $data) {
// Get expected URIs.
$original_uri = file_unmanaged_copy($data['test_file'], 'public://', FILE_EXISTS_RENAME);
$generated_uri = 'public://styles/image_effects_test/public/' . \Drupal::service('file_system')->basename($original_uri);
// Test source image dimensions.
$image = $image_factory->get($original_uri);
$this->assertEqual($data['original_width'], $image->getWidth());
$this->assertEqual($data['original_height'], $image->getHeight());
// Load Image Style and get expected derivative URL.
$image_style = ImageStyle::load('image_effects_test');
$url = file_url_transform_relative($image_style->buildUrl($original_uri));
// Check that ::transformDimensions returns expected dimensions.
$variables = array('#theme' => 'image_style', '#style_name' => 'image_effects_test', '#uri' => $original_uri, '#width' => $image->getWidth(), '#height' => $image->getHeight());
$this->assertEqual('<img src="' . $url . '" width="' . $data['derivative_width'] . '" height="' . $data['derivative_height'] . '" alt="" class="image-style-image-effects-test" />', $this->getImageTag($variables));
// Check that ::applyEffect generates image with expected dimensions.
$image_style->createDerivative($original_uri, $image_style->buildUri($original_uri));
$image = $image_factory->get($generated_uri);
$this->assertEqual($data['derivative_width'], $image->getWidth());
$this->assertEqual($data['derivative_height'], $image->getHeight());
}
}
示例4: viewElements
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode)
{
$elements = array();
$files = $this->getEntitiesToView($items, $langcode);
// Early opt-out if the field is empty.
if (empty($files)) {
return $elements;
}
$image_style_setting = $this->getSetting('image_style');
// Determine if Image style is required.
$image_style = NULL;
if (!empty($image_style_setting)) {
$image_style = entity_load('image_style', $image_style_setting);
}
foreach ($files as $delta => $file) {
$image_uri = $file->getFileUri();
// Get image style URL
if ($image_style) {
$image_uri = ImageStyle::load($image_style->getName())->buildUrl($image_uri);
} else {
// Get absolute path for original image
$image_uri = $file->url();
}
$elements[$delta] = array('#markup' => $image_uri);
}
return $elements;
}
示例5: doTestColorshiftOperations
/**
* Color Shift operations test.
*/
public function doTestColorshiftOperations()
{
$image_factory = $this->container->get('image.factory');
// Test on the PNG test image.
$test_file = drupal_get_path('module', 'simpletest') . '/files/image-test.png';
$original_uri = file_unmanaged_copy($test_file, 'public://', FILE_EXISTS_RENAME);
$generated_uri = 'public://styles/image_effects_test/public/' . \Drupal::service('file_system')->basename($original_uri);
// Test data.
$test_data = ['#FF0000' => [$this->red, $this->yellow, $this->transparent, $this->fuchsia], '#00FF00' => [$this->yellow, $this->green, $this->transparent, $this->cyan], '#0000FF' => [$this->fuchsia, $this->cyan, $this->transparent, $this->blue], '#929BEF' => [[255, 155, 239, 0], [146, 255, 239, 0], $this->transparent, [146, 155, 255, 0]]];
foreach ($test_data as $key => $colors) {
// Add Color Shift effect to the test image style.
$effect = ['id' => 'image_effects_color_shift', 'data' => ['RGB][hex' => $key]];
$uuid = $this->addEffectToTestStyle($effect);
// Load Image Style.
$image_style = ImageStyle::load('image_effects_test');
// Check that ::applyEffect generates image with expected color shift.
$image_style->createDerivative($original_uri, $image_style->buildUri($original_uri));
$image = $image_factory->get($generated_uri, 'gd');
$this->assertTrue($this->colorsAreEqual($colors[0], $this->getPixelColor($image, 0, 0)));
$this->assertTrue($this->colorsAreEqual($colors[1], $this->getPixelColor($image, 39, 0)));
$this->assertTrue($this->colorsAreEqual($colors[2], $this->getPixelColor($image, 0, 19)));
$this->assertTrue($this->colorsAreEqual($colors[3], $this->getPixelColor($image, 39, 19)));
// Remove effect.
$uuid = $this->removeEffectFromTestStyle($uuid);
}
}
示例6: testPictureOnNodeComment
/**
* Tests embedded users on node pages.
*/
function testPictureOnNodeComment()
{
$this->drupalLogin($this->webUser);
// Save a new picture.
$image = current($this->drupalGetTestFiles('image'));
$file = $this->saveUserPicture($image);
$node = $this->drupalCreateNode(array('type' => 'article'));
// Enable user pictures on nodes.
$this->config('system.theme.global')->set('features.node_user_picture', TRUE)->save();
$image_style_id = $this->config('core.entity_view_display.user.user.compact')->get('content.user_picture.settings.image_style');
$style = ImageStyle::load($image_style_id);
$image_url = $style->buildUrl($file->getfileUri());
$alt_text = 'Profile picture for user ' . $this->webUser->getUsername();
// Verify that the image is displayed on the node page.
$this->drupalGet('node/' . $node->id());
$elements = $this->cssSelect('.node__meta .field--name-user-picture img[alt="' . $alt_text . '"][src="' . $image_url . '"]');
$this->assertEqual(count($elements), 1, 'User picture with alt text found on node page.');
// Enable user pictures on comments, instead of nodes.
$this->config('system.theme.global')->set('features.node_user_picture', FALSE)->set('features.comment_user_picture', TRUE)->save();
$edit = array('comment_body[0][value]' => $this->randomString());
$this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit, t('Save'));
$elements = $this->cssSelect('.comment__meta .field--name-user-picture img[alt="' . $alt_text . '"][src="' . $image_url . '"]');
$this->assertEqual(count($elements), 1, 'User picture with alt text found on the comment.');
// Disable user pictures on comments and nodes.
$this->config('system.theme.global')->set('features.node_user_picture', FALSE)->set('features.comment_user_picture', FALSE)->save();
$this->drupalGet('node/' . $node->id());
$this->assertNoRaw(file_uri_target($file->getFileUri()), 'User picture not found on node and comment.');
}
示例7: build
/**
* {@inheritdoc}
*/
public function build()
{
$query = \Drupal::entityQuery('node')->condition('status', 1)->condition('type', 'team_member')->sort('field_order', 'ASC');
$nids = $query->execute();
$nodes = node_load_multiple($nids);
//$nodes = entity_load_multiple('node', $nids);
$ind = 1;
$output = '<div class="tabbable tabs-left tabcordion">
<ul class="nav nav-tabs">';
foreach ($nodes as $node) {
$class = $ind == 1 ? 'active' : '';
$output .= '<li class="' . $class . '"><a data-target="#team_member_tab' . $ind . '" data-toggle="tab">' . $node->title->value . '<span>' . $node->get('field_designation')->value . '</span></a></li>';
$ind++;
}
$ind = 1;
$output .= '</ul>
<div class="tab-content">';
foreach ($nodes as $node) {
$class = $ind == 1 ? 'active' : '';
if (is_object($node->field_image->entity)) {
$path = $node->field_image->entity->getFileUri();
$url = ImageStyle::load('person_picture')->buildUrl($path);
$tab_content_html = '<div class="row"><div class="col-md-3"><img src="' . $url . '" alt=""></div><div class="col-md-9">' . $node->get('body')->value . '</div> </div>';
} else {
$tab_content_html = '<div>' . $node->get('body')->value . '</div>';
}
$output .= '<div class="tab-pane ' . $class . '" id="team_member_tab' . $ind . '"> ' . $tab_content_html . ' </div>';
$ind++;
}
$output .= '</div>
</div>';
return array('#type' => 'markup', '#markup' => $output, '#attached' => array('library' => array('barney_river_utilities/tabcordion', 'barney_river_utilities/tabcordion_hook')));
}
示例8: buildForm
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $grouping = NULL)
{
// Load logo image.
$rendered_image = NULL;
if (!empty($grouping->logo_fid)) {
$file = File::load($grouping->logo_fid);
if ($file) {
$logo_url = ImageStyle::load('ea_groupings_200x200')->buildUrl($file->getFileUri());
$image_array = array('#theme' => 'image', '#uri' => $logo_url, '#alt' => $this->t('Logo for @grouping', array('@grouping' => $grouping->title)), '#title' => $this->t('@grouping', array('@grouping' => $grouping->title)));
$rendered_image = \Drupal::service('renderer')->render($image_array);
}
}
// Construct form.
$form = array();
$form['gid'] = array('#type' => 'value', '#value' => $grouping->gid);
$form['add'] = array('#type' => 'fieldset', '#description' => $this->t('Update grouping'), '#title' => $this->t('Edit grouping'));
$form['add']['title'] = array('#type' => 'textfield', '#title' => $this->t('Title'), '#description' => $this->t('Title of grouping'), '#size' => 20, '#maxlength' => 20, '#required' => FALSE, '#default_value' => $grouping->title);
$form['add']['description'] = array('#type' => 'textarea', '#title' => $this->t('Description'), '#description' => $this->t('A short description of the grouping'), '#required' => FALSE, '#default_value' => $grouping->description);
$form['add']['logo'] = array('#type' => 'markup', '#markup' => $rendered_image);
$form['add']['logo_fid'] = array('#title' => $this->t('Update logo'), '#type' => 'managed_file', '#description' => $this->t('Upload a logo for the grouping'), '#default_value' => NULL, '#upload_location' => 'public://logos/');
$form['add']['time_zone'] = array('#title' => $this->t('Timezone'), '#type' => 'select', '#description' => $this->t('Select a time zone for the grouping'), '#options' => _ea_groupings_get_time_zones(), '#default_value' => $grouping->time_zone);
$form['add']['parent_gid'] = array('#type' => 'select', '#description' => $this->t('Select a parent grouping'), '#options' => _ea_groupings_get_groupings_list(FALSE, $grouping->gid), '#disabled' => _ea_groupings_is_parent($grouping->gid), '#default_value' => $grouping->parent_gid);
$form['add']['submit'] = array('#type' => 'submit', '#name' => 'add_group', '#value' => $this->t('Update grouping'), '#weight' => 100);
$form['add']['old_title'] = array('#type' => 'value', '#value' => $grouping->title);
$form['add']['old_logo_fid'] = array('#type' => 'value', '#value' => $grouping->logo_fid);
$form['add']['old_parent_gid'] = array('#type' => 'value', '#value' => $grouping->parent_gid);
return $form;
}
示例9: doTestWatermarkOperations
/**
* Watermark operations test.
*/
public function doTestWatermarkOperations()
{
$image_factory = $this->container->get('image.factory');
$test_file = drupal_get_path('module', 'simpletest') . '/files/image-1.png';
$original_uri = file_unmanaged_copy($test_file, 'public://', FILE_EXISTS_RENAME);
$generated_uri = 'public://styles/image_effects_test/public/' . \Drupal::service('file_system')->basename($original_uri);
$watermark_file = drupal_get_path('module', 'simpletest') . '/files/image-test.png';
$watermark_uri = file_unmanaged_copy($watermark_file, 'public://', FILE_EXISTS_RENAME);
$effect = ['id' => 'image_effects_watermark', 'data' => ['placement' => 'left-top', 'x_offset' => 1, 'y_offset' => 1, 'opacity' => 100, 'watermark_image' => $watermark_uri]];
$uuid = $this->addEffectToTestStyle($effect);
// Load Image Style.
$image_style = ImageStyle::load('image_effects_test');
// Check that ::applyEffect generates image with expected watermark.
$image_style->createDerivative($original_uri, $image_style->buildUri($original_uri));
$image = $image_factory->get($generated_uri, 'gd');
$watermark = $image_factory->get($watermark_uri, 'gd');
$this->assertFalse($this->colorsAreEqual($this->getPixelColor($watermark, 0, 0), $this->getPixelColor($image, 0, 0)));
$this->assertTrue($this->colorsAreEqual($this->getPixelColor($watermark, 0, 0), $this->getPixelColor($image, 1, 1)));
$this->assertTrue($this->colorsAreEqual($this->getPixelColor($watermark, 0, 1), $this->getPixelColor($image, 1, 2)));
$this->assertTrue($this->colorsAreEqual($this->getPixelColor($watermark, 0, 3), $this->getPixelColor($image, 1, 4)));
// Remove effect.
$this->removeEffectFromTestStyle($uuid);
// Test for watermark PNG image with full transparency set, 100% opacity
// watermark.
$test_file = drupal_get_path('module', 'image_effects') . '/tests/images/fuchsia.png';
$original_uri = file_unmanaged_copy($test_file, 'public://', FILE_EXISTS_RENAME);
$generated_uri = 'public://styles/image_effects_test/public/' . \Drupal::service('file_system')->basename($original_uri);
$watermark_file = drupal_get_path('module', 'simpletest') . '/files/image-test.png';
$watermark_uri = file_unmanaged_copy($watermark_file, 'public://', FILE_EXISTS_RENAME);
$effect = ['id' => 'image_effects_watermark', 'data' => ['placement' => 'left-top', 'x_offset' => 0, 'y_offset' => 0, 'opacity' => 100, 'watermark_image' => $watermark_uri]];
$uuid = $this->addEffectToTestStyle($effect);
// Load Image Style.
$image_style = ImageStyle::load('image_effects_test');
// Check that ::applyEffect generates image with expected transparency.
$image_style->createDerivative($original_uri, $image_style->buildUri($original_uri));
$image = $image_factory->get($generated_uri, 'gd');
$this->assertTrue($this->colorsAreEqual($this->getPixelColor($image, 0, 19), $this->fuchsia));
// Remove effect.
$this->removeEffectFromTestStyle($uuid);
// Test for watermark PNG image with full transparency set, 50% opacity
// watermark.
$test_file = drupal_get_path('module', 'image_effects') . '/tests/images/fuchsia.png';
$original_uri = file_unmanaged_copy($test_file, 'public://', FILE_EXISTS_RENAME);
$generated_uri = 'public://styles/image_effects_test/public/' . \Drupal::service('file_system')->basename($original_uri);
$watermark_file = drupal_get_path('module', 'simpletest') . '/files/image-test.png';
$watermark_uri = file_unmanaged_copy($watermark_file, 'public://', FILE_EXISTS_RENAME);
$effect = ['id' => 'image_effects_watermark', 'data' => ['placement' => 'left-top', 'x_offset' => 0, 'y_offset' => 0, 'opacity' => 50, 'watermark_image' => $watermark_uri]];
$uuid = $this->addEffectToTestStyle($effect);
// Load Image Style.
$image_style = ImageStyle::load('image_effects_test');
// Check that ::applyEffect generates image with expected alpha.
$image_style->createDerivative($original_uri, $image_style->buildUri($original_uri));
$image = $image_factory->get($generated_uri, 'gd');
$this->assertTrue($this->colorsAreEqual($this->getPixelColor($image, 0, 19), $this->fuchsia));
// GD and ImageMagick return slightly different colors, use the
// ::colorsAreClose method.
$this->assertTrue($this->colorsAreClose($this->getPixelColor($image, 39, 0), [127, 127, 127, 0]));
// Remove effect.
$this->removeEffectFromTestStyle($uuid);
}
示例10: doTestContrastOperations
/**
* Contrast operations test.
*/
public function doTestContrastOperations()
{
$image_factory = $this->container->get('image.factory');
$image_toolkit_id = $image_factory->getToolkitId();
// Test on the PNG test image.
$test_file = drupal_get_path('module', 'simpletest') . '/files/image-test.png';
$original_uri = file_unmanaged_copy($test_file, 'public://', FILE_EXISTS_RENAME);
$generated_uri = 'public://styles/image_effects_test/public/' . \Drupal::service('file_system')->basename($original_uri);
// Test data.
$test_data = ['0' => [$this->red, $this->green, $this->transparent, $this->blue], '-50' => [$image_toolkit_id === 'imagemagick' ? array(180, 75, 75, 0) : array(159, 95, 95, 0), $image_toolkit_id === 'imagemagick' ? array(75, 180, 75, 0) : array(95, 159, 95, 0), $this->transparent, $image_toolkit_id === 'imagemagick' ? array(75, 75, 180, 0) : array(95, 95, 159, 0)], '-100' => [$image_toolkit_id === 'imagemagick' ? array(128, 128, 128, 0) : array(127, 127, 127, 0), $image_toolkit_id === 'imagemagick' ? array(128, 128, 128, 0) : array(127, 127, 127, 0), $this->transparent, $image_toolkit_id === 'imagemagick' ? array(128, 128, 128, 0) : array(127, 127, 127, 0)], '50' => [array(255, 0, 0, 0), array(0, 255, 0, 0), $this->transparent, array(0, 0, 255, 0)], '100' => [array(255, 0, 0, 0), array(0, 255, 0, 0), $this->transparent, array(0, 0, 255, 0)]];
foreach ($test_data as $key => $colors) {
// Add contrast effect to the test image style.
$effect = ['id' => 'image_effects_contrast', 'data' => ['level' => $key]];
$uuid = $this->addEffectToTestStyle($effect);
// Load Image Style.
$image_style = ImageStyle::load('image_effects_test');
// Check that ::applyEffect generates image with expected contrast.
$image_style->createDerivative($original_uri, $image_style->buildUri($original_uri));
$image = $image_factory->get($generated_uri, 'gd');
$this->assertTrue($this->colorsAreEqual($colors[0], $this->getPixelColor($image, 0, 0)));
$this->assertTrue($this->colorsAreEqual($colors[1], $this->getPixelColor($image, 39, 0)));
$this->assertTrue($this->colorsAreEqual($colors[2], $this->getPixelColor($image, 0, 19)));
$this->assertTrue($this->colorsAreEqual($colors[3], $this->getPixelColor($image, 39, 19)));
// Remove effect.
$uuid = $this->removeEffectFromTestStyle($uuid);
}
}
示例11: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->fileSystem = $this->container->get('file_system');
$this->config('system.file')->set('default_scheme', 'public')->save();
$this->imageStyle = ImageStyle::create(['name' => 'test']);
$this->imageStyle->save();
}
示例12: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->style = ImageStyle::create(['name' => 'style_foo', 'label' => $this->randomString()]);
$this->style->save();
$this->replacementStyle = ImageStyle::create(['name' => 'style_bar', 'label' => $this->randomString()]);
$this->replacementStyle->save();
$this->entityTypeManager = \Drupal::entityTypeManager();
}
示例13: renderThumbnail
/**
* {@inheritdoc}
*/
public function renderThumbnail($image_style, $link_url)
{
$this->downloadThumbnail();
$output = ['#theme' => 'image', '#uri' => !empty($image_style) ? ImageStyle::load($image_style)->buildUrl($this->getLocalThumbnailUri()) : $this->getLocalThumbnailUri()];
if ($link_url) {
$output = ['#type' => 'link', '#title' => $output, '#url' => $link_url];
}
return $output;
}
示例14: testEntityDisplayDependency
/**
* Tests the dependency between ImageStyle and entity display components.
*/
public function testEntityDisplayDependency()
{
// Create two image styles.
/** @var \Drupal\image\ImageStyleInterface $style */
$style = ImageStyle::create(['name' => 'main_style']);
$style->save();
/** @var \Drupal\image\ImageStyleInterface $replacement */
$replacement = ImageStyle::create(['name' => 'replacement_style']);
$replacement->save();
// Create a node-type, named 'note'.
$node_type = NodeType::create(['type' => 'note']);
$node_type->save();
// Create an image field and attach it to the 'note' node-type.
FieldStorageConfig::create(['entity_type' => 'node', 'field_name' => 'sticker', 'type' => 'image'])->save();
FieldConfig::create(['entity_type' => 'node', 'field_name' => 'sticker', 'bundle' => 'note'])->save();
// Create the default entity view display and set the 'sticker' field to use
// the 'main_style' images style in formatter.
/** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display */
$view_display = EntityViewDisplay::create(['targetEntityType' => 'node', 'bundle' => 'note', 'mode' => 'default', 'status' => TRUE])->setComponent('sticker', ['settings' => ['image_style' => 'main_style']]);
$view_display->save();
// Create the default entity form display and set the 'sticker' field to use
// the 'main_style' images style in the widget.
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
$form_display = EntityFormDisplay::create(['targetEntityType' => 'node', 'bundle' => 'note', 'mode' => 'default', 'status' => TRUE])->setComponent('sticker', ['settings' => ['preview_image_style' => 'main_style']]);
$form_display->save();
// Check that the entity displays exists before dependency removal.
$this->assertNotNull(EntityViewDisplay::load($view_display->id()));
$this->assertNotNull(EntityFormDisplay::load($form_display->id()));
// Delete the 'main_style' image style. Before that, emulate the UI process
// of selecting a replacement style by setting the replacement image style
// ID in the image style storage.
/** @var \Drupal\image\ImageStyleStorageInterface $storage */
$storage = $this->container->get('entity.manager')->getStorage($style->getEntityTypeId());
$storage->setReplacementId('main_style', 'replacement_style');
$style->delete();
// Check that the entity displays exists after dependency removal.
$this->assertNotNull($view_display = EntityViewDisplay::load($view_display->id()));
$this->assertNotNull($form_display = EntityFormDisplay::load($form_display->id()));
// Check that the 'sticker' formatter component exists in both displays.
$this->assertNotNull($formatter = $view_display->getComponent('sticker'));
$this->assertNotNull($widget = $form_display->getComponent('sticker'));
// Check that both displays are using now 'replacement_style' for images.
$this->assertSame('replacement_style', $formatter['settings']['image_style']);
$this->assertSame('replacement_style', $widget['settings']['preview_image_style']);
// Delete the 'replacement_style' without setting a replacement image style.
$replacement->delete();
// The entity view and form displays exists after dependency removal.
$this->assertNotNull($view_display = EntityViewDisplay::load($view_display->id()));
$this->assertNotNull($form_display = EntityFormDisplay::load($form_display->id()));
// The 'sticker' formatter component should be hidden in view display.
$this->assertNull($view_display->getComponent('sticker'));
$this->assertTrue($view_display->get('hidden')['sticker']);
// The 'sticker' widget component should be active in form displays, but the
// image preview should be disabled.
$this->assertNotNull($widget = $form_display->getComponent('sticker'));
$this->assertSame('', $widget['settings']['preview_image_style']);
}
示例15: doTestSetCanvasOperations
/**
* Set canvas operations test.
*/
public function doTestSetCanvasOperations()
{
$image_factory = $this->container->get('image.factory');
$test_file = drupal_get_path('module', 'simpletest') . '/files/image-test.png';
$original_uri = file_unmanaged_copy($test_file, 'public://', FILE_EXISTS_RENAME);
$generated_uri = 'public://styles/image_effects_test/public/' . \Drupal::service('file_system')->basename($original_uri);
// Test EXACT size canvas.
$effect = ['id' => 'image_effects_set_canvas', 'data' => ['canvas_size' => 'exact', 'canvas_color][container][transparent' => FALSE, 'canvas_color][container][hex' => '#FF00FF', 'canvas_color][container][opacity' => 100, 'exact][width' => '200%', 'exact][height' => '200%']];
$uuid = $this->addEffectToTestStyle($effect);
// Load Image Style.
$image_style = ImageStyle::load('image_effects_test');
// Check that ::transformDimensions returns expected dimensions.
$image = $image_factory->get($original_uri);
$this->assertEqual(40, $image->getWidth());
$this->assertEqual(20, $image->getHeight());
$url = file_url_transform_relative($image_style->buildUrl($original_uri));
$variables = array('#theme' => 'image_style', '#style_name' => 'image_effects_test', '#uri' => $original_uri, '#width' => $image->getWidth(), '#height' => $image->getHeight());
$this->assertEqual('<img src="' . $url . '" width="80" height="40" alt="" class="image-style-image-effects-test" />', $this->getImageTag($variables));
// Check that ::applyEffect generates image with expected canvas.
$image_style->createDerivative($original_uri, $image_style->buildUri($original_uri));
$image = $image_factory->get($generated_uri, 'gd');
$this->assertEqual(80, $image->getWidth());
$this->assertEqual(40, $image->getHeight());
$this->assertTrue($this->colorsAreEqual($this->fuchsia, $this->getPixelColor($image, 0, 0)));
$this->assertTrue($this->colorsAreEqual($this->fuchsia, $this->getPixelColor($image, 79, 0)));
$this->assertTrue($this->colorsAreEqual($this->fuchsia, $this->getPixelColor($image, 0, 39)));
$this->assertTrue($this->colorsAreEqual($this->fuchsia, $this->getPixelColor($image, 79, 39)));
// Remove effect.
$this->removeEffectFromTestStyle($uuid);
// Test RELATIVE size canvas.
$effect = ['id' => 'image_effects_set_canvas', 'data' => ['canvas_size' => 'relative', 'canvas_color][container][transparent' => FALSE, 'canvas_color][container][hex' => '#FFFF00', 'canvas_color][container][opacity' => 100, 'relative][right' => 10, 'relative][left' => 20, 'relative][top' => 30, 'relative][bottom' => 40]];
$uuid = $this->addEffectToTestStyle($effect);
// Load Image Style.
$image_style = ImageStyle::load('image_effects_test');
// Check that ::transformDimensions returns expected dimensions.
$image = $image_factory->get($original_uri);
$this->assertEqual(40, $image->getWidth());
$this->assertEqual(20, $image->getHeight());
$url = file_url_transform_relative($image_style->buildUrl($original_uri));
$variables = array('#theme' => 'image_style', '#style_name' => 'image_effects_test', '#uri' => $original_uri, '#width' => $image->getWidth(), '#height' => $image->getHeight());
$this->assertEqual('<img src="' . $url . '" width="70" height="90" alt="" class="image-style-image-effects-test" />', $this->getImageTag($variables));
// Check that ::applyEffect generates image with expected canvas.
$image_style->createDerivative($original_uri, $image_style->buildUri($original_uri));
$image = $image_factory->get($generated_uri, 'gd');
$this->assertEqual(70, $image->getWidth());
$this->assertEqual(90, $image->getHeight());
$this->assertTrue($this->colorsAreEqual($this->yellow, $this->getPixelColor($image, 0, 0)));
$this->assertTrue($this->colorsAreEqual($this->yellow, $this->getPixelColor($image, 69, 0)));
$this->assertTrue($this->colorsAreEqual($this->yellow, $this->getPixelColor($image, 0, 89)));
$this->assertTrue($this->colorsAreEqual($this->yellow, $this->getPixelColor($image, 69, 89)));
// Remove effect.
$this->removeEffectFromTestStyle($uuid);
}