Skip to content

Instantly share code, notes, and snippets.

@jonom
Last active August 29, 2015 14:21
Show Gist options
  • Save jonom/b6faf6e32d7cb21241e1 to your computer and use it in GitHub Desktop.
Save jonom/b6faf6e32d7cb21241e1 to your computer and use it in GitHub Desktop.
SilverStripe crop image to maximum width or height
<?php
class MaxWidthHeightImageExtension extends DataExtension {
/**
* Crop an image if it exceeds a certain height.
* Use in templates e.g. $Image.SetWidth(200).MaxHeight(200)
*
* @param integer $maxHeight Max height of image
* @return Image
*/
public function MaxHeight($maxHeight) {
if ($this->owner->height > $maxHeight) return $this->owner->CroppedImage($this->owner->width, $maxHeight);
return $this->owner;
}
/**
* Crop an image if it exceeds a certain width.
* Use in templates e.g. $Image.SetHeight(200).MaxWidth(200)
*
* @param integer $maxWidth Max width of image
* @return Image
*/
public function MaxWidth($maxWidth) {
if ($this->owner->width > $maxWidth) return $this->owner->CroppedImage($maxWidth, $this->owner->height);
return $this->owner;
}
}
@JorisDebonnet
Copy link

It's so beautiful it should be included in SilverStripe! 😃

@jonom
Copy link
Author

jonom commented May 20, 2015

Haha thanks I was just creating an issue for that purpose ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment