Problem/Motivation

The image style generation has been improved in #2027423: Make image style system flexible. Now a module can change the way image derivatives are generated. However, right now altering the derivative URI can be done by extending \Drupal\image\Entity\ImageStyle. This is a problem because the last module to override the image style class wins, and everyone else loses.

Proposed resolution

Allow modules to alter the image style derivative generated URI by implementing a new hook_image_style_uri_alter().

Remaining tasks

None.

User interface changes

None.

API changes

New hook hook_image_style_uri_alter().

Data model changes

None.

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Feature because gives new, more flexible, ways for modules to handle the image derivatives generation.
Issue priority Not critical because right now modules still can alter the image derivative generation the last win and all other lose.
Disruption No disruption.
CommentFileSizeAuthor
#112 1358896-101-reroll.patch8.07 KBSocialNicheGuru
#101 1358896-101.patch8.38 KBbceyssens
#99 1358896-99.patch8.4 KBbceyssens
#96 1358896-96.patch8.37 KBa.dmitriiev
#95 1358896-95.patch8.37 KBa.dmitriiev
#92 1358896-92.patch5.7 KBa.dmitriiev
#88 1358896-88.patch4.69 KBa.dmitriiev
#88 interdiff_1358896_85-88.txt1.13 KBa.dmitriiev
#85 interdiff_1358896_83-85.txt1.61 KBankithashetty
#85 1358896-85.patch5.82 KBankithashetty
#83 1358896-83.patch6.82 KBKevin.-
#82 interdiff_77-81.txt696 bytesvsujeetkumar
#81 1358896-81.patch6.92 KBvsujeetkumar
#79 1358896-77.patch6.93 KBThomasDik
#76 1358896_76.patch6.87 KBvsujeetkumar
#74 1358896-72.patch7.01 KBxxronis
#72 1358896-72.patch7.31 KBxxronis
#70 1358896-70.patch6.98 KBPrineShazar
#70 1358896-70.patch6.98 KBPrineShazar
#67 interdiff_61-67.txt1.71 KBdpagini
#67 1358896-67.patch6.94 KBdpagini
#61 interdiff-1358896-59-61.txt2.03 KBharsha012
#61 1358896-61.patch6.89 KBharsha012
#59 interdiff-1358896-56-59.txt3.74 KBharsha012
#59 1358896-59.patch6.76 KBharsha012
#56 interdiff-1358896-53-56.txt886 bytesharsha012
#56 1358896-56.patch6.75 KBharsha012
#53 interdiff-47-53.txt1.43 KBharsha012
#53 1358896-53.patch6.75 KBharsha012
#47 1358896-47.patch6.75 KBgrndlvl
#37 1358896-37.patch6.99 KBclaudiu.cristea
#37 interdiff.txt864 bytesclaudiu.cristea
#33 1358896-33.patch6.93 KBclaudiu.cristea
#33 interdiff.txt1.17 KBclaudiu.cristea
#31 interdiff.txt805 bytesclaudiu.cristea
#31 1358896-31.patch6.85 KBclaudiu.cristea
#26 1358896-26.patch6.52 KBclaudiu.cristea
#26 interdiff.txt5.7 KBclaudiu.cristea
#24 1358896-24.patch4.28 KBclaudiu.cristea
#14 1358896-hook-image-style-path-alter.patch549 bytesDave Reid
#9 1358896-flexible-scheme-uri-image-style-9.patch2.63 KBclaudiu.cristea
#5 1358896-stream-wrapper-image-style-path.patch1.85 KBDave Reid
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Dave Reid’s picture

Title: image_style_path() needs an alter hook » Allow stream wrappers to determine the URI of their own image style derivatives in image_style_path()

Actually the better method would be to allow the stream wrappers themselves implement a getImageStyleUri() function and have image_style_path() return the result of that method if possible.

Dave Reid’s picture

Should work something like

function image_style_path($style_name, $uri) {
  $scheme = file_uri_scheme($uri);

  if (!$scheme) {
    $scheme = file_default_scheme();
    return "$scheme://styles/$style_name/$scheme/$uri";
  }
  else {
    // Attempt to return an image style URI using the appropriate wrapper.
    if ($wrapper = file_stream_wrapper_get_instance_by_uri($uri)) {
      if (method_exists($wrapper, 'getImageStyleUri')) {
        return $wrapper->getImageStyleUri($style_name);
      }
    }

    // Return a default image style URI.
    return "$scheme://styles/$style_name/$scheme/" . file_uri_target($uri);
  }
}
becw’s picture

