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:



Install the Image Megapixels module.


// basic usage
$pageimage = $pageimage->megapixels(float $megapixels);

// usage with all arguments
$pageimage = $pageimage->megapixels(float $megapixels, array $options = []);


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.

Twitter updates

    Error retrieving Twitter status

Latest news

  • ProcessWire Weekly #473
    In the 473rd issue of ProcessWire Weekly we'll check out latest core updates, introduce new third party module called Template Fragment Controller, and more. Read on! / 3 June 2023
  • A look at the new Page Edit Restore module
    The new Page Edit Restore module helps to prevent page edits in the admin from getting lost when the user’s session is lost. This post covers it in detail.
    Blog / 12 May 2023
  • Subscribe to weekly ProcessWire news

“To Drupal, or to ProcessWire? The million dollar choice. We decided to make an early switch to PW. And in retrospect, ProcessWire was probably the best decision we made. Thanks are due to ProcessWire and the amazing system and set of modules that are in place.” —Unni Krishnan, Founder of PigtailPundits