Using Image Sizes

If you create image sizes using the class ImageSize, you can display them in the Size & Texts tab of the Image Editor.

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
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;

public class StaticImageSizeProvider implements ImageSizeProvider {

    @Override
    public Set<ImageSize> getAll() {
        Set<ImageSize> myImageSizes = new HashSet<>();
        myImageSizes.add(ImageSizeMaker.makeImage("Large", "large", 100, 100));
        myImageSizes.add(ImageSizeMaker.makeImage("Huge", "huge", 200, 200));
        myImageSizes.add(ImageSizeMaker.makeImage("Gargantuan", "gartanutan", 300, 300));
        myImageSizes.add(ImageSizeMaker.makeImage("Massive", "massive", 400, 400));
        return myImageSizes;
    }

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

In the previous snippet—

  • Line 10 declares a concrete class that implements ImageSizeProvider.
  • Line 12–20 implement the method getAll. You must implement this method for instances of ImageSize to appear in the image editor.
    • Line 14 instantiates a set of image sizes.
    • Lines 15–18 instantiate custom image sizes with a display name, internal name, width, and height, and add the custom image sizes to the set.

Typical implementations of ImageSizeProvider read image sizes defined in a theme’s configuration file _config.json. That file includes an array imageSizes of internal names. For example, the following listing defines an image size with internal name massive.

{
  "imageSizes": {
    "massive": {
      "width": 500,
      "height": 500
    }
  }
}

If you create additional image sizes as in the snippet Implementing ImageSizeProvider, ensure they do not collide with any of the internal names retrieved from _config.json. For example, because the previous JSON snippet configures an image size with internal name massive, it collides with an image of the same name in the snippet Implementing ImageSizeProvider. You need to determine how to address such collisions. (For a best practice for naming image sizes, see Naming Convention for Image Sizes.)

Step 2: Add Set of Images to ImageSizeProvider Stack

 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: