/** * Go over all KalturaThumbAsset thumbnails and look for an exact match. * If none is found, look for the one with the nearest aspect ratio (i.e. the one * with the smallest distance from the original). If there are several with the * same delta from original - the one with the largest dimensions will be picked. * * @param array $thumbAssets ThumbAsset objects array * @param int $requiredWidth Thumbnail's requested width * @param int $requiredHeight Thumbnail's requested height * @return kThumbnailDescriptor|null The thumbnail asset with exact/closest * aspect ratio to the required, or null * if the entry doesn't contain thumbnails. */ public static function getNearestAspectRatioThumbnailDescriptorFromThumbAssets($thumbAssets, $requiredWidth, $requiredHeight, $fallbackThumbnailPath = null) { // Calc aspect ratio + distance from requiredAspectRatio $chosenThumbnailDescriptor = null; kThumbnailDescriptor::initDimensions($requiredWidth, $requiredHeight); if ($fallbackThumbnailPath) { $imageSizeArray = getimagesize($fallbackThumbnailPath); $thumbWidth = $imageSizeArray[0]; $thumbHeight = $imageSizeArray[1]; $chosenThumbnailDescriptor = kThumbnailDescriptor::fromParams($thumbWidth, $thumbHeight, $fallbackThumbnailPath, true); } if (empty($thumbAssets)) { return $chosenThumbnailDescriptor; } // Loop all available thumb assets and choose the best match foreach ($thumbAssets as $thumbAsset) { $descriptor = kThumbnailDescriptor::fromThumbAsset($thumbAsset); if (!$chosenThumbnailDescriptor) { $chosenThumbnailDescriptor = $descriptor; } else { // Compare the last best-match with the current descriptor $res = self::compareThumbnailDescriptors($chosenThumbnailDescriptor, $descriptor); // Keep the last best-match unless it needs to go down the ranks (in case $res > 0) $chosenThumbnailDescriptor = $res <= 0 ? $chosenThumbnailDescriptor : $descriptor; } } return $chosenThumbnailDescriptor; }