This would be super useful for anyone using remote stream wrappers. It would make it easy to create local derivatives for non-writable stream wrappers, and it would also allow core image styles to be automatically generated on stream wrappers that don't use file-path-like naming. image_style_create_derivative() will already create derivatives on non-local stream wrappers (by creating them in a remote location and copying them over).

Dave Reid’s picture

Issue tags: +Media Initiative
Dave Reid’s picture

Status: Active » Needs work
FileSize
1.85 KB

Initial patch, very rough.

tms320c’s picture

I believe this comment may be useful here as well.
In short, style generation problem seems to rise from the hook_menu() implementation in image.module. If url path is not identical with path used by "public:" wrapper and is not "private", Drupal won't invoke image_style_deliver() callback.

I have implemented hook_menu() in my module and syles work after that. Of course, I tested it with very simple module, but I believe that if a wrapper is implemented correctly (see File API and PHP streams) it won't have problems with styles creation/deletion.

From the other hand, implementing special-puropse method like getImageStyleUri(), which serves very specific task in otherwise generic file system is arguable.

Dave Reid’s picture

Issue tags: +sprint
benshell’s picture

I've created a related patch where I took a different approach to figuring out where to store image derivatives: #1821166: Support image style derivatives using a different file scheme than the original image

claudiu.cristea’s picture

Title: Allow stream wrappers to determine the URI of their own image style derivatives in image_style_path() » Flexible scheme and URI for image derivatives
Assigned: Dave Reid » claudiu.cristea
Status: Needs work » Needs review
FileSize
2.63 KB

@benshell, your approach is too particular and assume that all derivatives will use the same scheme.

Rerolled #5. If passes, we need to add an alteration point so that modules can decide to use different schemes for different image styles. Coming soon.

Status: Needs review » Needs work

The last submitted patch, 1358896-flexible-scheme-uri-image-style-9.patch, failed testing.

claudiu.cristea’s picture

Looking back to #9 it doesn't seems to me a good approach to let the stream wrapper define a policy for derivative location.

There's another approach that allows extending of \Drupal\image\Plugin\Core\Entity\ImageStyle in order to provide alteration of derivatives stream wrapper, path and even access: #2027423: Make image style system flexible.

claudiu.cristea’s picture

Status: Needs work » Closed (duplicate)

Marking this as duplicate of #2027423: Make image style system flexible.

Dave Reid’s picture

Version: 8.0.x-dev » 7.x-dev
Assigned: claudiu.cristea » Dave Reid
Issue summary: View changes
Status: Closed (duplicate) » Needs work

There is still a valid need for at least an alter hook in Drupal 7 since the changes in #2027423: Make image style system flexible are not backportable at all. I also question the actual flexibility, because I know for a fact that in D7 now, there would be several different modules that would want to alter the way image styles are generated. With the current code in D8 the last module to override the image style generation class wins, and everyone else loses. Which is not flexible.

Dave Reid’s picture

Status: Needs work » Needs review
FileSize
549 bytes

Let's start out a little simpler with just a new alter hook so that modules that want to change the image style path doesn't fail to invalid token.

