You are here

public function AssetImageHelper::getThumbnailUrlBySize in Media: Acquia DAM 8

Get the URL to the DAM-provided thumbnail if possible.

Parameters

\cweagans\webdam\Entity\Asset $asset: The asset to get the thumbnail size from.

int $thumbnailSize: Find the closest thumbnail size without going over when multiple thumbnails are available.

Return value

string|false The preview URL or FALSE if none available.

File

src/Service/AssetImageHelper.php, line 106

Class

AssetImageHelper
Class AssetImageHelper.

Namespace

Drupal\media_acquiadam\Service

Code

public function getThumbnailUrlBySize(Asset $asset, $thumbnailSize = 1280) {
  if (empty($asset->thumbnailurls[0]->url)) {
    return FALSE;
  }

  // Copy thumbnail array to variable to avoid a notice about indirect
  // access.
  $thumbnails = $asset->thumbnailurls;

  // Default to first regardless of size.
  $biggest_matching = $thumbnails[0]->url;
  foreach ($thumbnails as $thumbnail) {
    if (!empty($thumbnail->url) && $thumbnailSize >= $thumbnail->size) {

      // Certain types do not have a 1280 size available despite returning
      // a URL. We either have to hard code mime types as they crop up, or
      // check if the URL is accessible on our own. Other URL sizes do not
      // appear to have this issue.
      if (1280 == $thumbnail->size && $this
        ->checkRemoteThumbnailStatusCode($thumbnail->url, 403)) {
        continue;
      }
      $biggest_matching = $thumbnail->url;
    }
  }
  return $biggest_matching;
}