Problem/Motivation

I'm seeing the following when I update a single crop selection on an Image. Since the crop x,y coordinates have changed, ImageWidgetCropManager::updateCropProperties() is called. If we take a look at this method:

public function updateCropProperties(Crop $crop, array $crop_properties) {
  // Parse all properties if this crop have changed.
  foreach ($crop_properties as $crop_coordinate => $value) {
    // Edit the crop properties if he have changed.
    $crop->set($crop_coordinate, $value, TRUE)
    ->save();
  }
}

we can see $crop->set($crop_coordinate, $value, TRUE) is called for each item in $crop_properties.
Looking at $crop_properties, a sample value looks like:

Array
(
    [width] => 1055
    [height] => 593
    [x] => 528
    [y] => 1070
)

It appears that the Crop entity is being saved for each item in the array (so 4x). If we take a look at Crop::save():

Crop::save() {
    parent::save();
    image_path_flush($this->uri->value);
}

Thus for each Crop entity save, image_path_flush() is called. image_path_flush() looks like:

function image_path_flush($path) {
  $styles = ImageStyle::loadMultiple();
  foreach ($styles as $style) {
    $style->flush($path);
  }
}

In summary the side effect of saving the Crop entity for each setting of each of the 4 crop properties results in a load of all Image styles present on the system and a flush of these image styles.

Proposed resolution

When you have only a couple image styles and a couple of crop types, this probably isn't that noticeable. However, if you have more crop types and more image styles the impact of repeatedly flushing all of the image styles can reduce performance.

I'm not sure if it is necessary to save after setting each crop property. I think we can move the Crop::save() in updateCropProperties() to outside of the for loop.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mirie created an issue. See original summary.

mirie’s picture

Here's a patch that moves the save() to outside of the for loop.

woprrr’s picture

Hii @mirie,

Thanks a lot for you analyse and feedback !!!

It's true that case can be optimized !! I just need to verify the real impact in my memories in 8.0-RC we have a limitation with that (needed to save each coordinates one by one ...). i test it fast !!

woprrr’s picture

Status: Needs review » Reviewed & tested by the community

All ok Thanks :)

  • woprrr committed a58fe8c on 8.x-1.x authored by mirie
    Issue #2811755 by mirie: Reduce number of image style flushes on crop...
deviantintegral’s picture

Status: Reviewed & tested by the community » Fixed
woprrr’s picture

Thank @deviantintegral to have switch to fixed :)

woprrr’s picture

Status: Fixed » Closed (fixed)