ImageMegapixels by Robin S

Adds methods to Pageimage objects useful for resizing to a target megapixel value.

Image Megapixels

A module for ProcessWire CMS/CMF. Adds methods to Pageimage objects useful for resizing to a target megapixel value.

Example use


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:

megapixels

Installation


Install the Image Megapixels module.

API


// 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.

Getting dimensions

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'>";
}

Install and use modules at your own risk. Always have a site and database backup before installing new modules.

Latest news

  • ProcessWire Weekly #518
    The 518th issue of ProcessWire Weekly brings in all the latest news from the ProcessWire community. Modules, sites, and more. Read on!
    Weekly.pw / 13 April 2024
  • ProFields Table Field with Actions support
    This week we have some updates for the ProFields table field (FieldtypeTable). These updates are primarily focused on adding new tools for the editor to facilitate input and management of content in a table field.
    Blog / 12 April 2024
  • Subscribe to weekly ProcessWire news

“Indeed, if ProcessWire can be considered as a CMS in its own right, it also offers all the advantages of a CMF (Content Management Framework). Unlike other solutions, the programmer is not forced to follow the proposed model and can integrate his/her ways of doing things.” —Guy Verville, Spiria Digital Inc.