claudiu.cristea’s picture

  1. +++ b/modules/image/image.module
    @@ -1082,7 +1082,9 @@ function image_style_path($style_name, $uri) {
    +  drupal_alter('image_style_path', $result, $style_name, $uri);
    

    Here, the alterable is $result. The others ($style_name and $uri) are context variables. That said, why not passing as context everything that was previously processed in image_style_path()? I would go with this:

    $context = array(
      'style_name' => $style_name,
      'uri' => $uri,
      'scheme' => $scheme,
      'path' => $path,
    );
    drupal_alter('image_style_path', $result, $context);
    

    This will assure that alter implementations will not have to recompute again $scheme and $path.

    Also there's need for a new entry in docs: image.api.php with the new hook_image_style_path_alter().

  2. +++ b/modules/image/image.module
    @@ -1082,7 +1082,9 @@ function image_style_path($style_name, $uri) {
    +  return $result;
    

    Let's add an empty line before return.

deviantintegral’s picture

I ended up using the patch at #14 to support image styles in the amazons3 module. It's working pretty well, though it is a (mild) annoyance to be completely throwing out $result to recreate it in an alter hook.

I found this useful not just for managing the stream wrapper, but also for changing the entire URL. For S3, we have to ensure that there is always a bucket in the URL and don't have bare paths like s3://image.jpg. With this, I was able to do nearly all of the heavy lifting of image generation in contrib code without having to patch image module at all.

deviantintegral’s picture

Anyone following this might be interested in #2479523: Add a hook_file_stream_wrapper_uri_normalize_alter() hook too.

Dave Reid’s picture

Version: 7.x-dev » 8.0.x-dev
Status: Needs review » Needs work
Issue tags: -sprint +D8Media

Reviewing the original issue, we're going to want this alter hook for Drupal 8 as well.

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 14: 1358896-hook-image-style-path-alter.patch, failed testing.

tim520’s picture

Hi all,

I found error message in Watchdog : Amazon S3 was unable to create an image style derivative. Check the temporary directory configuration and permissions.

But some of my images could be generated correctly, some not.

What is wrong ?

Thanks

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 14: 1358896-hook-image-style-path-alter.patch, failed testing.

claudiu.cristea’s picture

Assigned: Dave Reid » Unassigned
Status: Needs work » Needs review
FileSize
4.28 KB

Added:

  • The alter hook dispatcher.
  • Documentation for the new hook_image_style_uri_alter().
  • Test for hook_image_style_uri_alter().

Status: Needs review » Needs work

The last submitted patch, 24: 1358896-24.patch, failed testing.

claudiu.cristea’s picture

Issue summary: View changes
Status: Needs work » Needs review
FileSize
5.7 KB
6.52 KB
  • Fixed the phpunit test.
  • More meaningful example in docs and test.
  • Added issue summary.
  • Added beta evaluation.
claudiu.cristea’s picture

Assigned: Unassigned » Dave Reid

Green. Reassigning.

mondrake’s picture

Status: Needs review » Needs work

@claudiu.cristea, what about ImageStyle::flush()? If I am not mistaken, with the patch in #26 the altered URI can be anything, and therefore there is no guarantee that derivative images will be flushed. I think that at least the initial part of the altered URI should be enforced to be '{scheme}://styles/{style-name}' so that ::flush() can do its job.

claudiu.cristea’s picture

@mondrake, that we cannot do because we'll lose exactly the main point of this feature request. Modules should take care of their own flushing policy by implementing: hook_image_style_flush().

mondrake’s picture

Ah yeah forgot about that... then maybe we need a comment in the hook docs to make clear that implementations should take care of flushing the custom directories. Also some test for altering URI to a totally arbitrary directory structure and that flushing actually removes it.

claudiu.cristea’s picture

Status: Needs work » Needs review
FileSize
6.85 KB
805 bytes

@mondrake, Just take a look at ImageStyle::flush(). He handles very well the flushing of derivatives inside Drupal. That's because he uses also ::buildUri() to detect the location of file that needs to be flushed. And ::buildUri() returns something already altered ;)

But, it's true that we need to add some docs for the cases when developers are altering the URIs to make them point outside Drupal (to Instagram, etc.)

mondrake’s picture

uses also ::buildUri() to detect the location of file that needs to be flushed

True, but only if a path to a image is passed (i.e. a single derivative needs to be flushed).

But if you are flushing (or updating) the ImageStyle entity, all the derivative images need to be flushed. This occurs by deleting the entire directory and all its files. And here ::buildUri() is not used, only the pattern {scheme}://styles/{style-name} for each writable scheme is deleted.

If, say, your unaltered URI is
public://styles/mystyle/public/field/image/bingo.png
and your altered URI is
public://mymodule/mystyle/bingo.png

then ImageStyle::flush itself will not remove the /mystyle directory from public://mymodule, with the consequence that the derivative images will be out of sync. So, that's where hook_image_style_flush() should also care about.

claudiu.cristea’s picture

FileSize
1.17 KB
6.93 KB

@mondrake, yes, that makes sense. But implementing a whole testing scenario seems to much. I think, now, the docs are enough.

mondrake’s picture

Fine with me, thanks. Leaving at NR for @Dave Reid.

mondrake queued 33: 1358896-33.patch for re-testing.

mondrake’s picture

Status: Needs review » Needs work

Nitpicks:

  1. +++ b/core/modules/image/src/Entity/ImageStyle.php
    @@ -185,7 +208,16 @@ public function buildUri($uri) {
    +    // Assure a new extension, if case.
    

    "Append a different extension to the image file name, if the image style specifies so."

  2. +++ b/core/modules/image/src/Entity/ImageStyle.php
    @@ -185,7 +208,16 @@ public function buildUri($uri) {
    +    // Allow modules to alter the uri before is returned.
    

    ... before it is returned.

claudiu.cristea’s picture

Assigned: Dave Reid » Unassigned
Status: Needs work » Needs review
FileSize
864 bytes
6.99 KB

Thank you for these findings.

claudiu.cristea’s picture

Assigned: Unassigned » Dave Reid

Ouch! Sorry, I didn't wanted to unassign.

Status: Needs review » Needs work

The last submitted patch, 37: 1358896-37.patch, failed testing.

Status: Needs work » Needs review

claudiu.cristea queued 37: 1358896-37.patch for re-testing.

Status: Needs review » Needs work

The last submitted patch, 37: 1358896-37.patch, failed testing.

mondrake’s picture

@claudiu, I have been still thinking about #32.

Now we are building the URI and then pass it to the alter hook with the context.

What about if, instead, we pass, by reference, the components, like e.g. based on the example:

  • scheme: public
  • basepath: styles/mystyle
  • filepath: public/field/image/bingo.png

then, let the hook alter the components, and build the URI only after returning from the alters.

This way we could also add the same alter hook in ImageStyle::flush, and pass only scheme + basepath, for example something like

    // Delete the style directory in each registered wrapper.
    $wrappers = \Drupal::service('stream_wrapper_manager')->getWrappers(StreamWrapperInterface::WRITE_VISIBLE);
    foreach ($wrappers as $wrapper => $wrapper_data) {
+    $context = ['scheme' => $wrapper, 'basepath' => 'styles/' . $this->id(), 'image_style' => $this];
+    $this->moduleHandler->alter('image_style_uri', $context);
-      if (file_exists($directory = $wrapper . '://styles/' . $this->id())) {
+      if (file_exists($directory = $context['scheme'] . '://' . $context['basepath'])) {
        file_unmanaged_delete_recursive($directory);
      }
    }

so that the flushing of the image style is covered the same way of the flushing of a single derivative?

Anonymous’s picture

I have created s3 wrapper for d8 and now im stuck with image derivatives :D

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

grndlvl’s picture

Status: Needs work » Needs review
FileSize
6.75 KB

Re-rolled against 8.3.x

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

orbistertius’s picture

I was searching for a way to make image styles of a private image public. What you are doing here makes a lot of sence to me so please push this into the next release so I can create a module (my first d8 module) adding to the image style form a radio element "Storage as" with "same as original" (default), "public file" and "private file". What can I do to make this work go into core?

josephdpurcell’s picture

Status: Needs review » Reviewed & tested by the community

I believe this issue addresses what was described #1903190: Allow image style derivatives of private images to be stored on the public file system.

Using Drupal 8.3.5 I was able to apply this patch and use the following hook to allow the watermark image style of an image field using private storage to have those watermarked images be available publicly:

/**
 * Implements hook_image_style_uri_alter().
 */
function mymodule_image_style_uri_alter(&$uri, array $context) {
  $image_style_id = $context['image_style']->id();

  // Watermarked images are available publicly.
  if ('my_watermark' == $image_style_id) { 
    $source_scheme = $context['scheme'];
    $path = $context['path'];
    $uri = "public://styles/{$image_style_id}/{$source_scheme}/{$path}";
  }
}

So, the patch works!

I'm setting this to RTBC given that it has tests, works as described/meets requirements, and in my expedited review of the code I didn't find any obvious issues.

andypost’s picture

Version: 8.4.x-dev » 8.5.x-dev
Wim Leers’s picture

Assigned: Dave Reid » Unassigned
Status: Reviewed & tested by the community » Needs work
Issue tags: +API addition, +Needs change record

Bunch of nits, plus missing CR.

  1. +++ b/core/modules/image/image.api.php
    @@ -39,5 +39,38 @@ function hook_image_style_flush($style) {
    + * This hook allows modules to alter the uri of the derivative. By implementing
    ...
    + * builds the uri of an image derivative.
    ...
    + * Note: If you use a special uri alteration that stores the image derivatives
    
    +++ b/core/modules/image/src/Entity/ImageStyle.php
    @@ -183,7 +206,17 @@ public function buildUri($uri) {
    +    // Allow modules to alter the uri before it is returned.
    

    s/uri/URI/

  2. +++ b/core/modules/image/image.api.php
    @@ -39,5 +39,38 @@ function hook_image_style_flush($style) {
    + * be available on a different schema. This hook is called when the image module
    

    s/schema/scheme/

  3. +++ b/core/modules/image/image.api.php
    @@ -39,5 +39,38 @@ function hook_image_style_flush($style) {
    +  if (($name = $context['image_style']->id()) == 'watermarked_low_resolution') {
    

    ===

harsha012’s picture

Status: Needs work » Needs review
FileSize
6.75 KB
1.43 KB

fixed the nit picks and added the CR

https://www.drupal.org/node/2917669

orbistertius’s picture

#50 worked for me, hope to see this patch applied to the core soon.

claudiu.cristea’s picture

Status: Needs review » Needs work
+++ b/core/modules/image/image.api.php
@@ -39,5 +39,38 @@ function hook_image_style_flush($style) {
+ * Alters the derivative uri.
...
+ * This hook allows modules to alter the uri of the derivative. By implementing
...
+ *   The image style uri to be altered.

These replacements were left out: s/uri/URI

harsha012’s picture

Status: Needs work » Needs review
FileSize
6.75 KB
886 bytes

fixed nit picks #55

claudiu.cristea’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs change record

This issue has been RTBC'd in #50. Since #50 there were only nits fixes. I'm RTBCing back (only for the nits part #50 => #56).

I've also improved the change record.

Thank you for the work here.

Wim Leers’s picture

Status: Reviewed & tested by the community » Needs work

Sorry, once more:

  1. +++ b/core/modules/image/image.api.php
    @@ -39,5 +39,38 @@ function hook_image_style_flush($style) {
    + * be available on a different scheme. This hook is called when the image module
    

    Nit: "on a different scheme" sounds strange? In a different stream wrapper?

  2. +++ b/core/modules/image/image.api.php
    @@ -39,5 +39,38 @@ function hook_image_style_flush($style) {
    +function hook_image_style_uri_alter(&$uri, array $context) {
    +  if (($name = $context['image_style']->id()) === 'watermarked_low_resolution') {
    +    $path = $context['path'];
    +    // Low resolution, watermarked images are available publicly, in the
    +    // public:// stream wrapper.
    +    $uri = "public://styles/$name/public/$path";
    +  }
    

    Doesn't this mean only writeable stream wrappers can be used?

    See \Drupal\Core\StreamWrapper\StreamWrapperInterface::WRITE.

  3. +++ b/core/modules/image/src/Entity/ImageStyle.php
    @@ -185,7 +208,17 @@ public function buildUri($uri) {
    +    // Allow modules to alter the uri before it is returned.
    

    s/uri/URI/

  4. +++ b/core/modules/image/src/Tests/ImageStylesPathAndUrlTest.php
    @@ -265,4 +265,20 @@ public function doImageStyleUrlAndPathTests($scheme, $clean_url = TRUE, $extra_s
    +    // The 'watermarked_low_resolution' derivatives are available publicly, in
    +    // the public:// stream wrapper.
    
    +++ b/core/modules/image/tests/modules/image_module_test/image_module_test.module
    @@ -32,3 +32,15 @@ function image_module_test_image_effect_info_alter(&$effects) {
    +    // Low resolution, watermarked images are available publicly, in the
    +    // public:// stream wrapper.
    

    "in the public:// stream wrapper" seems kind of pointless to mention?

  5. +++ b/core/modules/image/tests/modules/image_module_test/image_module_test.module
    @@ -32,3 +32,15 @@ function image_module_test_image_effect_info_alter(&$effects) {
    +  if (($name = $context['image_style']->id()) == 'watermarked_low_resolution') {
    

    ===

  6. +++ b/core/modules/image/tests/src/Unit/ImageStyleTest.php
    @@ -58,10 +58,14 @@ protected function getImageStyleMock($image_effect_id, $image_effect, $stubs = [
    +    $module_handler = $this->getMockBuilder('\Drupal\Core\Extension\ModuleHandlerInterface')
    

    Let's use ModuleHandlerInterface::class, this is easier to refactor in the future.

harsha012’s picture

Status: Needs work » Needs review
FileSize
6.76 KB
3.74 KB

Worked as per the nit picks

claudiu.cristea’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/image/image.api.php
    @@ -65,8 +65,8 @@
    -    // Low resolution, watermarked images are available publicly, in the
    -    // public:// stream wrapper.
    

    I don't understand the "writable" thing. Probably we want to say: "Low resolution, watermarked images are available in the public readable stream wrapper."

  2. +++ b/core/modules/image/src/Tests/ImageStylesPathAndUrlTest.php
    @@ -277,7 +277,7 @@
    -    // the public:// stream wrapper.
    +    // the stream wrapper.
    

    The correct phrase would be: "The 'watermarked_low_resolution' derivatives are available publicly."

  3. +++ b/core/modules/image/tests/src/Unit/ImageStyleTest.php
    @@ -59,7 +59,7 @@
    -    $module_handler = $this->getMockBuilder('\Drupal\Core\Extension\ModuleHandlerInterface')
    +    $module_handler = $this->getMockBuilder('\Drupal\Core\Extension\ModuleHandlerInterface::class')
    

    This should be:

    
    ...
    use Drupal\Core\Extension\ModuleHandlerInterface;
    ...
       $module_handler = $this->getMockBuilder(ModuleHandlerInterface::class)
    
harsha012’s picture

Status: Needs work » Needs review
FileSize
6.89 KB
2.03 KB

fixed as per #60

claudiu.cristea’s picture

Status: Needs review » Reviewed & tested by the community

Thank you. RTBC for changes since #50.

larowlan’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +Needs issue summary update

Hi

I discussed this with @catch and we're not hugely keen on having config entities fire alter hooks.
To try and understand why this is still needed, can we get an issue summary update here?
I see in #12 this was marked as a duplicate and in #13 it was moved to Drupal 7 only.

But then in #18 it was moved back to Drupal 8, but without mentioning why, in particular what the need is that can't be solved by #2027423: Make image style system flexible. I note it was tagged `Media initiative` at the same time, so can we get some information on how the two are related?

Can someone provide some examples of why this is needed - thanks.

Lee

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dpagini’s picture

Here's an attempt at a re-roll for 8.5.5 (and hopefully 8.6.x). I would be interested in the traction that the approach proposed by @mondrake gets, though...

AMDandy’s picture

I've applied #67 against 8.5.6 and the paths are updated but the image is a 404. I've run drush if --all but they're still 404'ing. Did I miss a step?

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

PrineShazar’s picture

Re-rolling patch from dpagini for 8.7.x

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

xxronis’s picture

Status: Needs review » Needs work

The last submitted patch, 72: 1358896-72.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

xxronis’s picture

re-rolling the 8.8.x patch

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

vsujeetkumar’s picture

Status: Needs work » Needs review
FileSize
6.87 KB

Re-roll patch created for 9.1.x.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

ThomasDik’s picture

Modified patch for 9.2.x.

Status: Needs review » Needs work

The last submitted patch, 79: 1358896-77.patch, failed testing. View results

vsujeetkumar’s picture

Fixed the fail tests, Please have a look.

vsujeetkumar’s picture

FileSize
696 bytes

Interdiff added.

Kevin.-’s picture

Re-rolling patch for 9.3.x.

joachim’s picture

+++ b/core/modules/image/src/Entity/ImageStyle.php	(date 1634652867285)
@@ -94,6 +95,28 @@
+  public function __construct(array $values, $entity_type, ModuleHandlerInterface $module_handler = NULL) {

Adding a constructor to an entity class with DI seems a bit weird to me. Entity objects don't do DI, and I seem to remember the reasons involve something like serialisation.

That might not affect config entities, but still, there's no support for this in the Entity system yet.

ankithashetty’s picture

Updated patch in #83 with the changes suggested in #84, i.e., removed constructor from an entity class as it doesn't support DI.

Thanks!

Status: Needs review » Needs work

The last submitted patch, 85: 1358896-85.patch, failed testing. View results

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

a.dmitriiev’s picture

Cleanup patch from #85 to not inject module handler service to ImageStyle in tests.

a.dmitriiev’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 88: 1358896-88.patch, failed testing. View results

a.dmitriiev’s picture

The tests are broken, because now the container is needed to be set in unit test because of \Drupal::moduleHandler() call.

Also, I've noticed that when accessing the url of the image derivative that was made public with a hook, but the original image is in private filesystem, the derivative itself is not automatically created, like it is done for images that are in public folder. Maybe there was something wrong with my NGINX settings, I will check more.

a.dmitriiev’s picture

Fix unit tests

a.dmitriiev’s picture

Status: Needs work » Needs review
a.dmitriiev’s picture

I can confirm that in web/core/modules/image/src/Controller/ImageStyleDownloadController.php on line 160 the source of the image can't be found and controller throws an exception 'Error generating image, missing source file.' This happens when the private image has a public derivative url. So it means that the derivative is not created automatically like for "normal" image styles. I suggest to add here the alter hook. Or the concept of changing the url should be changed and it should be as a setting on ImageStyle entity to select the scheme of the derivative.

a.dmitriiev’s picture

This change includes automatic image derivative creation. Be aware that it also requires another alter hook, so that the original file uri can be restored to point for example to different scheme.

a.dmitriiev’s picture

Re-uploading the patch with fixed code standards

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

bceyssens’s picture

After implementing the new alter hook, patch #96 seems to do the trick for us!

bceyssens’s picture

Rerolled patch against #96 for 9.4.3.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

bceyssens’s picture

Rerolled patch against #96 for 9.5.0.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs change record updates, +Needs Review Queue Initiative

Tagging for CR update as a new api function hook_image_style_source_alter doesn't seem to be referenced

Also was previously tagged for IS update which may still need to happen

Did not test patch.

catch’s picture

I hadn't seen this yet, it looks like the same use-case as #3354207: Allow image style derivatives to use a separate stream wrapper except that would just allow the stream wrapper to be configured. #3027639: Make css/js optimized assets path configurable shows what it looks like.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Dave Reid’s picture

Issue tags: +remote_stream_wrapper
djg_tram’s picture

Oh, dear me, a major showstopper and nothing has changed for years. :-( That hook would be desperately needed, up to the latest 10.1 and more... What could we do to make it happen ASAP?

djg_tram’s picture

For those who happen to be in my shoes: create a route subscriber and alter the route image.style_private to your own controller. Copy Drupal\image\Controller\ImageStyleDownloadController::deliver() to it and modify it to either do something entirely different, or probably just handle your own case and call parent::deliver() to get the original behavior.

In my case, I mostly copied it in its entirety, leaving out where it calls the `hook_file_download_alter()` hook and doing by own permission based decision instead. YMMV.

fonant’s picture

@djg_tram - I can managed to get the derived image written to the public filesystem, but how do I tell Drupal to use the public URL for this image, rather than the private /system/files/... one?

a.dmitriiev’s picture

@fonant, if you use the patch and the hook, there is no need to do anything else. The uri of the image is changed for image style, so if it is public then the url will be for public scheme.

fonant’s picture

@a.dmitriiev thanks, I was wondering how @djg_tram had managed with his method.

I now have this working with the patch and a custom module with a hook. Needs a bit more work and some tidying before I post it here.

SocialNicheGuru’s picture

SocialNicheGuru’s picture

FileSize
8.07 KB

Rerolled 101 to apply to Drupal 10.2.x with the commit, #2786735: Image derivative generation does not work if effect "Convert" in use and file stored in private filesystem which causes the conflict.

SocialNicheGuru’s picture

I am testing it.
the uri has changed.
but i can't seem to get the image_style with the new public uri to be generated.
Should I add code to the hook to generate the image style

I used the example from the patch.

But I am unclear how can the image_style be generated once I rewrite the uri with the hook.

drush cr does not seem to be doing it

a.dmitriiev’s picture

I have the patch and this hook in a custom module.

/**
 * Implements hook_image_style_uri_alter().
 */
function MY_MODULE_image_style_uri_alter(&$uri, array $context) {
  $image_style_name = $context['image_style']->id();
  if (in_array($image_style_name, ['watermarked', 'watermarked_other'])) {
    $path = $context['path'];
    // Low resolution, watermarked images are available in the public readable
    // stream wrapper.
    $uri = "public://styles/$image_style_name/public/$path";
  }
}