Registering Image Sizes

The easiest way to configure the listing of image sizes in the image editor’s Size & Texts tab is through a theme’s configuration file _config.json; for details, see Define Image Sizes. However, if you create image sizes using the class ImageSize, you can register them for display in the Size & Texts tab of the Image Editor by following the steps in this section.

Step 1: Implement ImageSizeProvider

Implementing ImageSizeProvider
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package content.images;

import com.psddev.cms.image.ImageSize;
import com.psddev.cms.image.ImageSizeProvider;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StaticImageSizeProvider implements ImageSizeProvider {

    @Override
    public Set<ImageSize> getAll() {
        return Stream.of(ImageSize.builder()
                       .displayName("Square large 50x50")
                       .internalName("square-large")
                       .width(50)
                       .height(50)
                       .build(),
                   ImageSize.builder()
                       .displayName("Square huge 100x100")
                       .internalName("square-huge")
                       .width(100)
                       .height(100)
                       .build(),
                   ImageSize.builder()
                       .displayName("Square massive 150x150")
                       .internalName("square-massive")
                       .width(150)
                       .height(150)
                       .build(),
                   ImageSize.builder()
                       .displayName("Square gargantuan 200x200")
                       .internalName("square-gargantuan")
                       .width(200)
                       .height(200)
                       .build())
                   .collect(Collectors.toCollection(HashSet::new));
    }

    @Override
    public ImageSize get(List<String> list, String s) {
        return null;
    }
}

In the previous snippet—

  • Line 12 declares a concrete class that implements ImageSizeProvider. (If you are using themes, Brightspot automatically uses the class ThemeImageSizeProvider that also implements ImageSizeProvider to read image sizes from the theme’s configuration file _config.json.)
  • Lines 14–41 implement the method getAll. You must implement this method for instances of ImageSize to appear in the image editor. The method in this snippet instantiates and returns custom image sizes with a display name, internal name, width, and height.

Step 2: Add Set of Images to ImageSizeProvider Stack

Compose a class that extends AbstractFilter and implements AbstractFilter.Auto. Generally, at run time such classes place objects in various parts of the Brightspot UI.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package content.images;

import com.psddev.cms.image.ImageSizeProvider;
import com.psddev.dari.db.ApplicationFilter;
import com.psddev.dari.util.AbstractFilter;
import com.psddev.dari.util.ThreadLocalStack;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

class StaticImageSizeProviderFilter extends AbstractFilter implements AbstractFilter.Auto {

    @Override
    public void updateDependencies(Class<? extends AbstractFilter> filterClass, List<Class<? extends Filter>> dependencies) {
        if (ApplicationFilter.class.isAssignableFrom(filterClass)) {
            dependencies.add(getClass());
        }
    }

    @Override
    protected void doRequest(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws Exception {
        ThreadLocalStack<ImageSizeProvider> providerStack = ImageSizeProvider.getCurrentStack();
        providerStack.push(new StaticImageSizeProvider());

        try {
            chain.doFilter(request, response);

        } finally {
            providerStack.pop();
        }
    }
}

In the previous snippet, line 26 pushes onto the ImageSizeProvider stack instances of StaticImageSizeProvider as described in the snippet Implementing ImageSizeProvider. Generally, the class name in that line must be identical to the class name you used for implementing ImageSizeProvider.

Step 3: Reload Brightspot in Browser

Brightspot automatically deploys the custom image sizes, and users see them in the image editor’s Size & Texts tab after reloading Brightspot in the browser. If the custom image sizes are not visible, restart the Brightspot server. (If the custom image sizes are still not visible, ensure Dari’s reloader is running; for details, see Reloader.)

../../../_images/custom-image-sizes.png

See also: