A module for ProcessWire CMS/CMF. Adds methods to Pageimage objects useful for resizing to a target megapixel value.
You are creating a lightbox gallery of images with different aspect ratios. For the enlargements, rather than setting a fixed maximum width or height you want all the enlargements have the same size in terms of area, allowing a panoramic image to be wider than a square image, for instance.
Another use case is sizing a gallery of sponsor logos. The supplied logos are different aspect ratios but you need to ensure the logos are sized so each has equal prominence on the page.
The effect of resizing three different aspect ratios by the same megapixel target value can be seen in the screenshot below:
Install the Image Megapixels module.
// basic usage
$pageimage = $pageimage->megapixels(float $megapixels);
// usage with all arguments
$pageimage = $pageimage->megapixels(float $megapixels, array $options = []);
Example:
foreach($page->images as $image) {
echo "<img src='$image->megapixels(0.8)->url' alt='$image->description'>"
}
If needed you can supply an array of options for Pageimage::size() as a second argument.
If you just want to get the height and width dimensions needed to size an image to the given number of megapixels you can use the Pageimage::megapixelsDimensions()
method that this module also adds. It returns an array with width
and height
as keys.
Example of how this could be used to output a gallery of logos:
foreach($page->logos as $logo) {
$dimensions = $logo->megapixelsDimensions(0.01);
$width = $dimensions['width'];
$height = $dimensions['height'];
$width2x = $width * 2;
$height2x = $height * 2;
echo "<img src='{$logo->size($width, $height)->url}' srcset='{$logo->size($width, $height)->url} 1x, {$logo->size($width2x, $height2x)->url} 2x' alt='Logo' width='$width' height='$height'>";
}