Jump to content

Robin S
 Share

Recommended Posts

Inspired by the "max megapixels" option for the client-side image resizer, I made a simple module that adds target megapixel resizing for Pageimages.

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

 

https://github.com/Toutouwai/ImageMegapixels
https://processwire.com/modules/image-megapixels/

  • Like 14
Link to comment
Share on other sites

@adrianromega, did you see the "Example use" section above?

To expand on this... it's useful in any situation where you want to be fair to a group of images with different aspect ratios. Take the three images shown in the screenshot above. If these images had been resized using maxSize(800,800) for instance, photo 2 (the square image) would be larger than the others in total area - it would contain the most image data.

Now suppose you're running a competition on your website where visitors can vote for the best photo. The three images above have been submitted by three different photographers. If you are resizing by maxSize() rather than megapixels then it's not fair on photographers 1 and 3 that their images are shown smaller than the image from photographer 2.

  • Like 4
Link to comment
Share on other sites

Hi @Robin S, congrats to one more nice module! And I totally understand why you created it. I also (wanted to) use such a feature in different scenarios. Therefor once I added the "weighten" option to the PIA module.

Hopefully you don't mind my following propagation of an alternative way to apply such a functionality. Maybe it is usefull for some people to have a different way to define / calculate / imagine the resulting images.

You call it Megapixels, Pia uses image pixel dimensions for width & height or a single dimension for squares, defining a bounding box and get the images contained & weightened into that box. Additonally Pia handles one out of four values to the weighten-option to prioritize the landscape or portrait images in two ways: 

 

So, what you wanted was allready available in the modules directory for about three years. But I believe, when seeing all your modules, you have had fun building and sharing it.

OT:
For me, the last two years it is not possible to keep track of all the good new stuff coming into the core and from community members (like you). Some years ago, it felt doable for me. Lately I also found "a new possibility" to do things that was allready three years available. Even I had read it in the past and bookmarked it under my other several hundred PW links, it got out of focus. If somebody know a good way to keep track and or archive all the good stuff from here, (besides the few things that get mentions in the weekly news & blog posts), please share. :)

  • Like 5
Link to comment
Share on other sites

9 minutes ago, horst said:

So, what you wanted was allready available in the modules directory for about three years. But I believe, when seeing all your modules, you have had fun building and sharing it.

:). Yeah, @Robin S has been on fire! I've learnt a lot about Inputfields and Hooks from his work.

 

10 minutes ago, horst said:

For me, the last two years it is not possible to keep track of all the good new stuff coming into the core and from community members (like you). Some years ago, it felt doable for me. Lately I also found "a new possibility" to do things that was allready three years available. Even I had read it in the past and bookmarked it under my other several hundred PW links, it got out of focus.

That's me! :P.

  • Like 3
Link to comment
Share on other sites

11 hours ago, horst said:

Therefor once I added the "weighten" option to the PIA module.

Ah, I should have known there would be something in PIA for this - it's the real Swiss Army Knife for images. :-)
I really need to have a proper explore of that module. (I think it's the state of the docs that has been putting me off - maybe you could consolidate/tidy everything into the GitHub readme?)

11 hours ago, horst said:

For me, the last two years it is not possible to keep track of all the good new stuff coming into the core and from community members

I have a very primitive system. For modules I'm interested in I have a spreadsheet with name, category, link, brief description of what it does (in my own words). But I often forget to update it with new modules and just try to rely on memory.

For forum snippets (and links to the original post) I just have a folder with a bunch of text files in it. The text files are named according to what the snippet is about. Then when I'm looking for something I use the quick filter feature in my file manager (XYplorer).

  • Like 4
Link to comment
Share on other sites

This is OT, sorry, but if there is enough interest, the last few posts could be moved to a dedicated Topic...

12 hours ago, Robin S said:

I have a very primitive system. For modules I'm interested in I have a spreadsheet with name, category, link, brief description of what it does (in my own words). But I often forget to update it with new modules and just try to rely on memory.

I do not have to much spare time this year but I would love to help anyone interested in developing an alternative module directory. For example there are such things for WP:

ProcessWire is a native cataloging system, as we all know. So it would not take too much time for a few experienced devs to come up with something useful and opensource. What I could contribute is UI/UX design and creating and editing content, for example. We've read lots of other posts about willing to help to boost the ProcessWire documentation, so I guess there should be enough of us to turn it into a success story.

12 hours ago, Robin S said:

For forum snippets (and links to the original post) I just have a folder with a bunch of text files in it. The text files are named according to what the snippet is about. Then when I'm looking for something I use the quick filter feature in my file manager (XYplorer).

This site I propose could be a "module+snippets finder", ie. a "ProcessWire Solutions" site, meaning both modules and snippets could be in the database, making integrated "solution" searches possible.

Anybody up to the challenge?

Edited by szabesz
typos
  • Like 2
Link to comment
Share on other sites

22 hours ago, szabesz said:

This is OT, sorry, but if there is enough interest, the last few posts could be moved to a dedicated Topic...

I have to agree: this is a good discussion, but doesn't belong to module support thread. Any mods up to that? ;)

22 hours ago, szabesz said:

This site I propose could be a "module+snippets finder", ie. a "ProcessWire Solutions" site, meaning both modules and snippets could be in the database, making integrated "solution" searches possible.

I hope I'm not causing any unnecessary confusion – and I may not see the full picture yet, so perhaps I'm missing the point entirely – but when it comes to snippets, there's already https://processwire-recipes.com/. If you have useful snippets for common tasks etc. I'd suggest submitting them there.

Similarly I'm leaning towards collecting ideas for improving the existing modules directory rather than creating a competing platform. In the 2018 roadmap post Ryan listed modules directory as one focus point for this year, and I'm sure he'd be happy to get some feedback on exactly how it could be improved.

  • Like 4
Link to comment
Share on other sites

14 hours ago, teppo said:

I have to agree: this is a good discussion, but doesn't belong to module support thread. Any mods up to that? ;)

I hope I'm not causing any unnecessary confusion – and I may not see the full picture yet, so perhaps I'm missing the point entirely – but when it comes to snippets, there's already https://processwire-recipes.com/. If you have useful snippets for common tasks etc. I'd suggest submitting them there.

Similarly I'm leaning towards collecting ideas for improving the existing modules directory rather than creating a competing platform. In the 2018 roadmap post Ryan listed modules directory as one focus point for this year, and I'm sure he'd be happy to get some feedback on exactly how it could be improved.

I agree that this is a good discussion and needs to be continued somewhere (I'm not the one for moving things, I haven't figured how to do it right yet). 

I have an upcoming blog project whose sole purpose is to talk about ProcessWire's many features.  Once someone has moved this conversation to a new area, I can further elaborate on what I am working towards.  With that said, I also agree with what @teppo seems to be saying.

  • Like 2
Link to comment
Share on other sites

In v0.2.0 I've added a Pageimage::megapixelsDimensions() method that returns just the dimensions needed to resize an image to a target megapixel value. I used this recently to create a gallery of sponsor logos in normal and high dpi variations. Example from the updated readme:

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

I've also added the module to the PW directory.

  • Like 4
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...