Jump to content

$pageimage->maxSize() not working as described?


Robin S
 Share

Recommended Posts

In the blog post that introduces the $pageimage->maxSize() method, it is described like this:

Quote

$thumb = $image->maxSize($width, $height); 

This will return an image no larger than the given dimensions, without cropping or anything that would modify the proportions. Of course, this also means that it's likely one of the dimensions (width or height) will be less than what you asked for, since nothing can be cropped.

But when I use maxSize() in PW 3.0.37 I do get a cropped image. I have an image that is 1200x600 and if I do $my_image->maxSize(400,400)->url I get an image that is cropped 400x400 when I think it should be uncropped 400x200.

Can anyone confirm? I'm sure this method used to work properly as described.

  • Like 1
Link to comment
Share on other sites

Hi @Robin S

I can confirm that this method has unexpected behavior. 

Not sure, but I think that this:

public function maxSize($width, $height, $options = array()) {
		$w = $this->width();
		$h = $this->height();
		if($w >= $h) {
			if($w > $width && $h > $height) {
				return $this->size($width, $height, $options);
			} else {
				return $this->maxWidth($width, $options);
			}
		} else {
			if($w > $width && $h > $height) {
				return $this->size($width, $height, $options);
			} else {
				return $this->maxHeight($height, $options);
			}
		}
	}

should be

	public function maxSize($width, $height, $options = array()) {
		$w = $this->width();
		$h = $this->height();
		if($w >= $h) {
			return $this->maxWidth($width, $options);
		} else {
			return $this->maxHeight($height, $options);
		}
	}

 

  • Like 2
Link to comment
Share on other sites

  • 7 months later...

i came across this issue on PW 3.0.63

i did some testing and it seems that this issue is not fixed with @Zeka s solution. it is still not working for squared images! see this tests:

screencapture-hrdwebsite-baumrock-C3-BCber-uns-1496916662570.thumb.png.52349a660f61270eecf3f6987ec0227a.png

i changed the function to @Robin S suggested fix and it works:

screencapture-hrdwebsite-baumr.png

	/**
	 * Return an image no larger than the given width and height
	 * 
	 * #pw-group-resize-and-crop
	 * 
	 * @param int $width Max allowed width
	 * @param int $height Max allowed height
	 * @param array $options See `Pageimage::size()` method for options
	 * @return Pageimage
	 * 
	 */
	public function maxSize($width, $height, $options = array()) {
		$defaults = array(
			'upscaling' => false,
			'cropping' => false
			);
		$options = array_merge($defaults, $options);
		return $this->size($width, $height, $options);
	}

works also with smaller images using upscaling => true and cropping => true :)

Link to comment
Share on other sites

  • 6 months later...

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...