Registering image sizes
The easiest way to configure the listing of image sizes in the image editor’s Sizes tab is through a theme’s configuration file _config.json
. However, if you create image sizes using the class ImageSize
, you can register them for display in the Sizes tab of the image editor by following the steps in this section.
Step 1: Implement ImageSizeProvider
import com.psddev.cms.image.ImageSize;
import com.psddev.cms.image.ImageSizeProvider;
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;
}
}
-
Declares a concrete class that implements ImageSizeProvider. (If you are using themes, Brightspot automatically uses the class
ThemeImageSizeProvider
that also implementsImageSizeProvider
to read image sizes from the theme’s configuration file_config.json
.)
-
Implements the method
getAll
. You must implement this method for instances ofImageSize
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.
import com.psddev.cms.image.ImageSizeProvider;
import com.psddev.dari.db.ApplicationFilter;
import com.psddev.dari.util.AbstractFilter;
import com.psddev.dari.util.ThreadLocalStack;
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();
}
}
}
-
Pushes onto the
ImageSizeProvider
stack instances ofStaticImageSizeProvider
as described in the snippet "Implementing ImageSizeProvider." Generally, the class name in that line must be identical to the class name you used for implementingImageSizeProvider
.
Step 3: Reload Brightspot in browser
Brightspot automatically deploys the custom image sizes, and users see them in the image editor’s Sizes 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.)
See also: