Created
May 19, 2015 17:18
-
-
Save jonom/ab7b7f733c884ab8e63c to your computer and use it in GitHub Desktop.
Revisions
-
jonom created this gist
May 19, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,78 @@ <?php class BalancedImageExtension extends DataExtension { /** * Creates a scaled image that fits within a given container and does not exceed a specified area (square space). * Useful for ensuring a series of images of various dimensions (especially logos) have approximately the same visual weight. * Use in templates with $BalancedImage * * @param integer $containerWidth Max width of image * @param integer $containerHeight Max height of image * @param integer $multiplier Maximum portion of container space image can take up (max 1) * @param integer $margin Min space between frame edge and image * @return Image */ public function BalancedImage($containerW, $containerH, $multiplier = 0.5, $padX = false, $padY = false, $margin = 0) { // ToDo - Trim any white space included around image before adjusting. $frameW = $containerW; $frameH = $containerH; if ($margin) { //reduce container size to accommodate margin $containerW = $containerW - $margin*2; $containerH = $containerH - $margin*2; } $w = $this->owner->width; $h = $this->owner->height; if (!($w && $h)) return false; $iWHRatio = $w/$h; $cWHRatio = $containerW/$containerH; // Default to 50% of available space $maxPixels = round($containerW*$containerH*$multiplier); // Scale to fill container if ($iWHRatio >= $cWHRatio) { $w = $containerW; $h = round($w/$iWHRatio); } else { $h = $containerH; $w = round($h*$iWHRatio); } // Scale down further if exceeds max space if ($w*$h > $maxPixels) { $w = round(sqrt($maxPixels * $iWHRatio)); $h = round($w / $iWHRatio); } if ($padX && $padY) return $this->owner->getFormattedImage('FramedImage', $w, $h, $frameW, $frameH); if ($padX) return $this->owner->getFormattedImage('FramedImage', $w, $h, $frameW, $h); if ($padY) return $this->owner->getFormattedImage('FramedImage', $w, $h, $w, $frameH); return $this->owner->isSize($w, $h) ? $this->owner : $this->owner->ResizedImage($w, $h); } /** * // ToDo Description * Use in templates with $ * * @param Image_Backend $backend * @param integer $ * @param integer $ * @return Image_Backend */ public function generateFramedImage(Image_Backend $backend, $imageW, $imageH, $frameW, $frameH, $backgroundColor = "FFFFFF"){ // Pad on both sides return $backend->resize($imageW, $imageH)->paddedResize($frameW, $imageH, $backgroundColor)->paddedResize($frameW, $frameH, $backgroundColor); } }