public static function initDimensions($requiredWidth, $requiredHeight) { self::$requiredWidth = $requiredWidth; self::$requiredHeight = $requiredHeight; self::$requiredAspectRatio = $requiredWidth / $requiredHeight; self::$initialized = true; }
/** * Look for the smallest delta from original aspect ratio. * If the deltas match: * If the dimensions match - give priority to the default thumbnail (if any). * Otherwise: * Give priority to the thumbnail the exactly matches the required dimensions. * If none exist - prefer the asset with larger dimensions. * * @param kThumbnailDescriptor $a @see getNearestAspectRatioThumbnailDescriptorFromThumbAssets() * @param kThumbnailDescriptor $b @see getNearestAspectRatioThumbnailDescriptorFromThumbAssets() * @return int (-1) = a before b, (+1) = a after b, (0) = don't care (equal) */ private static function compareThumbnailDescriptors($a, $b) { $aDelta = $a->getDeltaFromOrigAspectRatio(); $bDelta = $b->getDeltaFromOrigAspectRatio(); // Look for the smaller delta if ($aDelta < $bDelta) { return -1; } else { if ($aDelta > $bDelta) { return 1; } else { // Note: Because the aspect ratio is identical, it's enough to check just one dimension (we'll check the width) $aWidth = $a->getWidth(); $bWidth = $b->getWidth(); if ($aWidth != $bWidth) { $requiredWidth = kThumbnailDescriptor::getRequiredWidth(); // Give priority to exact-match dimensions if ($aWidth == $requiredWidth) { return -1; } else { if ($bWidth == $requiredWidth) { return 1; } else { // Give priority to the one the the larger dimensions return $bWidth - $aWidth; // < 0 = $a has priority, > 0 = $b has priority, 0 = equal } } } else { // Boost the priority of the larger asset for the sake of better quality // by looking for the asset with the bigger dimensions return $b->getIsDefault() - $a->getIsDefault(); // -1 = $a has priority, 1 = $b has priority, 0 = equal } } } }